Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: question on...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 11011 of 11531
Post > Topic >>

Re: question on foreach loop

by rob.dixon@[EMAIL PROTECTED] (Rob Dixon) May 1, 2008 at 05:44 PM

Andrew Curry wrote:
>> -----Original Message-----
>> rom: J. Peng [mailto:peng.kyo@[EMAIL PROTECTED]
 
> Sent: 01 May 2008 15:31
> To: itshardtogetone@[EMAIL PROTECTED]
> Cc: beginners@[EMAIL PROTECTED]
> Subject: Re: question on foreach loop
> 
> To add a statement of "print $_" follow the foreach, you will see what
> was happening.
> 
> foreach (@[EMAIL PROTECTED]
){
>    print "$_\n";
>    splice @[EMAIL PROTECTED]
>    print "printing \@[EMAIL PROTECTED]
 = @[EMAIL PROTECTED]
";
> }
> 
> On Thu, May 1, 2008 at 10:15 PM,  <itshardtogetone@[EMAIL PROTECTED]
> wrote:
>> Hi Members,
>>  Can someone explain why the foreach loop did not iterate 10 times.
>>
>>  Thanks
>>
>>  #################
>>  use strict;
>>  use warnings;
>>
>>  my @[EMAIL PROTECTED]
 = (1..10);
>>
>>  foreach (@[EMAIL PROTECTED]
){
>>     splice @[EMAIL PROTECTED]
>>     print "printing \@[EMAIL PROTECTED]
 = @[EMAIL PROTECTED]
";  }
>>
>>  ##### results ###########
>>  printing @[EMAIL PROTECTED]
 = 2 3 4 5 6 7 8 9 10
>>  printing @[EMAIL PROTECTED]
 = 3 4 5 6 7 8 9 10
>>  printing @[EMAIL PROTECTED]
 = 4 5 6 7 8 9 10
>>  printing @[EMAIL PROTECTED]
 = 5 6 7 8 9 10
>>  printing @[EMAIL PROTECTED]
 = 6 7 8 9 10
> 
> Is not what is exactly expected
> 
> Removes the elements designated by OFFSET and LENGTH
>              from an array, and replaces them with the elements
>              of LIST, if any.  
> So above...
> 
> ON @[EMAIL PROTECTED]
 1,2,3,4,5,6,7,8,9,10#
> Remove 0,1 (1) so.... 
> printing @[EMAIL PROTECTED]
 = 2 3 4 5 6 7 8 9 10
> 
> ON @[EMAIL PROTECTED]
 2,3,4,5,6,7,8,9,10
> Remove 2
> printing @[EMAIL PROTECTED]
 = 3 4 5 6 7 8 9 10
> 
> 
> ON @[EMAIL PROTECTED]
 3,4,5,6,7,8,9,10
> Remove 3
> printing @[EMAIL PROTECTED]
 = 4 5 6 7 8 9 10
> 
> ON @[EMAIL PROTECTED]
 4,5,6,7,8,9,10
> Remove 4
> printing @[EMAIL PROTECTED]
 = 5 6 7 8 9 10
> 
> ON @[EMAIL PROTECTED]
 5,6,7,8,9,10
> Remove 5
> printing @[EMAIL PROTECTED]
 = 6 7 8 9 10
> 
> As the iterator of foreach has been modified once by the foreach and
> once by the splice it thinks it has nothing more to process?

It's best just to take it as read that if you modify the array that
foreach is
interating over then the results are undefined. You should never do it, if
only
because the actual behaviour may well vary for different versions or
builds of
Perl, and possibly even for the same version on different platforms.
Indeed it's
not ruled out that the same build on the same platform could behave
differently
on two identical runs.

If you want a way of understanding why you are seeing these results, then
how
about this: foreach has executed five iterations, and there are only five
elements in the array, so it's all done.

Rob
 




 6 Posts in Topic:
question on foreach loop
itshardtogetone@[EMAIL PR  2008-05-01 22:15:36 
Re: question on foreach loop
chas.owens@[EMAIL PROTECT  2008-05-01 10:30:22 
Re: question on foreach loop
chas.owens@[EMAIL PROTECT  2008-05-01 10:27:29 
Re: question on foreach loop
peng.kyo@[EMAIL PROTECTED  2008-05-01 22:31:29 
RE: question on foreach loop
andrew.curry@[EMAIL PROTE  2008-05-01 16:22:35 
Re: question on foreach loop
rob.dixon@[EMAIL PROTECTE  2008-05-01 17:44:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 19:11:35 CDT 2008.