in reply to Printing hash

The code:

foreach my $key (sort keys %hash)

creates an anonymous array of the sorted keys in the hash %hash. The foreach is a loop construct, each iteration thru the loop sets $key to the next item in the anonymous array until the end of the array, when the loop ends.

Scott

Replies are listed 'Best First'.
Re^2: Printing hash
by bobf (Monsignor) on Feb 13, 2006 at 02:37 UTC
      Quote:Secondly, $key is an alias to each value in the list, which means modifying $key will modify the original value :EndQuote

      It will change the value in the list returned by keys and sort, but not in the original hash, e.g.

      my %hash = (1 => 'a', 2 => 'b', 3 => 'c'); foreach my $value (sort keys %hash ) { $value--; } print join( ', ', sort keys %hash ), "\n"; # 1, 2, 3
      -imran