Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: iterating 2 arrays in parallel

by linuxer (Deacon)
on Apr 15, 2009 at 10:37 UTC ( #757627=note: print w/ replies, xml ) Need Help??


in reply to Re^2: iterating 2 arrays in parallel
in thread iterating 2 arrays in parallel

The given example uses arrays of the same length, so pairwise looks fine to me.

And if they are not of the same length, I think the same way as JavaFan, that iswas left as an exercise to the reader. I don'tdidn't want to guess, what has to happen if the lengths differ. It's the OP who should know, what to do.

Nevertheless, thanks for your examples ;o)

Update:

What about this pairwise solution to work with arrays of different lengths:

use List::MoreUtils qw( pairwise ); my @a=(1 .. 5 ); my @b=( 'a' .. 'c' ); ### Good point from JavaFan; bad idea to ignore 0 ### my @combined = pairwise { ( $a || '' ) .':'. ( $b || '' ) } @a, @b +; ### fixed: my @combined = pairwise { ( defined $a ? $a : '' ) .':'. ( defined $b +? $b : '' ) } @a, @b; { local $,="\n", print @combined; }


Comment on Re^3: iterating 2 arrays in parallel
Select or Download Code
Re^4: iterating 2 arrays in parallel
by JavaFan (Canon) on Apr 15, 2009 at 10:55 UTC
    I think it's a bad idea to replace a 0 with an empty string as your solution does. Either use defined, '//', or just turn off warnings.

      Damn, right. I feared I missed something ;o). Fixed code and used defined(). Thank you.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-07-28 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls