simon.proctor has asked for the wisdom of the Perl Monks concerning the following question:

I was hoping someone could explain the following:
my $a = 22; my $b = 5; my $c = ($a, $b++); print $a,"\t",$b,"\t",$c,"\n";
Why does c get assigned the value of b?

I realise this is most likely because I don't fully grok the () operation so if someone could point me in the direction of the right M to RTF then I'd be grateful.

Thnx

SP

Replies are listed 'Best First'.
Re: Unexpected variable assignment
by dragonchild (Archbishop) on Apr 09, 2003 at 21:57 UTC
    An array in scalar context returns its size. A list in scalar context returns the last element. Your code is dealing with a list.

    The best way I've found to distinguish between the two is that if you're manipulating the named thing, you're working with an array. Otherwise, chances are you're working with a list.

    my @x = (6 .. 10); my $a = (6 .. 10); my $b = @x; print "$a $b\n";

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Sorry, I'm being obtuse, here.

      Is it a list because an operation occurs on an element within ()?. When I looked at it saw an array in scalar context. Your explanation hasn't quite come down to my level of incompetence - fancy another go? :)

        The parenthesis don't construct a list. In this case, the context of the assignment does.

        my $scalar = 1, 2, 3; # interpreted as: (my $scalar = 1), 2, 3; my @letters = 'a', 'b', 'c'; # interpreted as: (my @letters = 'a'), 'b', 'c';

        The problem here is that the precendence of the assignment is higher than the precendence of the comma. The paranthesis group the constants and help the parser realize that the comma is not separating statements.

        Arrays are accessed via containers (variables). Lists aren't.

        Arrays may or may not have names -- anonymous arrays are still arrays.

        Lists can be stored in arrays, but then they cease to behave as lists.

        Not, not really. A list isn't a Perl data structure, but an array is. A list is just a bunch of stuff. The way we work with "stuff" in Perl is to use the "comma operator" (and its cousin, '=>'). The comma operator is one of those things that no-one (who isn't on p5p) thinks about and expects to just DWIM. But, it's the way that arrays and lists interacting possible.

        Think about it this way - a list is some stuff collected together, but an array is a place in memory with all sorts of magic associated with it. This means that you can take an array and do useful operations on it, but you cannot do that with a list. However, there is no such thing as "array context" - it's "list context".

        The reason why most people confuse the two (and why they're usually interchangeable) is that an array will (usually) impose list context around it, if it's an lvalue. (Also, there's the deplorably-named wantarray, which actually checks for list context and has nothing to do with arrays.)

        At this point, you've exhausted my knowledge. If you want more, I suggest reading the relevant portions of the Camel book. I did so at one point, thought it was neat, then promptly forgot it cause it's never once been relevant to me in my years of Perl development. :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Unexpected variable assignment
by Mr. Muskrat (Canon) on Apr 09, 2003 at 21:58 UTC

    my $c = ($a, $b++);

    This line assigns last value of the list to $c (operation of comma operator) then increments the value in $b by 1.

Re: Unexpected variable assignment
by PodMaster (Abbot) on Apr 10, 2003 at 06:06 UTC
    As others have pointed out, it's how the comma operator operates in scalar context.
    Learn all about it in japhys excellent article "List" is a Four-Letter Word.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Unexpected variable assignment
by Vorlin (Beadle) on Apr 09, 2003 at 22:42 UTC
    In your my $c = ($a,$b++);, nothing is being done except writing the value of $a into $c, then writing the value of $b (without the ++ incrementation), and in your print statement, it shows the original $b however, your $b is now 6, not 5 because of the () statement.

    () with variables in them, as listed, will perform all computations/etc and the values would then be different.

    This is the way I've seen it through working with ()...I could be wrong and there could be much better explanations, but that's what I know.
Re: Unexpected variable assignment
by pg (Canon) on Apr 10, 2003 at 03:45 UTC
    Also you can force this into array context, instead of the default list context, just by doing something like this:
    use strict; my $a = 100; my $b = 200; my $c = ($a, $b++)[0];# force it into array context print $a, "\n"; print $b, "\n"; print $c, "\n";
    This prints:
    100 201 100