in reply to How a for() assignment works

for my $var (map lc, grep exists $hash_ref->{lc $_}, @vars) { print "Value: " . $hash_ref->{$var} . "\n" ; }
or even
for my $var (map $hash_ref->{lc $_}, grep exists $hash_ref->{lc $_}, @ +vars) { print "Value: " . $var . "\n" ; }
finally leading to
print "Value: " . $_ . "\n" for map $hash_ref->{lc $_}, grep exists $hash_ref->{lc $_}, @vars;
but if we swap the map and grep we can eliminate the duplicate lc
print "Value: " . $hash_ref->{$_} . "\n" for grep exists $hash_ref->{$_}, map lc, @vars;
Of course now we have three loops running :-(

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

    Or you could always do it like this:

    print map exists $hash_ref->{lc()} ? "Value: $hash_ref->{lc()}\n" : '' +, @vars;