Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: multiple local vars in a foreach loop

by roboticus (Chancellor)
on Jan 31, 2013 at 11:21 UTC ( #1016292=note: print w/replies, xml ) Need Help??


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 (Bishop) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016292]
help
Chatterbox?
[virtualsue]: I welcome any talktaive people who do not mind using a propietary walled-garden service for free
[virtualsue]: or even proprietary
[virtualsue]: we talk about programming and music and stuff
herveus waves to virtualsue

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2017-09-26 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (296 votes). Check out past polls.

    Notices?