in reply to Yet Another Split Question
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Yet Another Split Question
by TimToady (Parson) on Aug 12, 2004 at 00:26 UTC | |
by ysth (Canon) on Aug 12, 2004 at 04:05 UTC | |
by TimToady (Parson) on Aug 12, 2004 at 08:15 UTC | |
by ysth (Canon) on Aug 12, 2004 at 11:16 UTC |