Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Folks,

Evaluates for each elements in @one and @two and return its value to @multi;

@one=('2', '3', '4', '5', '10', '100'); @two=('6', '2', '3', '7', '2', '7');

@multi array would have:

@multi=('12', '6', '12', '35', '20', '700');

Thank for Your Help!!.

Sow

Replies are listed 'Best First'.
Re: Evaluates two array index
by gopalr (Priest) on Apr 06, 2005 at 11:15 UTC

    Hello,

    use List::MoreUtils Module.

    use List::MoreUtils qw(:all); @one=('2', '3', '4', '5', '10', '100'); @two=('6', '2', '3', '7', '2', '7'); @multi=pairwise{$a*=$b} @one,@two; print "\n@multi\n";

    Output

    12 6 12 35 20 700

    Thanks,

    Gopalr

Re: Evaluates two array index
by sh1tn (Priest) on Apr 06, 2005 at 11:14 UTC
    @one=('2', '3', '4', '5', '10', '100'); @two=('6', '2', '3', '7', '2', '7'); @multi = map{ $one[$_] * $two[$_] }0..$#one;


Re: Evaluates two array index
by Nevtlathiel (Friar) on Apr 06, 2005 at 11:14 UTC
    Try using $i as an element counter to multiply the corresponding elements of each of the first two arrays together and then push onto the results array, where are you having problems?
Re: Evaluates two array index
by tlm (Prior) on Apr 06, 2005 at 11:17 UTC
    push @multi, shift( @one )*shift( @two ) while @one && @two;

    the lowliest monk

      shift is unsuitable unless you want empty arrays.


Re: Evaluates two array index
by cog (Parson) on Apr 06, 2005 at 11:26 UTC
    Actually, cristian just posted an obfu that uses something very, very similar to what you want :-)

    You might learn something from looking at his code (it's obfu-ed, but not so much that you won't understand it).

Re: Evaluates two array index
by Forsaken (Friar) on Apr 07, 2005 at 07:16 UTC
    assuming for argument's sake that it is 100% certain that @one and @two contain an identical number of entries and that all of them are numerical...
    my $i = 0; while($one[$i]) { $multi[$i] = $one[$i] * $two[$i]; $i += 1; }
    simple, readable, and it should do the trick :-)