just a thought: it's not really the index you're interested in. you just want to loop over more than one array in parallel and perl provides no operator for this. other than writing a sub which messes with lots of array and code refs do any of the perl gurus know of some idiom for this?
here's the sub i'd use (untested):
sub arrloop {
my ($arrays, $code) = @_;
# keep calling code until we've been through all of the
# arrays.
for (my $i = 0;
grep { $i <= scalar(@$_) } @$arrays;
$i++) {
$code->(map { $_->[$i] } @$array);
}
}
which i could use like this:
arrloop [\@first, \@middle, \@last], sub {
my ($first, $middle, $last) = @_;
print "Hello $first $middle $last\n";
}
this is just as contrived an examples as Limbic~Region's example, but my question is about iterating over arrays, not proper choice of data structures.
if i'm not mistaken perl6's for will solve this.
Edit: s/PRE/CODE/ tags. larsen
-
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.
|