in reply to arrays, context, and print - oh my.

print +(1,2,3,4)[3]
works, because the + tells perl that the () are a list and not enclosing the parameters for the print function. Otherwise perl would understand something like ( print( 1, 2, 3, 4 ) )[3]

print scalar (1,2,3,4)[2];
Doesn't work, because you seem to want the scalar value of an array (it's size), but you're specifying a list. That's a difference you should read up on. SuperSearch should give enough info though ;)

Someone hit me if i'm wrong :)

Ordinary morality is for ordinary people. -- Aleister Crowley

Replies are listed 'Best First'.
Re^2: arrays, context, and print - oh my.
by fishbot_v2 (Chaplain) on Aug 09, 2005 at 12:51 UTC
    print scalar (1,2,3,4)[2];
    Doesn't work, because you seem to want the scalar value of an array (it's size), but you're specifying a list.

    I think that you are wrong on this one, notice:

    print scalar (1,2,3,4)[2]; # syntax error at )[ print scalar((1,2,3,4)[2] ); # prints 3 print scalar+(1,2,3,4)[2]; # also prints 3

    Your first answer applies to both. The scalar is just hogging the parens. It is also useless, as it is forcing a scalar context on a scalar.

      Yep. You're right :)

      Ordinary morality is for ordinary people. -- Aleister Crowley
Re^2: arrays, context, and print - oh my.
by tphyahoo (Vicar) on Aug 09, 2005 at 11:18 UTC
    Thanks phaylon.

    I think the docu for + I wanted was in perlop: "Unary ``+'' has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments. (See examples above under Terms and List Operators (Leftward).)"