in reply to code that fails to act as expected...
foreach $c (each %couples) { print "$c\n"; };
each %couple returns, in list context, the next key/value pair. In this case (bacause the %hash is stored in "semi-random" order), returns the pair (abbot,costello). You can visualize this as:
foreach $c ("abbot","costello") { print "$c\n"; };
The foreach prints then "abbot" and "costello".
The expected output can be obtained in list context, for example:
while (my ($c,$d) = each %couples){ print "$c $d\n"; }
The shorter way I could imagine to do this is:
print "$_ $couples{$_}$/" for (keys %couples);
Hope this helps!
citromatik
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: code that fails to act as expected...
by cgmd (Beadle) on Jun 15, 2007 at 11:08 UTC |