Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Last index use in array slice

by ColonelPanic (Friar)
on Dec 03, 2012 at 09:23 UTC ( #1006827=note: print w/replies, xml ) Need Help??

in reply to Last index use in array slice

Another way to do it: instead of splitting everything and then ignoring some of it, find and split only the stuff you want.

use strict; use warnings; my $str = "discard\ndiscard\nkeep1\nkeep2"; my $discard = 2; my @arr = ($str =~ /^(?:[^\n]*\n){$discard}(.*)/s ? split /\n/, $1 : ( +) ); print "@arr";

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

Replies are listed 'Best First'.
Re^2: Last index use in array slice
by bart (Canon) on Dec 03, 2012 at 13:15 UTC
    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.

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006827]
LanX has a friend in Berlin who switched to go(lang) and just got a kid ...
[marto]: good morning all
[Corion]: Hi marto ;)
[Discipulus]: Corion ah! you opened a new search path! thanks
[choroba]: LanX No, but they grow and want more!
LanX was conditioned by a female
Discipulus my 'kids' always complain with our car..
[LanX]: true, cars are bad in growing
[marto]: WaitWindow()
[Corion]: Discipulus: You need to respond with 0 to WM_QUERYENDSESSION

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-01-17 09:48 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (154 votes). Check out past polls.