devslashneil has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I'm sure i've done this before no worries, but i can't figure out why it is a syntax error now.
my $var = "1:2:3:4:5:6"; my $x = split(":",$var)[4];
The error is:
"syntax error at ./splittest.pl line 4, near ")[""
I'm sure its a stupid error, but i was just wondering what the correct syntax for this is?.

Neil Archibald - /dev/IT -

Replies are listed 'Best First'.
Re: subroutine()[$x];
by tilly (Archbishop) on Jun 28, 2003 at 07:00 UTC
    Perl doesn't know if you want to take a function then slice the result, or take a slice and pass that to the function. So you have to clarify as follows:
    my $var = "1:2:3:4:5:6"; my $x = (split":",$var)[4];
•Re: subroutine()[$x];
by merlyn (Sage) on Jun 28, 2003 at 07:42 UTC
      Minor nitpick:   qw/ ... / [ LIST ] (without any parenthesis around qw//) works too (as of 5.6 iirc). Not that this is a particulary useful construct. :-)

      ihb
      Thank you both for your replies, that cleared it up :)

      Neil Archibald - /dev/IT -
duh, simple formatted correctly
by Anonymous Monk on Jun 28, 2003 at 22:36 UTC
    my $var = "1:2:3:4:5:6";
    my $x = (split(":",$var))[4];
simple
by Anonymous Monk on Jun 28, 2003 at 22:32 UTC
    my $var = "1:2:3:4:5:6"; my $x = (split(":",$var))4;