Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > On "for (@foo)"

Reply
Thread Tools

On "for (@foo)"

 
 
bill
Guest
Posts: n/a
 
      05-28-2004



Something like this burned me really bad once:

my @x = (1) x 3;
print "@x\n";
for my $y (@x) {
$y = 0;
}
print "@x\n";
__END__
1 1 1
0 0 0

It's as if the loop variable $y was a reference to the current
element of the array, except that it doesn't need to be dererenced.
Surprisingly, to me at least, this doesn't produce any errors:

for my $y (1..3) {
$y = 0;
}

Perl doesn't see the above as analogous to

1 = 0; # triggers "Can't modify constant item" error


OK, now, in an expression like this

for my $x (@X) {
# etc.
}

I assume that Perl keeps track of the index on @X correponding to
the current element. Is there a way to access this "current index"
from within a Perl program?

Thanks!

-bill
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      05-28-2004

Quoth bill <(E-Mail Removed)>:
>
> Something like this burned me really bad once:
>
> my @x = (1) x 3;
> print "@x\n";
> for my $y (@x) {
> $y = 0;
> }
> print "@x\n";
> __END__
> 1 1 1
> 0 0 0
>
> It's as if the loop variable $y was a reference to the current
> element of the array, except that it doesn't need to be dererenced.


Yes.

> Surprisingly, to me at least, this doesn't produce any errors:
>
> for my $y (1..3) {
> $y = 0;
> }
>
> Perl doesn't see the above as analogous to
>
> 1 = 0; # triggers "Can't modify constant item" error


No: an array element is an lvalue, and modifiable.

> OK, now, in an expression like this
>
> for my $x (@X) {
> # etc.
> }
>
> I assume that Perl keeps track of the index on @X correponding to
> the current element. Is there a way to access this "current index"
> from within a Perl program?


No, it simply keeps track of the current element. From that it can find
the next one.

Ben

--
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
-Assyrian stone tablet, c.2800 BC http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      05-28-2004

Quoth "Andrew Palmer" <atp5470 at fsu.edu>:
> > OK, now, in an expression like this
> >
> > for my $x (@X) {
> > # etc.
> > }
> >
> > I assume that Perl keeps track of the index on @X correponding to
> > the current element. Is there a way to access this "current index"
> > from within a Perl program?

>
> I think the only way to do what you want is like
>
> for(my $i=0;$i<$#X;++$i)
> {
> $X[$i]=0;
> }


You never need C-style loops:

for my $i (0..$#X) {
$X[$i] = 0;
}

Ben

--
We do not stop playing because we grow old;
we grow old because we stop playing.
(E-Mail Removed)
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-28-2004
Ben Morrow <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Quoth "Andrew Palmer" <atp5470 at fsu.edu>:
> > > OK, now, in an expression like this
> > >
> > > for my $x (@X) {
> > > # etc.
> > > }
> > >
> > > I assume that Perl keeps track of the index on @X correponding to
> > > the current element. Is there a way to access this "current index"
> > > from within a Perl program?

> >
> > I think the only way to do what you want is like
> >
> > for(my $i=0;$i<$#X;++$i)
> > {
> > $X[$i]=0;
> > }

>
> You never need C-style loops:
>
> for my $i (0..$#X) {
> $X[$i] = 0;
> }


$_ = 0 for @X;

Anno
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      05-28-2004
Anno Siegel wrote:

> $_ = 0 for @X;

@X = (0) x @X;


--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
 
Reply With Quote
 
bill
Guest
Posts: n/a
 
      05-28-2004
In <c966vt$bkc$(E-Mail Removed)> Ben Morrow <(E-Mail Removed)> writes:


>Quoth bill <(E-Mail Removed)>:
>> Surprisingly, to me at least, this doesn't produce any errors:
>>
>> for my $y (1..3) {
>> $y = 0;
>> }
>>
>> Perl doesn't see the above as analogous to
>>
>> 1 = 0; # triggers "Can't modify constant item" error


>No: an array element is an lvalue, and modifiable.


Ah, yes. Somehow I thought that '..' returned a list. Come to
think of it, does anything in Perl *ever* return a list? I tried
other things that I *thought* returned lists, such as `command` or
m//g, in expressions like

for my $x (`seq 1 3`) {
$x = 0;
}

or

for my $x ('123' =~ m/\d/g) {
$x = 0;
}

but Perl did not complain, unlike the case

for my $x (1, 2, 3) {
$x = 0;
}

in which Perl kicks up a ruckus.

Then again, maybe this is not a terribly good test of listudinosity.

-bill

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-28-2004
bill <(E-Mail Removed)> wrote:
>
>
>
> Something like this burned me really bad once:



That can happen when you don't carefully read the documentation
for the features that you use.


> It's as if the loop variable $y was a reference to the current
> element of the array, except that it doesn't need to be dererenced.



The docs call it an "alias".

Perl's control structures are documented in:

perldoc perlsyn


The 3rd paragraph in the "Foreach Loops" section discusses the
behavior that you have observed.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
J Krugman
Guest
Posts: n/a
 
      05-28-2004
In <(E-Mail Removed)> Tad McClellan <(E-Mail Removed)> writes:

>bill <(E-Mail Removed)> wrote:
>>
>> Something like this burned me really bad once:


>That can happen when you don't carefully read the documentation
>for the features that you use.


I don't think one could blame users from assuming that 2+2 is going
to evaluate to 4, no matter how clearly the documentations stated

Note that perl always evaluates 2+2 to 5.

The idea that the loop variable somehow channels through to the
array it is looping over is something I've never seen outside of
Perl.

jill
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-28-2004

Quoth bill <(E-Mail Removed)>:
> In <c966vt$bkc$(E-Mail Removed)> Ben Morrow <(E-Mail Removed)> writes:
>
>
> >Quoth bill <(E-Mail Removed)>:
> >> Surprisingly, to me at least, this doesn't produce any errors:
> >>
> >> for my $y (1..3) {
> >> $y = 0;
> >> }
> >>
> >> Perl doesn't see the above as analogous to
> >>
> >> 1 = 0; # triggers "Can't modify constant item" error

>
> >No: an array element is an lvalue, and modifiable.

>
> Ah, yes. Somehow I thought that '..' returned a list. Come to
> think of it, does anything in Perl *ever* return a list?


Hang on, no, I misread your code... my brane read it as

for my $y (@x) {

.. Hmmm; I'm puzzled too . I think I'd call this a perl bug, especially
as

for my $y (1, 2, 3) {

fails...

Ben

--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] (E-Mail Removed)
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-28-2004

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel):
> Ben Morrow <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >
> > Quoth "Andrew Palmer" <atp5470 at fsu.edu>:
> > > > OK, now, in an expression like this
> > > >
> > > > for my $x (@X) {
> > > > # etc.
> > > > }
> > > >
> > > > I assume that Perl keeps track of the index on @X correponding to
> > > > the current element. Is there a way to access this "current index"

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > > > from within a Perl program?

^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > for my $i (0..$#X) {
> > $X[$i] = 0;
> > }

>
> $_ = 0 for @X;


The OP was asking about more complex loops than the example given...

Ben

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments