in reply to Using foreach to process a hash

foreach ($value, $key = each %texthash) {

Where in the documentation did you see an example like that?

You seem to have some vague idea of how each works, but your syntax is so far off, I'd have to suppose that you haven't read the man page for each, because if you had, you'd have seen the following example:

while (($key, $value) = each %hash) {

You got three things wrong:

  1. use while, not foreach
  2. the pair returned by each is $key,$value not $value,$key
  3. you need parentheses around ($key,$value)
We're building the house of the future together.

Replies are listed 'Best First'.
Re^2: Using foreach to process a hash
by blazar (Canon) on Oct 22, 2006 at 12:42 UTC
    while (($key, $value) = each %hash) {

    You got three things wrong:

    1. use while, not foreach
    2. the pair returned by each is $key,$value not $value,$key
    3. you need parentheses around ($key,$value)

    But of course, if he wants to use foreach, then for completeness it's fair to remind that he can do so, but in connection with keys, not each:

    foreach my $key (keys %hash) { my $value=$hash{$key};

    Update: hadn't noticed it already been covered by imp's reply. Hopefully repetita iuvant

      Yes, but that had already been covered by imp's reply.