A question of context

Discussion in 'Perl Misc' started by sheinrich@my-deja.com, Nov 7, 2007.

  1. Guest

    Why does this line work as expected

    print qw(A B C D E F G H)[split //, '76543210'], "\n"; # outputs
    "HGFEDCBA"

    but this one doesn't compile:

    print (split //, 'ABCDEFGH')[split //, '76543210'], "\n"; # syntax
    error at ..., near ")["

    ???

    Is there a tweak to make the 2nd version work as well?
    I tried some combinations with @(), @{} but didn't get it working.

    Eplanation and remedy would both be appreciated.

    TIA, Steffen
    , Nov 7, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Nov 7, 9:18 am, wrote:
    > Why does this line work as expected
    >
    > print qw(A B C D E F G H)[split //, '76543210'], "\n"; # outputs
    > "HGFEDCBA"
    >
    > but this one doesn't compile:
    >
    > print (split //, 'ABCDEFGH')[split //, '76543210'], "\n"; # syntax
    > error at ..., near ")["
    >
    > ???
    >
    > Is there a tweak to make the 2nd version work as well?
    > I tried some combinations with @(), @{} but didn't get it working.
    >
    > Eplanation and remedy would both be appreciated.


    If it looks like a function, it acts like a function. What that means
    is that if a function like print is immediately followed by a
    parenthesis, then the parentheses enclose the arguments to the
    function. So in your example, the arguments to print stopped at
    the ), and the rest therefore makes no sense.

    To solve, you can either enclose everything you meant to print in
    parentheses:
    print ((split //, 'ABCDEFGH')[split //, '76543210'], "\n");
    or stick a + in front of the first (
    print +(split //, 'ABCDEFGH')[split //, '76543210'], "\n";

    Paul Lalli
    Paul Lalli, Nov 7, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 7, 3:24 pm, Paul Lalli <> wrote:
    > On Nov 7, 9:18 am, wrote:
    >
    >
    >
    > > Why does this line work as expected

    >
    > > print qw(A B C D E F G H)[split //, '76543210'], "\n"; # outputs
    > > "HGFEDCBA"

    >
    > > but this one doesn't compile:

    >
    > > print (split //, 'ABCDEFGH')[split //, '76543210'], "\n"; # syntax
    > > error at ..., near ")["

    >
    > > ???

    >
    > > Is there a tweak to make the 2nd version work as well?
    > > I tried some combinations with @(), @{} but didn't get it working.

    >
    > > Eplanation and remedy would both be appreciated.

    >
    > If it looks like a function, it acts like a function. What that means
    > is that if a function like print is immediately followed by a
    > parenthesis, then the parentheses enclose the arguments to the
    > function. So in your example, the arguments to print stopped at
    > the ), and the rest therefore makes no sense.
    >
    > To solve, you can either enclose everything you meant to print in
    > parentheses:
    > print ((split //, 'ABCDEFGH')[split //, '76543210'], "\n");
    > or stick a + in front of the first (
    > print +(split //, 'ABCDEFGH')[split //, '76543210'], "\n";
    >
    > Paul Lalli


    <slapping my forehead>
    .... and if I'd have used warnings as I usually do it would have said:
    "print (...) interpreted as function"

    Thanks a lot pal!
    , Nov 7, 2007
    #3
    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. VS_NET_DEV
    Replies:
    2
    Views:
    3,798
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    702
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    652
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    432
    www.pulpjava.com
    Nov 9, 2006
  5. Jani Tiainen
    Replies:
    3
    Views:
    1,017
    Jani Tiainen
    Aug 27, 2007
Loading...

Share This Page