develooper Front page | perl.beginners | Postings from July 2003

Re: for/foreach question

Thread Previous | Thread Next
From:
awarsd
Date:
July 24, 2003 10:59
Subject:
Re: for/foreach question
Message ID:
20030724175951.82431.qmail@onion.perl.org
Hi,

I think it is good to have foreach and for loop
even tough they are the same
if you are searching in a file you can almost make a sentence
foreach $line(@infile) #<-- to me this is more readable
than
for $line(@infile)
But for loop, I use it only for numbers
for(1..53) or for( $i=0;$i<50;$i++);
it is also close to c++ so the transition from one language to another is
tiny bit easier.
This is my little point of view.

Anthony



"David" <dzhuo@looksmart.net> wrote in message
news:20030724174657.76684.qmail@onion.perl.org...
> Peter Fleck wrote:
>
> > I just stumbled upon this in some perl I'm working on:
> >
> > for $arrayref (@datedbi) {
> >     #do stuff
> > }
> >
> > It didn't look right and sure enough, it should be 'foreach'.
> >
> > But it worked fine and that's my question - why is this working?
> >
> > @datedbi's elements are references to lists and they all seem to be
> > getting processed the way I want whether I use 'for' or 'foreach' in
> > the code.
>
> Perl internally compiles 'for' into 'foreach' for you:
>
> [panda]$ perl -MO=Deparse -e 'for $i (1..4){;}'
> foreach $i (1 .. 4) {
>     ();
> }
> -e syntax OK
> [panda]$
>
> they are exactly the same.
>
> david



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About