- or download this
# 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);
}
- or download this
{
my (%history, $histcount); # Should I initialise $histcount = 0 or
+ is that "Cargo cult"?
...
... # deside we want the $n'th previous value
my $last_nth = getHist( $n );
...
- or download this
for ( my $i=$#history; $i >=0; $i-- ) { # for $i = (last index of @hi
+story) to 0
$_ = $history[$i]; # get the value from the posi
+tion in @history
...
: splice @history, $i--, 1) # if it has remove it from @h
+istory
# (reducing $i to compensate
+for its remove;
}
- or download this
for my $i ($#history .. 0 ) {
!exists $history{ history[$i] } # use exists as testing its v
+alue against zero will
...
: splice @history, $i--, 1); # the -- in this version woul
+d be bad!!
# Thou shalt not modify the i
+terator in a for loop!
}
- or download this
for ($#history .. 0) {
my $temp = pop @history;
...
unshift @history;
} # else nothing to do we've already removed it.
}
- or download this
for ($#history .. 0) {
my $temp = pop @history;
...
$history{ $temp } ++;
unshift @history;
}
- or download this
for ( push @history, '~~SOME UNLIKELY VALUE~~', my $value= shift @hist
+ory;
$value ne '~~SOME UNLIKELY VALUE~~';
...
{
push @history, $value, $history{ $value }++ unless exists $history
+{ $value }
}
- or download this
use constant SENTINEL => '~~SOME UNLIKELY VALUE~~';
...
{
push @history, $value, $history{ $value }++ unless exists $history
+{ $value }
}
- or download this
my %history;
....
# get a new value here and only add it if its not there.
$history{ $newvalue } = 1 unless exists $history{ $newvalue };
...
- or download this
my (%history, $histcount);
....
# get a new value here and only add it if its not there.
$history{ $newvalue } = $histcount++ unless exists $history{ $newvalue
+ };
- or download this
$history{ $newvalue } = $histcount++;
...
- or download this
for my $i (keys %history) {
last if $history{ $i } == ($histcount - $n);
}
my $last_nth = $history{ $i };
- or download this
# 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
- or download this
my $last_nth;
for my $i (keys %history) {
last if ($last_nth = $history{ $i }) == ($histcount - $n);
}
- or download this
my last_nth = grep{ $history{ $_ } == ($histcount - $n } keys %history
+;
- or download this
my ($last_nth) = grep{ $history{ $_ } == ($histcount - $n); } keys %hi
+story;
- or download this
my (%history, $histcount);
...
# To retrieve the last-but-$n'th
my ($last_nth) = grep{ $history{ $_ } == ($histcount - $n); } keys %hi
+story;
- or download this
my (%history, $histcount); # Should I initialise $histcount = 0 or
+ is that "Cargo cult"?
...
... # deside we want the $n'th previous value
my $last_nth = getHist( $n );
...
- or download this
my (%history, $histcount); # Should I initialise $histcount = 0 or
+ is that "Cargo cult"?
...
#validate
return = grep{ $hashref->{ $_ } == ($histcount - $n); } keys %hist
+ory;
}
- or download this
... # get $newvalue from somewhere
addHist( \%history, $histcount, $newvalue );
...
... # deside we want the $n'th previous value
my $last_nth = getHist( \%history, $histcount, $n );
...
- or download this
{
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; }
}
- or download this
... # get $newvalue from somewhere
addHist( $newvalue );
...
... # deside we want the $n'th previous value
my $last_nth = getHist( $n );
...