Rephrase the list literal question
The following is from page 75 in the Camel: "List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment: $x = ( ($a, $b) = (7,7,7) ); #set $x to 3, not 2 " why. how. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Rephrase the list literal question
Nkuipers wrote: > The following is from page 75 in the Camel: > > "List assignment in scalar context returns the number of elements produced > by the expression on the right side of the assignment: > > $x = ( ($a, $b) = (7,7,7) ); #set $x to 3, not 2 > can you guess what $x is now: $x = ($a,$b) = (7,7,7); make sense now right? :-) if still doesn't make sense, it's time to check the associativity of the '=' operator. yes, i know nowaday no one is mentioning that anymore :-) david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Rephrase the list literal question
On Wed, Oct 09, 2002 at 03:22:20PM -0700, nkuipers wrote: > The following is from page 75 in the Camel: > > "List assignment in scalar context returns the number of elements produced by > the expression on the right side of the assignment: > > $x = ( ($a, $b) = (7,7,7) ); #set $x to 3, not 2 > > " > > why. how. Note that this is the same as $x = ($a, $b) = (7,7,7); = is right associative. ($a, $b) = (7,7,7) you already understand. This is the list assignment mentioned above. It is in a scalar context because of the assignment to $x, which is a scalar. But "the right side of the assignment" is still talking about the list assignment, and thus $x gets the number of elements in (7,7,7), which is 3, not ($a, $b) which would be 2. This allows you to do useful things like $x = () = some_function; which evaluates some_function in list context, and sets $x to the number of elements returned. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Rephrase the list literal question
The most useful application of this semantic is in a construct like while (my ($key, $value) = each %hash) or if (my ($x, $y) = /(...)(...)/) If anything is assigned to the list of variables, the result of the assignment will be at least 1 (or more likely 2, in the cases above). That value is true and hence the condition succeeds. This is a case of where the syntax looks quite intuitive and yet it works by virtue of a language feature virtually no one knows. -- Peter Scott http://www.perldebugged.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]