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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.