http://www.perlmonks.org?node_id=1016292


in reply to multiple local vars in a foreach loop

shawnkielty:

Update: LanX is (of course) correct.

Not with foreach, but you can do it using while and each:

my %hash; while (my ($key, $value) = each %hash) { ... }

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: multiple local vars in a foreach loop
by LanX (Saint) on Jan 31, 2013 at 11:28 UTC
    Right answer to wrong question! ;-)

    DB<156> while (my ($key, $value) = each @a) { print "$key, $value" } +;; Type of arg 1 to each must be hash (not array dereference) at (eval 13 +5)[multi_perl5db.pl:644] line 2, near "@a) "

    Cheers Rolf

      Depends on the version of Perl: each.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Unfortunately not! :(

        This doesn't help splicing multiple loop vars...

        each:

        it will also return the index and value for the next element of an array so that you can iterate over it;

        Wrong answer wrong question! ;-)

        Cheers Rolf