Is this expected in a foreach()?

G

Gowtham

Is this expected? I feel changes to $b shouldn't change the array
@a ...

DB<26> @a = qw/ A B C /;

DB<27> foreach my $b ( @a ) { $b =~ s/([A-Z])/lc $1/e; }

DB<28> x @a
0 'a'
1 'b'
2 'c'

This is perl 5.8.8

Thanks
Gowtham
 
P

Peter Makholm

Gowtham said:
Is this expected? I feel changes to $b shouldn't change the array
@a ...

Yes it is expected and well documented. You feeling is wrong. Read
'perldoc perlsyn':

If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. Conversely, if any element of LIST is NOT an lvalue,
any attempt to modify that element will fail. In other words, the
"foreach" loop index variable is an implicit alias for each item in the
list that you're looping over.
This is perl 5.8.8

I belive the above to be true for all perl-versions I have worked
with.

//Makholm
 
D

Dr.Ruud

Gowtham schreef:
Is this expected? I feel changes to $b shouldn't change the array
@a ...

DB<26> @a = qw/ A B C /;

DB<27> foreach my $b ( @a ) { $b =~ s/([A-Z])/lc $1/e; }

DB<28> x @a
0 'a'
1 'b'
2 'c'

<quote src="perlsyn">
If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. Conversely, if any element of LIST is NOT an lvalue,
any attempt to modify that element will fail. In other words, the
"foreach" loop index variable is an implicit alias for each item in the
list that you're looping over.
</quote>
 
G

Gowtham

Yes it is expected and well documented. You feeling is wrong. Read
'perldoc perlsyn':

If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. Conversely, if any element of LIST is NOT an lvalue,
any attempt to modify that element will fail. In other words, the
"foreach" loop index variable is an implicit alias for each item in the
list that you're looping over.


I belive the above to be true for all perl-versions I have worked
with.

//Makholm

Ok. How is an lvalue defined? Is it something to which we can assign
something?
Something which can be the part of the left hand side of an assignment
expression
right?

But, here the list @a contains literal strings and not references to
other variables.
I know I am wrong but would like to understand how an lvalue is
defined in general and
how particularly in perl. It will also be helpful if somebody can give
examples for non-lvalues...

Thanks again
Gowtham
 
A

Achim Peters

Gowtham said:
Is this expected? I feel changes to $b shouldn't change the array
@a ...

DB<26> @a = qw/ A B C /;

DB<27> foreach my $b ( @a ) { $b =~ s/([A-Z])/lc $1/e; }

And don't forget the 11th perl commandment:

"Thou shalt never use $a or $b except when thou art programming in a
sort context"

"But it's only sample code!"

"I said, NEVER, son."

Bye
Achim
 
D

Dr.Ruud

Gowtham schreef:
It will also be helpful if somebody can give
examples for non-lvalues...

perl -Mstrict -Mwarnings -le'
my @txt = qw{this is an example};
tr/a-z/n-za-m/ for @txt;
print for @txt;
tr/a-z/n-za-m/ for qw{this is an example};
'
guvf
vf
na
rknzcyr
Modification of a read-only value attempted at -e line 5.
 
B

Ben Bullock

It's also "better" that way - if you have to backtrack into the array
and then alter the value in the array, it's much less convenient. As
usual, Perl is optimized for maximum programmer convenience at the
expense of understandability.
Ok. How is an lvalue defined? Is it something to which we can assign
something?

lvalue means "left value". Look at

$x = $y

Here $x is on the left of the equals sign, so it's an "lvalue".

$x = "baby" # OK
$x = $y # OK
"baby" = $y # Not OK, "baby" is not an lvalue.
Something which can be the part of the left hand side of an assignment
expression right?

No, not right, left.
But, here the list @a contains literal strings and not references to
other variables.
I know I am wrong but would like to understand how an lvalue is
defined in general and
how particularly in perl. It will also be helpful if somebody can give
examples for non-lvalues...

You shouldn't really ask for examples on a newsgroup like this, it's
not a teaching forum. You need to read up on this from web pages, or
books, or try running example programs.
 
G

Gowtham

Yes, got it.

So, this is a feature rather than a bug/annoyance. This
feature is particularly useful when manipulating a very
large list.

Thanks for all the details.

- Gowtham
 
U

Uri Guttman

G> Yes, got it.
G> So, this is a feature rather than a bug/annoyance. This
G> feature is particularly useful when manipulating a very
G> large list.

s/a very large//;

aliasing in for is very useful in many things. among other things it
also speeds up the loops as only aliases need to be made and not copies
of all the data. the length of the list is not a factor in the
usefulness of this feature.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top