# 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); } #### { my (%history, $histcount); # Should I initialise $histcount = 0 or is that "Cargo cult"? sub addHist { $history{ $_[0] } = $histcount++; } sub getHist { return = grep{ $history{ $_ } == ($histcount - $_[0]); } keys %history; } } ... # get $newvalue from somewhere addHist( $newvalue ); ... # deside we want the $n'th previous value my $last_nth = getHist( $n ); ... #### for ( my $i=$#history; $i >=0; $i-- ) { # for $i = (last index of @history) to 0 $_ = $history[$i]; # get the value from the position in @history !$history{$_} # test if that value has been seen before ? $history{$_} = 1 # if it hasn't record it : splice @history, $i--, 1) # if it has remove it from @history # (reducing $i to compensate for its remove; } #### for my $i ($#history .. 0 ) { !exists $history{ history[$i] } # use exists as testing its value against zero will # autovivify (which is why we need to set its value to 1) ? $history{ $history[$i] }++ # autovivify and increment. (By using exists, the # setting to 1 is redundant but... : splice @history, $i--, 1); # the -- in this version would be bad!! # Thou shalt not modify the iterator in a for loop! } #### for ($#history .. 0) { my $temp = pop @history; if (!exists $history{ $temp }) { $history{ $temp }++; unshift @history; } # else nothing to do we've already removed it. } #### for ($#history .. 0) { my $temp = pop @history; next if exists $history{ $temp }; $history{ $temp } ++; unshift @history; } #### for ( push @history, '~~SOME UNLIKELY VALUE~~', my $value= shift @history; $value ne '~~SOME UNLIKELY VALUE~~'; $value = shift @history ) { push @history, $value, $history{ $value }++ unless exists $history{ $value } } #### use constant SENTINEL => '~~SOME UNLIKELY VALUE~~'; for ( push @history, SENTINEL, my $value= shift @history; $value ne SENTINEL; $value = shift @history ) { push @history, $value, $history{ $value }++ unless exists $history{ $value } } #### my %history; .... # get a new value here and only add it if its not there. $history{ $newvalue } = 1 unless exists $history{ $newvalue }; ... #### my (%history, $histcount); .... # get a new value here and only add it if its not there. $history{ $newvalue } = $histcount++ unless exists $history{ $newvalue }; #### $history{ $newvalue } = $histcount++; ... #### for my $i (keys %history) { last if $history{ $i } == ($histcount - $n); } my $last_nth = $history{ $i }; #### # Can't use multiple modifiers. Basic Plus2 was nice! my last_nth = do { $_ for (keys %history) } while ( $history{ $_ } == ($histcount - $n) ); my $last_nth = $_ if $history{ $_ } == n for (keys %history); # Again $_ is local #### my $last_nth; for my $i (keys %history) { last if ($last_nth = $history{ $i }) == ($histcount - $n); } #### my last_nth = grep{ $history{ $_ } == ($histcount - $n } keys %history; #### my ($last_nth) = grep{ $history{ $_ } == ($histcount - $n); } keys %history; #### my (%history, $histcount); # To add $newvalue $history{ $newvalue } = $histcount++ unless exists $history{ $newvalue }; # To retrieve the last-but-$n'th my ($last_nth) = grep{ $history{ $_ } == ($histcount - $n); } keys %history; #### my (%history, $histcount); # Should I initialise $histcount = 0 or is that "Cargo cult"? sub addHist { $history{ $_[0] } = $histcount++; } sub getHist { return = grep{ $history{ $_ } == ($histcount - $_[0]); } keys %history; } # then ... # get $newvalue from somewhere addHist( $newvalue ); ... # deside we want the $n'th previous value my $last_nth = getHist( $n ); ... #### my (%history, $histcount); # Should I initialise $histcount = 0 or is that "Cargo cult"? sub addHist { my ($hashref, $histcount, $new ) = @_; # Naming our parameters makes things clearer # validate $hashref->{ $new } = $histcount++; } sub getHist { my ($hashref, $histcount, $new ) = @_; #validate return = grep{ $hashref->{ $_ } == ($histcount - $n); } keys %history; } #### ... # get $newvalue from somewhere addHist( \%history, $histcount, $newvalue ); ... # deside we want the $n'th previous value my $last_nth = getHist( \%history, $histcount, $n ); ... #### { my (%history, $histcount); # Should I initialise $histcount = 0 or is that "Cargo cult"? sub addHist { $history{ $_[0] } = $histcount++; } sub getHist { return = grep{ $history{ $_ } == ($histcount - $_[0]); } keys %history; } } #### ... # get $newvalue from somewhere addHist( $newvalue ); ... # deside we want the $n'th previous value my $last_nth = getHist( $n ); ...