Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > List and scalar OOE

Reply
Thread Tools

List and scalar OOE

 
 
nemo@weathersong.net
Guest
Posts: n/a
 
      10-17-2005
I don't understand the seeming discrepancy between this:

@a = ('foo', 'bar', 'baz');
@b = (@a, shift @a);

and this:

$a = 5;
@b = ($a, ++$a, $a++);

In the first case, OOE is intuitive. @b is first assigned the value -as
it is then- of @a, and then the value of "shift @a", so @b ends up
being foo, bar, baz, foo. In the second case however, the list elements
seem to be evaluated in advance (or, as if they were now pointers, not
copies), so @b is 7 7 6, instead of 5 6 6.

Is the discrepancy intentional? That is, OOE behaves differently on
lists and scalars? Or is there some other subtle difference I'm missing
completely, here?

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-17-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> $a = 5;
> @b = ($a, ++$a, $a++);
> ... In the second case however, the list elements
> seem to be evaluated in advance (or, as if they were now pointers, not
> copies), so @b is 7 7 6, instead of 5 6 6.


> Is the discrepancy intentional? That is, OOE behaves differently on
> lists and scalars? Or is there some other subtle difference I'm missing
> completely, here?


I think the behavior is attributed to the fact that the "++" operator
has a very high order of precedence, whereas a comma operator (as a
list element separator) has a rather low order of precedence. The
expression is evaluated according to precedence, so the "++" gets
evaluated first, before the "listification" happens.

 
Reply With Quote
 
 
 
 
nemo@weathersong.net
Guest
Posts: n/a
 
      10-17-2005
Ahh, it's not OOE at all. I was overthinking it. Thanks.

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      10-17-2005

(E-Mail Removed) wrote:
> Ahh, it's not OOE at all. I was overthinking it. Thanks.


i think Perl looks at the statement from right to left. is it really a
matter of binding precedence?

 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      10-18-2005
(E-Mail Removed) wrote:

> I don't understand the seeming discrepancy between this:
>
> @a = ('foo', 'bar', 'baz');
> @b = (@a, shift @a);
>
> and this:
>
> $a = 5;
> @b = ($a, ++$a, $a++);
>
> In the first case, OOE is intuitive.


For an explanation of OOE with respect to ++ see the old thread "When
does (pre|post)increment happen?"

http://groups.google.com/group/comp....153e4f8b1d0877

 
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
RE: Newbie - How to iterate list or scalar ? Delaney, Timothy (Tim) Python 1 08-10-2006 03:39 PM
Newbie - How to iterate list or scalar ? Andy Dingley Python 7 08-09-2006 09:31 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
function taking scalar or list argument beliavsky@aol.com Python 9 08-24-2004 04:45 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments