Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Processing pairs of values from even-sized array

by Bloodnok (Vicar)
on Jun 03, 2011 at 12:32 UTC ( [id://907964]=note: print w/replies, xml ) Need Help??


in reply to Processing pairs of values from even-sized array

Hi Ronald ,

Have you tried using each itself. as in something like ...

my $array = { @array }; while (my ($k, $v) = each %$array) { . . }
Or possibly simpler still, but I haven't yet worked out how to perform an in-line cast of a list to an array - while (my ($k, $v) = each %{@array}) {... generates errors coz the list is being evaluated in scalar, not list, context ... JIC you were wondering:-)

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: Processing pairs of values from even-sized array
by AnomalousMonk (Archbishop) on Jun 03, 2011 at 12:51 UTC
    my $array = { @array };

    This will 'randomize' (or 'hash-ize'?) the order of elements in the array, and  %{{@array}} likewise. Update: Oops:  each %{{@arr}} has another, entirely different, fatal (or should I say immortal?) flaw!

      I suppose that depends on whether the pairs have to pulled from the list in the same order in which they appear in the list ... which altho' implied, isn't, AFAICT, specified in/by the OP.

      Ooops, my mistake, it _is_ specified : 'This array should be processed left-to-right...'

      A user level that continues to overstate my experience :-))
Re^2: Processing pairs of values from even-sized array
by rovf (Priest) on Jun 03, 2011 at 13:43 UTC
    Have you tried using each itself.
    This doesn't work, because this would require $array being a hash reference.

    -- 
    Ronald Fischer <ynnor@mm.st>

      Interestingly, the 5.12+ each allows iteration over an array (and similarly for keys and values), but each 'key' is a succcessive index of the array, and each 'value' is the element at that index: not quite what you wanted in the OP.

      >perl -wMstrict -le "my @ra = qw(a b c d); ;; while (my ($i, $element) = each @ra) { print qq{ra $i is '$element'}; } " ra 0 is 'a' ra 1 is 'b' ra 2 is 'c' ra 3 is 'd'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-24 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found