There's a few ways to do it. The most intuitive is the following:
for (my $i = 0; $i < @a; $i++) {
# Do stuff with @a and @b
}
Another way would be to make a temporary hash and use
each. This means you don't care what order stuff is done in, so long as the Nth element of @a is processed with the Nth element of @b.
my %temp;
@temp{@a} = @b;
while (my ($a_val, $b_val) = each %temp) {
# Do stuff with $a_val and $b_val
}
The first is clearer and easier to work with. Plus, it allows the addition of some @c later on in the future.
Now, if these are parallel arrays, you might want to consider doing the following:
use mapcar; # This is on a node [tye] posted somewhere on PM
my @temp_arr = mapcar { \@_ } (\@a, \@b);
foreach my $vals (@temp_arr) {
# Do stuff with $vals->[0] (from @a) and $vals->[1] (from @b)
}
That makes it very clear they are parallel arrays. *shrugs* YMMV
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.