Help for this page

Select Code to Download


  1. 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);
      }
    
  2. 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 );
    ...
    
  3. 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;
    }
    
  4. 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!
    }
    
  5. or download this
    for ($#history .. 0) {
        my $temp = pop @history;
    ...
            unshift @history;
        } # else nothing to do we've already removed it.
    }
    
  6. or download this
    for ($#history .. 0) {
        my $temp = pop @history;
    ...
        $history{ $temp } ++;
        unshift @history;
    }
    
  7. 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 }
    }
    
  8. or download this
    use constant SENTINEL => '~~SOME UNLIKELY VALUE~~';
    
    ...
    {
        push @history, $value, $history{ $value }++ unless exists $history
    +{ $value }
    }
    
  9. 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 };
    ...
    
  10. 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
    + };
    
  11. or download this
    $history{ $newvalue } = $histcount++;  
    ...
    
  12. or download this
    for my $i  (keys %history) {
        last if $history{ $i } == ($histcount - $n);         
    }
    my $last_nth = $history{ $i };
    
  13. 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
    
  14. or download this
    my $last_nth;
    for my $i  (keys %history) {
        last if ($last_nth = $history{ $i }) == ($histcount - $n); 
    }
    
  15. or download this
    my last_nth = grep{ $history{ $_ } == ($histcount - $n } keys %history
    +;
    
  16. or download this
    my ($last_nth) = grep{ $history{ $_ } == ($histcount - $n); } keys %hi
    +story;
    
  17. or download this
    my (%history, $histcount);
    
    ...
    
    # To retrieve the last-but-$n'th 
    my ($last_nth) = grep{ $history{ $_ } == ($histcount - $n); } keys %hi
    +story;
    
  18. 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 );
    ...
    
  19. 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; 
    }
    
  20. 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 );
    ...
    
  21. 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; }
    }
    
  22. or download this
    ... # get $newvalue from somewhere
    addHist( $newvalue );
    ...
    ... # deside we want the $n'th previous value
    my $last_nth = getHist( $n );
    ...