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

Fellow Monks: I run into the following error with this line of code:
my $start=(split(/ /,$data[146]),3);
I was hoping to pull the fourth element of a single-space delimited data line with the code shown above. However I get the following error:
split to @_ is deprecated
I could just do it this way:
my @startline=split(/ /,$data[146]); my $start=$startline[3];
But.... I was really hoping to do it in the way which was first mentioned, which is unfortunately deprecated, a smooth one-liner. Any thoughts on how to do this? I am certian that I must be missing the obvious. Thanks in advance for your wisdom.

Replies are listed 'Best First'.
Re: implicit split to @_ is deprecated
by hardburn (Abbot) on Aug 29, 2003 at 16:58 UTC

    You were close:

    my $start=(split(/ /,$data[146]))[3];

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Righteous! That's exactly it! I knew I was somewhere close! Thanks Again!
Re: implicit split to @_ is deprecated
by tcf22 (Priest) on Aug 29, 2003 at 16:58 UTC
    How about
    my $start=(split(/ /,$data[146]))[3];
Re: implicit split to @_ is deprecated
by ajdelore (Pilgrim) on Aug 29, 2003 at 17:04 UTC

    This will return the fourth element of the split:

    my $start = (split / /, $data[146])[3];

    I think that you might not be doing what you expected here:

    my $start=(split(/ /,$data[146]),3); # ^^

    The last argument to split means that it will split to a maximum of three strings. If you are trying to get the fourth element, this won't help you.

    perldoc -f split

    </ajdelore>

      your right, however it is indeed the 4th element/column that I am looking at, so we are still on target. Thanks.
Re: implicit split to @_ is deprecated
by CombatSquirrel (Hermit) on Aug 29, 2003 at 17:18 UTC
    If you are working with larger data 'lines' it might be interesting for you to use a combination of both methods:
    my $start = split(/\s+/, $data[146], 4)[3];
    This will avoid matching the delimiter more than three times and can save you some time when processing huge amounts of data in scalar variables. So it probably doesn't apply to your case. But maybe you'll find it entertaining, at least ;-)
    Cheers,
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
      It is a good idea but it won't work the way you have it. The problem is that with limit parameter, the last element contains the rest of the string. If there are more than four data fields, then the fourth element will contain all of them. Also, the parenthesis around the split are needed to force it into list context. Instead, this will work:
      my $start = (split(/s\+/, $data[145], 5))[4];
        Errr, OK, OBO error ;-) Stupid 0-indexed arrays
        Thanks
        CombatSquirrel.
        Update: Your code won't work either. Use array index 3 instead of 4 - you are getting the rest field as well.
        Entropy is the tendency of everything going to hell.