cgmd has asked for the wisdom of the Perl Monks concerning the following question:
I came across a piece of code, which was provided to demonstrate a limitation when using the each() function:
#!/usr/bin/perl %couples = ( george => gracie, abbot => costello, johnson => boswell ); foreach $c (each %couples) { print "$c\n"; };
The captioned text accompanying this code reads:
"It's important to rember with each() that it will give you only the next key/value pair from the named hash. The output should contain two lines of text, one for george, one for gracie."
In fact, this code produces for me, on my unix system, the following output:
abbot costello
My first question is whether the information in the text regarding the expected code output is erroneous or is it, perhaps, a cross-platform issue, since this was originally DOS code?
My other question is, what is the minimum code that would, indeed print out the names of all three couples and overcome the supposed limitation of the each() function?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: code that fails to act as expected...
by varian (Chaplain) on Jun 13, 2007 at 11:47 UTC | |
|
Re: code that fails to act as expected...
by citromatik (Curate) on Jun 13, 2007 at 11:48 UTC | |
by cgmd (Beadle) on Jun 15, 2007 at 11:08 UTC | |
|
Re: code that fails to act as expected...
by derby (Abbot) on Jun 13, 2007 at 11:59 UTC | |
|
Re: code that fails to act as expected...
by Moron (Curate) on Jun 13, 2007 at 12:17 UTC |