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

I have two arrays and i need to subtract as below and assign the value to array

@array1 is below 1 3459 2 4568 3 5494 4 3489 @array2 is below 1 3459 2 4576 3 5498 4 3489

i need to subtract first row second column from array1 with first row second column from array2 and goes on for all the rows assign the difference to a variable . example : 3459-3459 and 4568-4576, 5494-5498,3489-3489

Replies are listed 'Best First'.
Re: subtract two columns from perl arrays
by Athanasius (Archbishop) on Nov 12, 2013 at 03:25 UTC

    Hello agreen54,

    As others have said, we won’t write the code for you (PerlMonks is a teaching site, not a code-writing service). But I will give you two hints on possible ways to approach this task:

    1. Use a C-style for loop to iterate over the indices of the two arrays. See perlsyn#For-Loops. Update: GrandFather (below) is of course correct, prefer a Perl-style for loop! (What was I thinking?) See perlsyn#Foreach-Loops.

    2. Look at the pairwise function in the module List::MoreUtils.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      A Perl style for loop to iterate over the indices is clearer and more Perlish (whatever that means) ;).

      True laziness is hard work
        ...and within that "Perl style" loop, insert the "pseudocode" you wrote above:
        "i need to subtract first row second column from array1 with first row second column from array2 and goes on for all the rows assign the difference to a variable"
        Two arrays, subtraction operation, assigning the difference to a variable. I'd say you are more than halfway towards a solution. :)
Re: subtract two columns from perl arrays
by GrandFather (Saint) on Nov 12, 2013 at 02:16 UTC

    Ok, so what have you tried? How can we help you fix code you haven't shown us?

    True laziness is hard work
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: subtract two columns from perl arrays
by soonix (Chancellor) on Nov 12, 2013 at 08:15 UTC
    If you have @arrays, your problem may simply be wrong terminology. What you are seeing as two columns, probably are indices and values.
    You could try
    use strict; use warnings; use Data::Dumper; # your code for creating @array1 and @array2 goes here print Dumper(\@array1); print Dumper(\@array2);
    and show us the output.