Really simple question.

Daniel Barlow dan at telent.net
Thu Feb 8 18:01:44 GMT 2007


Andy Armstrong wrote:
> The normal idiom is
> 
>    for my $i ( 0 .. $#arrayname ) {
>        # do some stuff
>    }
> 
> You can use C style for loops if you must
> 
>    for ( my $i = 0; $i < @arrayname; $i++ ) {
>        # do some stuff
>    }
> 
> It's generally neither necessary or desirable though.

Well, and I learn something new every day.  Muttering about generating
big temporary arrays, off I go to perlop(1) and find it now says

 The range operator is useful for writing "foreach (1..10)"
 loops and for doing slice operations on arrays. In the current imple-
 mentation, no temporary array is created when the range operator is
 used as the expression in "foreach" loops, but older versions of Perl
 might burn a lot of memory

I think the language is obviously too regular and easily memorised,
because if only I had forgotten something and had to read that page
again I'd have noticed the change since whenever I first internalised it.


-dan



More information about the london.pm mailing list