in reply to Yet Another Split Question

Your code gives me an error because
print (split)[1];
doesn't mean what you think it is. The compiler doesn't know whether you mean:
( print(split) )[1]; 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)).

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).

print +(split)[1];
Or you could just explicitly parenthesize two levels as above.

Update: not relevant to your specific question, but to your code in general -- you can just replace what you have with:

perl -lnae 'print $F[1]' somefile
-a does auto split into @F, -n does auto while(<>){..}, and -l does auto newline on print statements. See perlrun for details.

blokhead

Replies are listed 'Best First'.
Re^2: Yet Another Split Question
by TimToady (Parson) on Aug 12, 2004 at 00:26 UTC
    And here we have another fine example of the need for a whitespace dependency in the parsing of parentheses. Which is why the OP's code above will work correctly in Perl 6.
      It's certainly a common enough error to warrant a warning (which, unsurprisingly, perl5 does give once the syntax error-producing [] is removed). But it's a only a warning, so that people with a whitespacy coding style can turn it off.
        Well, it's judgement call I have to make, but I think if you're going to go to the trouble of turning off a warning, it's just as easy to install a syntax warping pragma that lets you program all whitespacey without a warning. Meanwhile, for the vast majority of nonwhitespacey folks, Just Works beats a warning most any day, not to mention saving a whole lot of wear and tear on the FAQ.