http://qs1969.pair.com?node_id=445554

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I have defined a hash and assigned values to it as follows:

my(%sortListArr); $sortListArr{'1'} = 'A1'; $sortListArr{'2'} = 'A2'; $sortListArr{'3'} = 'A3'; $sortListArr{'4'} = 'A4'; $sortListArr{'5'} = 'A5'; $sortListArr{'6'} = 'A6';

Then when I do a:

foreach $key (keys %sortListArr){ print $sortListArr{$key},"\n"; }

The o/p is A1, A2....A6. However upon adding a 7th value:

$sortListArr{'7'} = 'A7';

The o/p becomes A7, A1, A2, A3...A6. Any explanations to this mystery? The problem disappears if I assign the values as:

$sortListArr{'0'} = 'A1'; $sortListArr{'1'} = 'A2'; $sortListArr{'2'} = 'A3'; $sortListArr{'3'} = 'A4'; $sortListArr{'4'} = 'A5'; $sortListArr{'5'} = 'A6'; $sortListArr{'6'} = 'A7';

The o/p is then A1, A2.....A7.

Tamojit

20050407 Janitored by Corion: Added formatting