Gerard Robin wrote:
Hello,

Hello,

with perl 5.8 with this script:

#!/usr/bin/perl
#hash2.pl

use warnings;
use strict;

my %where=(
           London => "England",
           Madrid => "Spain",
          );

print "-"x20, "\n";

my ($a, $b) = each %where;
my ($c, $d) = each %where;

print "$a   $b\n";
print "$c   $d\n";

print "-"x20, "\n";

# my @ou = %where;

$where{Paris} = "France";

while (my ($x, $y) = each %where) {

print "$x => $y\n";
}
print "-"x20, "\n";

perldoc -f each

        When the hash is entirely read, a null array is returned in list
        context (which when assigned produces a false (0) value), and "undef"
        in scalar context.  The next call to "each" after that will start
        iterating again.  There is a single iterator for each hash, shared by
        all "each", "keys", and "values" function calls in the program; it can
        be reset by reading all the elements from the hash, or by evaluating
        "keys HASH" or "values HASH".  If you add or delete elements of a hash
        while you're iterating over it, you may get entries skipped or
        duplicated, so don't.



You have two keys in the hash and you are calling each() twice which does not
reset the iterator.  You need to reset the iterator by either calling each()
until it returns nothing (undef) or by calling keys() or values().  So instead
of:

my @ou = %where;

you should do:

keys %where;



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


  • Re: each John W. Krahn

Reply via email to