Conrad, Bill (ThomasTech) <> wrote:
>       I thought that Perl had another internal variable that contained
the
> numerical index of each array entry without having to count them. I
> guess what I thought was happening internally was that Perl was
> maintaining an index of @List and returning @List[index] when in fact
> Perl is internally making a copy of @List and popping each entry off
> the copied list.     

You couldn't have been more wrong, Bill. The foreach style loop iterates
over the provided list with the control variable ($_ unless specified)
being an alias for each value in that list. It is not unknown for
novices and experienced programmers alike to miss that and get
unexpected results.

Also, many people don't realise that for and foreach are synonyms. For
example:

use strict;
use warnings;

my @array = qw{a b c d e};

for (@array) {
    $_ = uc $_;
}

foreach (my $i = 0; $i < @array; ++$i) {
    print "entry $i: $array[$i]\n";
}

Although I wouldn't recommend writing such counter-intuitive code for
real.

HTH, HAND

-- 
Brian Raven 


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to