two arrays

U

usaims

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
 
T

Tassilo v. Parseval

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
 
T

Tad McClellan

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.
 
M

Mark Clements

Purl said:
axel wrote:

(snipped)




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
 
T

Tad McClellan

Mark Clements said:
Purl Gurl wrote:

scalar length?


It sounds kewl!

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

Tassilo v. Parseval

Also sprach Tad McClellan:
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.
... 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top