Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 (Chancellor) 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?
[choroba]: I usually do this with presentations
[Corion]: But now I think statically (re)generating the Pod tests is a saner approach, and likely I'll regenerate the tests either in Makefile.PL or from xt/ but have them live below t/
[choroba]: I keep the snippets in files of their own, and use a Makefile to syntax highlight them and insert them into slides, while also running them and inserting the output if required
[Corion]: choroba: Ooooh - I didn't think of that! I write my presentations as POD and if it "roughly" looks like Perl code, I should also syntax-check that...
[haukex]: Yes sorry I don't run them all the time, my POD tests are only run as author tests (and are excluded when I'm using Devel::Cover)
[Corion]: choroba: Hmm - no, I keep the snippets inline, but as my framework also has support for capturing output etc., maybe I should do the same...
[Corion]: haukex: Yes, that approach is sane, and it heals the fragility of Pod parsers in a nice way while still syntax-checking stuff
[choroba]: Unfortunately, none of it is online
[haukex]: I figured that POD tests make sense, but only as author tests
[choroba]: I mean, the slides are, but not the makefile with scripts to create them

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-02-27 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (385 votes). Check out past polls.