On Wed, 20 Nov 2002, darren chamberlain wrote:
> * Chris Devers <[EMAIL PROTECTED]> [2002-11-20 15:25]:
> > foreach $i (@array) {
> > # do something with $i
> >
> > # then if appropriate...
> > print $another_array[$i];
>
> But $i is an element of the array, not an integer to be used as an
> offset, most likely.
Hence the "if appropriate". I did say I was confused :)
But yeah, the solution is to use an iterator (which $i above probably
wouldn't be unless @array just happened to be 0 1 2 3 4 5... which it
probably isn't :).
> I think the answer is to use a C-style for loop:
>
> for (my $i = 0; $i < $#array; $i++) {
> # $i is your "iterator"
> # $array[$i] is the "current" element
What is the advantage of that over something like:
my $iterator = 0;
foreach my $elem (@array) {
$iterator++;
# do stuff on $elem, which seems like it would
# be the same thing as $array[$iterator] but not
# necessarily the same as $otherarray[$iterator]
}
These look roughly equivalent to me.
Is there any significant difference, beyond style preference?
--
Chris Devers [EMAIL PROTECTED]
Q: Why do the police always travel in threes?
A: One to do the reading, one to do the writing, and
the other keeps an eye on the two intellectuals.
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm