Does foreach operator iterate in order, or maybe randomly?

I was wondering if the foreach statement in Perl iterates the elements in an array in sequential order? That is, I get different results if I use foreach several times in the same array or list?

+5
source share
3 answers

Yes, the elements in the statement are foreachrepeated in order.

Your question may arise due to confusion over the iteration of the hash elements:

my %hash = ('a' => 1, 'b' => 2, 'c' => 3);
foreach my $key (keys %hash) { print $key } ;    # output is "cab"

, - Perl ( - Perl ). keys, "" -, foreach.

+9

, .

+1

The reason why the Perl foreach hash order seems to be random is the perlsec document linked here

0
source

All Articles