A question of context

S

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
 
P

Paul Lalli

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
 
S

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.

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top