Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > foreach with two arrays

Reply
Thread Tools

foreach with two arrays

 
 
Thomas P.
Guest
Posts: n/a
 
      07-19-2005
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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-19-2005
Thomas P. wrote:
> 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

 
Reply With Quote
 
 
 
 
Thomas P.
Guest
Posts: n/a
 
      07-19-2005
I just wanted to find out if this option is included in the foreach loop.
Thanks for the example.


"Paul Lalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Thomas P. wrote:
> > 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
>



 
Reply With Quote
 
Fabian Pilkowski
Guest
Posts: n/a
 
      07-19-2005
* Paul Lalli schrieb:
> Thomas P. wrote:
>
>> 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 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection of two arrays? PerlFAQ Server Perl Misc 0 02-02-2011 05:00 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Two arrays: return only values that exist in both arrays? Joshua Muheim Ruby 8 11-23-2007 07:04 PM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
Naming some arrays in a foreach loop?! Thomas Becker Perl Misc 2 07-07-2003 10:42 PM



Advertisments