Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A question of context

Reply
Thread Tools

A question of context

 
 
sheinrich@my-deja.com
Guest
Posts: n/a
 
      11-07-2007

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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      11-07-2007
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

 
Reply With Quote
 
 
 
 
sheinrich@my-deja.com
Guest
Posts: n/a
 
      11-07-2007
On Nov 7, 3:24 pm, Paul Lalli <mri...@gmail.com> 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


<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!

 
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
Spring context refering to application context Jani Tiainen Java 3 08-27-2007 07:24 AM
return HttpStatusCode.ServiceUnavailable in the context.context.Response.StatusCode (Http response code 503) Flip Rayner ASP .Net 1 01-23-2007 06:35 AM
How to retrieve a session Bean in the Context in a class with the Context reference asd Java 1 11-09-2006 05:00 PM
Strange Context Error: Context 0x197ee0 is disconnected in VS 2005 =?Utf-8?B?U3VuU21pbGU=?= ASP .Net 0 01-10-2006 03:59 PM
Context.Items vs Context.Handler (passing values between pages) VS_NET_DEV ASP .Net 2 05-25-2004 01:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57