Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: why doesn't "my ($a,$b)" return a list?

by kejohm (Hermit)
on Aug 20, 2010 at 12:17 UTC ( [id://856253]=note: print w/replies, xml ) Need Help??


in reply to why doesn't "my ($a,$b)" return a list?

On a side note, as of v5.12, the each(), keys(), and values() functions now operate on arrays, which might be what you are looking for. Here is an example:

#!perl use strict; use warnings; use feature qw(:5.12); my @array = 'a' .. 'g'; while( my( $index, $value ) = each @array ) { say "$index -> $value"; } __END__ 0 -> a 1 -> b 2 -> c 3 -> d 4 -> e 5 -> f 6 -> g

Replies are listed 'Best First'.
Re^2: why doesn't "my ($a,$b)" return a list?
by LanX (Saint) on Aug 20, 2010 at 12:39 UTC
    Yes indeed!!!

    Excellent, thx for showing :)

    (I was already wondering why it's not implemented this way)

    BUT just recently I got bitten by trying to nest two while (each ) on the same hash, because the iteration-pointer is tied to the hash... :(

    Cheers Rolf

      just recently I got bitten by trying to nest two while (each ) on the same hash, because the iteration-pointer is tied to the hash
      If you do not change the hash in the loop, you can use your own iterator like that:
      sub hash_iterator { my %h = @_; return sub { each %h; } } # make_iterator my %test = (k1 => 'v1', k2 => 'v2', k3 => 'v3', 'k-last' => 'v-last'); my $it1 = hash_iterator(%test); my $it2 = hash_iterator(%test); while(<>){ print join ':',($it1->())[0,1]; print ';',join ':',($it2->())[0,1]; print ' - ',join ':',($it2->())[0,1]; print "\n"; }
        sure, but thats why I was writing my own enumerator function.

        by supplying the variables as arguments I can naturally bind the iterator to them without the need to generate extra interators.

         while( enum @a => state($value,$index) ) {...}

        Actually an extra option for "each" to work like flip-flop would be the best solution.

        Cheers Rolf

Re^2: why doesn't "my ($a,$b)" return a list?
by LanX (Saint) on Aug 20, 2010 at 21:38 UTC
    > keys(), and values() functions now operate on arrays,

    are you sure? I was only able to find each in perldelta ...

    can't test it myself!

    Cheers Rolf

      Yes, it is mentioned in the perlfunc manpage under keys and values. Strange that is not in perldelta.

      Update: Link fixed.

        Thx!

        the necessity use feature qw(:5.12); isn't documented either.

        see feature

        could you plz check if it works without it?

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://856253]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-19 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found