while trying to "do it more than one way", I've stumbled across a weird behaviour of the ternary operator. I had this code which keeps only the recent input in history of Term::Readline:
after some refactoring (I especially didn't like the usage of so much variables/structures) I came up with something like that:if ($optimize_history) { # remove duplicities from history my @history = $term->GetHistory($file); my %history = map {$_ => 1} @history; my @new_history = (); for (reverse @history) { # in reverse order if ($history{"$_"}) { # if not remembered ye +t unshift @new_history, $_; # remember history l +ine delete $history{"$_"}; # and remove it } } $term->SetHistory(@new_history); # set history to new l +ist $term->WriteHistory($file); # save history } else { # only add new line to the history file $term->WriteHistory($file); }
to my big surprise, when I tried to refactor the ternary operator intoif ($optimize_history) { my @history = $term->GetHistory($file); my %history; # eliminate duplicates in history and keep only the most recent for(my $i=scalar(@history)-1;$i>=0;$i--) { $_ = $history[$i]; !$history{$_} ? $history{$_}=1 : splice(@history,$i--,1); } $term->SetHistory(@history); # set history to new list } $term->WriteHistory($file); # save history
I get an error message:$history{$_} ? splice(@history,$i--,1) : $history{$_}=1;
Ehm...Uh? Why is that? Have I - again - been caught by some precedence issue?Can't modify splice in scalar assignment at ./history.pl line 35, near + "1;" Execution of ./history.pl aborted due to compilation errors.
Bye
PetaMem
In reply to Ternary Operator: Condition-Negation by PetaMem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |