Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Last index use in array slice

by bart (Canon)
on Dec 03, 2012 at 13:15 UTC ( [id://1006873]=note: print w/replies, xml ) Need Help??


in reply to Re: Last index use in array slice
in thread Last index use in array slice

Alternatively, yet still using the idea of discarding part of the output:
my(undef, undef, @rest) = split/\n/;
But, with a variable value for $index, I'd rather go the splice route.

Replies are listed 'Best First'.
Re^3: Last index use in array slice
by ColonelPanic (Friar) on Dec 03, 2012 at 13:29 UTC

    Yeah, I thought of that one, but ultimately didn't use it because, as you mentioned, it doesn't work well with an arbitrary starting index.

    You could do something like this:

    my $str = "discard\ndiscard\nkeep1\nkeep2"; my $index = 2; my @arr = grep {$index-- <= 0} split "\n", $str;


    When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1006873]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2025-03-25 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (67 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.