> Also note that a foreach loop doesn?t have a guaranteed order.
Er, for an array, a foreach loop will go from front to back (or back to
front if you 'reverse' it). Hashes or hash keys will come out in a
non-determinable (or close enough) order but arrays darn well better come
out in order or something's wrong.
my @list = qw( apple banana pear grapefruit);
print "Will be: ", join(", ", @list), "\n";
foreach my $fruit ( @list ) {
print "$fruit\n";
}
my %at_where = map { $_ => $cnt++; } @list;
print "Probably won't be: ", join(", ", @list), "\n";
foreach my $key ( keys %at_where ) {
print "$key ($at_where{$key})\n";
}
Will be: apple, banana, pear, grapefruit
apple
banana
pear
grapefruit
Probably won't be: apple, banana, pear, grapefruit
grapefruit (3)
banana (1)
apple (0)
pear (2)
The hash may come out consistently, run to run, in that order but it won't
be the order you put them into the hash ... probably. And adding a new
element will probably change the order (added 'kiwi' to the array):
Will be: apple, banana, pear, grapefruit, kiwi
apple
banana
pear
grapefruit
kiwi
Probably won't be: apple, banana, pear, grapefruit, kiwi
grapefruit (3)
banana (1)
apple (0)
kiwi (4)
pear (2)
a
Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738 FAX 264-5932
Wire telegraph is a kind of a very, very long cat. You pull his tail in
New York and his head is meowing in Los Angeles. And radio operates
exactly the same way. The only difference is that there is no cat.
--Albert Einstein (explaining radio)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs