Front page | perl.beginners |
Postings from May 2008
RE: question on foreach loop
Thread Previous
|
Thread Next
From:
Andrew Curry
Date:
May 1, 2008 08:22
Subject:
RE: question on foreach loop
>-----Original Message-----
>rom: J. Peng [mailto:peng.kyo@gmail.com]
Sent: 01 May 2008 15:31
To: itshardtogetone@hotmail.com
Cc: beginners@perl.org
Subject: Re: question on foreach loop
To add a statement of "print $_" follow the foreach, you will see what
was happening.
foreach (@data){
print "$_\n";
splice @data,0,1;
print "printing \@data = @data\n";
}
On Thu, May 1, 2008 at 10:15 PM, <itshardtogetone@hotmail.com> wrote:
> Hi Members,
> Can someone explain why the foreach loop did not iterate 10 times.
>
> Thanks
>
> #################
> use strict;
> use warnings;
>
> my @data = (1..10);
>
> foreach (@data){
> splice @data,0,1;
> print "printing \@data = @data\n"; }
>
> ##### results ###########
> printing @data = 2 3 4 5 6 7 8 9 10
> printing @data = 3 4 5 6 7 8 9 10
> printing @data = 4 5 6 7 8 9 10
> printing @data = 5 6 7 8 9 10
> printing @data = 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 @data 1,2,3,4,5,6,7,8,9,10#
Remove 0,1 (1) so....
printing @data = 2 3 4 5 6 7 8 9 10
ON @data 2,3,4,5,6,7,8,9,10
Remove 2
printing @data = 3 4 5 6 7 8 9 10
ON @data 3,4,5,6,7,8,9,10
Remove 3
printing @data = 4 5 6 7 8 9 10
ON @data 4,5,6,7,8,9,10
Remove 4
printing @data = 5 6 7 8 9 10
ON @data 5,6,7,8,9,10
Remove 5
printing @data = 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?
--
J. Peng - QQMail Operation Team
eMail: peng.kyo@gmail.com AIM: JeffHua
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
commands, e-mail: beginners-help@perl.org http://learn.perl.org/
This e-mail is from the PA Group. For more information, see www.thepagroup.com.
This e-mail may contain confidential information.
Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments.
If you have received it in error, please contact the sender immediately.
Any opinion expressed in this e-mail is personal to the sender and may not reflect the opinion of the PA Group.
Any e-mail reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices.
Thread Previous
|
Thread Next