develooper Front page | perl.beginners | Postings from May 2008

Re: question on foreach loop

Thread Previous
From:
Chas. Owens
Date:
May 1, 2008 07:30
Subject:
Re: question on foreach loop
On Thu, May 1, 2008 at 10:27 AM, Chas. Owens <chas.owens@gmail.com> wrote:
> On Thu, May 1, 2008 at 10:15 AM,  <itshardtogetone@hotmail.com> wrote:
>  > Hi Members,
>  >  Can someone explain why the foreach loop did not iterate 10 times.
>  snip
>
> >  foreach (@data){
>  >     splice @data,0,1;
>  >     print "printing \@data = @data\n";
>  >  }
>  snip
>
>  Mucking about with the array being iterated over breaks the iterator.
snip

from perldoc perlsyn or http://perldoc.perl.org/perlsyn.html#Foreach-Loops
       If any part of LIST is an array, "foreach" will get very confused if
       you add or remove elements within the loop body, for example with
       "splice".   So don't do that.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

Thread Previous


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About