Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach of 2 variables

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
Is there a syntax for processing 2 arrays in parallel in a single foreach loop?

Something like:

foreach $var1 (@ARR1) and $var2 (@arr2) { ... }

Thanks.
 
Not really.
You need to use a "for" loop with an index.
The limit for the index is an intersting question.
It depends on whether the arrays are the same length or not and whether you need to process the overhang in some way.
Assuming they are the same length though it's quite simple.
Code:
for(my $index=0; $index<=$#ARR1; $index++) {
  my $var1 = $ARR1[$index];
  my $var2 = $ARR2[$index];
  # Use them here .....
}



Trojan.
 
Several options open to you, what do you need to get from the loop?
Are both arrays always the same length?
Code:
$array_length=@ARRAY1;
for($x=0; $x<$array_length; ++$x){
  $ITEM1=$ARRAY1[$x];
  $ITEM2=$ARRAY2[$x];
}
If you wait, one of the gurus will come up with a 1 line solution.

Keith
 
or using a bit more perlish syntax:

Code:
for(0 .. $#ARR1) {
  my $var1 = $ARR1[$_];
  my $var2 = $ARR2[$_];
  # Use them here .....
}

but they should be array's of equal lengths as has been pointed out
 
I would point out though that Kevin's syntax does create a list of the length of the array for indexing with.
For small arrays that's not relevant but for very large arrays it is an unneccessary use of space.
Sorry Kevin! ;-)

Happy New Year to all.



Trojan.
 
I am not aware that the basic system of generating lists has changed at all.
Maybe there is something new for me to learn there!
Either way, if there is any doubt, one should be careful of using a solution that might introduce an issue that can easily be avoided.

:)


Trojan.
 
Thanks for the replies. I figured I could use the old standard index method but being a perl novice I thought there might be a more elegant perlish method.
 
Range Operators

Binary ".." is the range operator, which is really two different operators depending on the context. In list context, it returns a list of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty list. The range operator is useful for writing foreach (1..10) loops and for doing slice operations on arrays. In the current implementation, no temporary array is created when the range operator is used as the expression in foreach loops, but older versions of Perl might burn a lot of memory when you write something like this:

for (1 .. 1_000_000) {
# code
}

 
I figured I could use the old standard index method but being a perl novice I thought there might be a more elegant perlish method.

There is, see my post above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top