Is this expected in a foreach()?

Discussion in 'Perl Misc' started by Gowtham, May 8, 2008.

  1. Gowtham

    Gowtham Guest

    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
    Gowtham, May 8, 2008
    #1
    1. Advertising

  2. Gowtham <> 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
    Peter Makholm, May 8, 2008
    #2
    1. Advertising

  3. Gowtham

    Dr.Ruud Guest

    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."
    Dr.Ruud, May 8, 2008
    #3
  4. Gowtham

    Gowtham Guest

    On May 8, 6:51 pm, Peter Makholm <> wrote:
    > Gowtham <> 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
    Gowtham, May 8, 2008
    #4
  5. Gowtham

    Achim Peters Guest

    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
    Achim Peters, May 8, 2008
    #5
  6. Gowtham

    Dr.Ruud Guest

    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."
    Dr.Ruud, May 8, 2008
    #6
  7. Gowtham

    Ben Bullock Guest

    Gowtham <> wrote:
    > On May 8, 6:51 pm, Peter Makholm <> wrote:
    >> Gowtham <> 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.
    Ben Bullock, May 9, 2008
    #7
  8. Gowtham

    Gowtham Guest

    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
    Gowtham, May 9, 2008
    #8
  9. Gowtham

    Uri Guttman Guest

    >>>>> "G" == Gowtham <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, May 9, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    Luc Kumps
    Aug 22, 2003
  3. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    319
    Hans Kesting
    Oct 22, 2004
  4. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    739
    Scott Allen
    Feb 12, 2005
  5. caldera
    Replies:
    1
    Views:
    4,402
    Scott Allen
    Feb 22, 2005
Loading...

Share This Page