Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How do I multiply the corresponding elements in two arrays together?

by Fang (Pilgrim)
on Sep 09, 2005 at 21:00 UTC ( [id://490753]=note: print w/replies, xml ) Need Help??


in reply to How do I multiply the corresponding elements in two arrays together?

You could use List::MoreUtils:

use List::MoreUtils qw/pairwise/; @a = (1..5); @b = (6..10); @x = pairwise { $a * $b } @a, @b; print join ", ", @x; __END__ 6, 14, 24, 36, 50

Be sure to check both array have the same number of elements, or you could get a trail of zeroes:

use List::MoreUtils qw/pairwise/; @a = (1..5); @b = (6..8); @x = pairwise { $a * $b } @a, @b; print join ", ", @x; __END__ 6, 14, 24, 0, 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found