A question of context

A question of context

am 07.11.2007 15:18:45 von sheinrich

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

Re: A question of context

am 07.11.2007 15:24:05 von Paul Lalli

On Nov 7, 9:18 am, sheinr...@my-deja.com 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

Re: A question of context

am 07.11.2007 16:16:11 von sheinrich

On Nov 7, 3:24 pm, Paul Lalli wrote:
> On Nov 7, 9:18 am, sheinr...@my-deja.com 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


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

Thanks a lot pal!