two arrays

Discussion in 'Perl Misc' started by usaims, Nov 21, 2005.

  1. usaims

    usaims Guest

    Hello:

    I'm trying to write code in a 'Perlish' way. I want the sum of the same
    element of two arrays. Everytime I want to run this code, it will have
    different results in the arrays, so I want to simply the code to
    accomondate that. Below is what I want to see in the long un-perlish
    way, I'm sure there is an easier way to write this. Please excuse my
    inexperience in coding, also I looked in the camel book and the
    cookbook, didn't see anything related. TIA


    #!/usr/bin/perl
    use strict;
    use warnings;
    use diagnostics;

    my @array = (1, 2, 3 );
    my @testarray = (2, 3, 4);


    my $test1 = $array[0] + $testarray[0];
    my $test2 = $array[1] + $testarray[1];
    my $test3 = $array[2] + $testarray[2];
    print "$test1\n";
    print "$test2\n";
    print $test3;

    usaims
     
    usaims, Nov 21, 2005
    #1
    1. Advertising

  2. Also sprach usaims:

    > I'm trying to write code in a 'Perlish' way. I want the sum of the same
    > element of two arrays. Everytime I want to run this code, it will have
    > different results in the arrays, so I want to simply the code to
    > accomondate that. Below is what I want to see in the long un-perlish
    > way, I'm sure there is an easier way to write this. Please excuse my
    > inexperience in coding, also I looked in the camel book and the
    > cookbook, didn't see anything related. TIA
    >
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > use diagnostics;
    >
    > my @array = (1, 2, 3 );
    > my @testarray = (2, 3, 4);
    >
    >
    > my $test1 = $array[0] + $testarray[0];
    > my $test2 = $array[1] + $testarray[1];
    > my $test3 = $array[2] + $testarray[2];
    > print "$test1\n";
    > print "$test2\n";
    > print $test3;


    You can use List::MoreUtils::pairwise for that. Summing up elements of
    two arrays pairwise is even one of the example in its documentation:

    use List::MoreUtils qw/pairwise/;

    my @summed = pairwise { $a + $b } @array, @testarray;
    print join "\n", @summed;

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Nov 21, 2005
    #2
    1. Advertising

  3. usaims

    Guest

    Bernard El-Hagin <> wrote:
    > "usaims" <> wrote:


    >> #!/usr/bin/perl
    >> use strict;
    >> use warnings;
    >> use diagnostics;


    >> my @array = (1, 2, 3 );
    >> my @testarray = (2, 3, 4);


    > All of the following...


    >> my $test1 = $array[0] + $testarray[0];
    >> my $test2 = $array[1] + $testarray[1];
    >> my $test3 = $array[2] + $testarray[2];
    >> print "$test1\n";
    >> print "$test2\n";
    >> print $test3;


    > ...can be rewritten like this:



    > for (0 .. 2) {

    ^^^^^
    |||||

    should be:

    for (0 .. $#array)

    Always avoiding hard-wiring such details :)

    > print $array[$_] + $testarray[$_], "\n";
    > }


    Axel
    >
     
    , Nov 21, 2005
    #3
  4. <> wrote:
    > Bernard El-Hagin <> wrote:
    >> "usaims" <> wrote:

    >
    >>> #!/usr/bin/perl
    >>> use strict;
    >>> use warnings;
    >>> use diagnostics;

    >
    >>> my @array = (1, 2, 3 );
    >>> my @testarray = (2, 3, 4);



    >> for (0 .. 2) {

    > ^^^^^
    > |||||




    > should be:



    some defensive programming before we get to the for loop:

    die "arrays are not the same size" unless @array == @testarray;


    > for (0 .. $#array)
    >
    > Always avoiding hard-wiring such details :)



    .... and always include defending against your own mistakes.

    I have found that I am very good at taking opportunities to mess
    things up, so I try to offer myself less of such opportunities in
    the first place.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 21, 2005
    #4
  5. Purl Gurl wrote:
    > axel wrote:
    >
    > (snipped)
    >
    >>> for (0 .. 2) {

    >> ^^^^^

    >
    >> should be:

    >
    >> for (0 .. $#array)

    >
    >> Always avoiding hard-wiring such details :)

    >
    > Which should be the shortest scalar of all arrays tested,
    > for circumstances of arrays of unequal scalar length.


    scalar length? scalars don't have a length, unless you mean the string
    length:

    my $len = length ( $value );

    If you mean "the number of items in the array", why not say that, or at
    least use a convention eg "array length" that gives people half a chance
    of understanding what you are talking about.

    >
    > However, doing so presents a problem of decision logic.
    > Under circumstances of unequal scalar lengths, should
    > remainder elements of sets of equal scalar length arrays
    > be manipulated? Should a single array prove to be the
    > longest scalar length, should remainder elements in
    > that array be manipulated?


    This is a tortuous paragraph even by your normal standards. Do you mean:
    "how should we treat the case where the arrays are of different lengths?"

    >
    > @Array1 = (1..4);
    > @Array2 = (1..5);
    > @Array3 = (1..6);
    >


    Yeah: you're doing what with these, exactly?

    Have you come across the Plain English Campaign?

    Mark
     
    Mark Clements, Nov 21, 2005
    #5
  6. Mark Clements <> wrote:
    > Purl Gurl wrote:


    >> Which should be the shortest scalar of all arrays tested,
    >> for circumstances of arrays of unequal scalar length.

    >
    > scalar length?



    It sounds kewl!

    All technical-like and what not. She sounds more credible (to herself)
    when she throws in gratuitious jargon.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 21, 2005
    #6
  7. Also sprach Tad McClellan:
    > <> wrote:
    >> Bernard El-Hagin <> wrote:
    >>> "usaims" <> wrote:


    >>>> my @array = (1, 2, 3 );
    >>>> my @testarray = (2, 3, 4);

    >
    >
    >>> for (0 .. 2) {

    >> ^^^^^
    >> |||||

    >
    >
    >
    >> should be:

    >
    >
    > some defensive programming before we get to the for loop:
    >
    > die "arrays are not the same size" unless @array == @testarray;


    Without that additional guard, the shorter array will be padded with
    undefined values which could also be what the OP wants. This is what
    List::MoreUtils::pairwise does.

    >> for (0 .. $#array)
    >>
    >> Always avoiding hard-wiring such details :)

    >
    >
    > ... and always include defending against your own mistakes.
    >
    > I have found that I am very good at taking opportunities to mess
    > things up, so I try to offer myself less of such opportunities in
    > the first place.


    But that's why you have warnings turned on, isn't it? So with or without
    the above constraint, the programmer will receive a message from his
    program upon which he may take the appropriate action (which could be as
    simple as putting 'no warnings qw/uninitialized/' into the body of the
    for-loop).

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Nov 22, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    436
    Alf P. Steinbach
    Aug 18, 2003
  2. Philipp
    Replies:
    21
    Views:
    1,130
    Philipp
    Jan 20, 2009
  3. Kev Jackson
    Replies:
    2
    Views:
    113
  4. Joshua Muheim
    Replies:
    8
    Views:
    196
    Dejan Dimic
    Nov 23, 2007
  5. Allen Walker

    Merging two arrays -> array of arrays

    Allen Walker, May 21, 2010, in forum: Ruby
    Replies:
    6
    Views:
    166
    Jesús Gabriel y Galán
    May 21, 2010
Loading...

Share This Page