doesn't mean what you think it is. The compiler doesn't know whether you mean:print (split)[1];
It's kinda like the expression: print (5 + 3) * 4 prints 8, and has a return value of 4 because it's parsed as: ( print(5 + 3) ) * 4. But in your case, since a list slice needs parentheses around the entire list your code causes a compile error (there are not parentheses around print(split)).( print(split) )[1]; print( (split)[1] );
You can disambiguate those parentheses with a unary plus, to tell the compiler that the parentheses aren't used for a function call, they just are used as a grouping in the first argument (and the function call is without parentheses).
Or you could just explicitly parenthesize two levels as above.print +(split)[1];
Update: not relevant to your specific question, but to your code in general -- you can just replace what you have with:
-a does auto split into @F, -n does auto while(<>){..}, and -l does auto newline on print statements. See perlrun for details.perl -lnae 'print $F[1]' somefile
blokhead
In reply to Re: Yet Another Split Question
by blokhead
in thread Yet Another Split Question
by bier
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |