Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is this expected in a foreach()?

Reply
Thread Tools

Is this expected in a foreach()?

 
 
Gowtham
Guest
Posts: n/a
 
      05-08-2008
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
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      05-08-2008
Gowtham <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      05-08-2008
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>

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Gowtham
Guest
Posts: n/a
 
      05-08-2008
On May 8, 6:51 pm, Peter Makholm <(E-Mail Removed)> wrote:
> Gowtham <(E-Mail Removed)> writes:
> > 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


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

 
Reply With Quote
 
Achim Peters
Guest
Posts: n/a
 
      05-08-2008
Gowtham schrieb:
> 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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-08-2008
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.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      05-09-2008
Gowtham <(E-Mail Removed)> wrote:
> On May 8, 6:51 pm, Peter Makholm <(E-Mail Removed)> wrote:
>> Gowtham <(E-Mail Removed)> writes:
>> > Is this expected? I feel changes to $b shouldn't change the array
>> > @a ...

>>
>> Yes it is expected and well documented.


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.
 
Reply With Quote
 
Gowtham
Guest
Posts: n/a
 
      05-09-2008
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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-09-2008
>>>>> "G" == Gowtham <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this expected IBGP peer behaviour? p47 Cisco 6 01-08-2006 02:52 PM
- Boot image integrity check...Failed (CRC expected 0x00004e9d, not ron Cisco 1 11-10-2005 01:24 AM
ISE:ERROR:Xst:829: Constant Value expected for Generic 'U'? Phil Tomson VHDL 3 02-16-2005 08:54 AM
[News] Jobless Claims Rise More Than Expected TechGeekPro MCSE 0 04-15-2004 03:44 PM
Re: Better than I expected 73171 The Poster Formerly Known as Kline Sphere MCSD 2 01-05-2004 05:41 PM



Advertisments