foreach with two arrays

T

Thomas P.

Hello,

I have two arrays and I want to use the foreach loop with both arrays
simultaneously, that means the loop should take one value from both arrays
at one run.
I tried something like this but it does not work :
@a=("12","14");
@b=("7","9");
foreach $value1 $value2 (@a,@b)

Regards
Thomas
 
P

Paul Lalli

Thomas said:
I have two arrays and I want to use the foreach loop with both arrays
simultaneously, that means the loop should take one value from both arrays
at one run.
I tried something like this but it does not work :
@a=("12","14");
@b=("7","9");
foreach $value1 $value2 (@a,@b)

I do not believe you're going to find the functionality you're looking
for in any built-in construct. But doing it manually really isn't that
hard.

#assumes @a and @b have same size.
for my $i (0..$#a){
my ($val1, $val2) = ($a[$i], $b[$i]);
#...
}

Another option might be to build a hash from the two arrays, and use
the each operator:

#assumes @a contains only unique values
#also assumes you don't care about the order or the elements,
#only their relations
my %hash;
@hash{@a} = @b
while (my ($val1, $val2) = each %hash){
#...
}

Paul Lalli
 
T

Thomas P.

I just wanted to find out if this option is included in the foreach loop.
Thanks for the example.


Paul Lalli said:
Thomas said:
I have two arrays and I want to use the foreach loop with both arrays
simultaneously, that means the loop should take one value from both arrays
at one run.
I tried something like this but it does not work :
@a=("12","14");
@b=("7","9");
foreach $value1 $value2 (@a,@b)

I do not believe you're going to find the functionality you're looking
for in any built-in construct. But doing it manually really isn't that
hard.

#assumes @a and @b have same size.
for my $i (0..$#a){
my ($val1, $val2) = ($a[$i], $b[$i]);
#...
}

Another option might be to build a hash from the two arrays, and use
the each operator:

#assumes @a contains only unique values
#also assumes you don't care about the order or the elements,
#only their relations
my %hash;
@hash{@a} = @b
while (my ($val1, $val2) = each %hash){
#...
}

Paul Lalli
 
F

Fabian Pilkowski

* Paul Lalli said:
I do not believe you're going to find the functionality you're looking
for in any built-in construct.

You're right. But each time you search for a special functionality not
provided by Perl itself you could ask CPAN for helpful solutions. Some
functions for processing lists are summarized in the modules List::Util
and List::MoreUtils. To do something as described above you could use
one of List::MoreUtils' functions: either pairwise() or each_array(),
depends on what you really want to do.
But doing it manually really isn't that hard.

Yes, it's not hard to implement this yourself, but I prefer not to
reinvent the wheel. That's one reason I like Perl ;-)

regards,
fabian
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top