in reply to How a for() assignment works
or evenfor my $var (map lc, grep exists $hash_ref->{lc $_}, @vars) { print "Value: " . $hash_ref->{$var} . "\n" ; }
finally leading tofor my $var (map $hash_ref->{lc $_}, grep exists $hash_ref->{lc $_}, @ +vars) { print "Value: " . $var . "\n" ; }
but if we swap the map and grep we can eliminate the duplicate lcprint "Value: " . $_ . "\n" for map $hash_ref->{lc $_}, grep exists $hash_ref->{lc $_}, @vars;
Of course now we have three loops running :-(print "Value: " . $hash_ref->{$_} . "\n" for grep exists $hash_ref->{$_}, map lc, @vars;
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How a for() assignment works
by jwkrahn (Abbot) on Mar 07, 2008 at 13:24 UTC |