Slice to end of array

Discussion in 'Perl Misc' started by Ronald Fischer, Dec 7, 2005.

  1. Given an array @x, what is the most elegant way to get a slice from index $n to the end of the array?

    My first attempt was @x[$n .. -1] failed - probably because the
    ... operator found that the rhs value was already lower than the
    lhs value and hence produced an empty range.

    I ended up with @x[$n .. $#x], which is not that bad, but maybe someone
    can come up with a better solution?

    Ronald
     
    Ronald Fischer, Dec 7, 2005
    #1
    1. Advertisements

  2. See the splice builtin's man page: `perldoc -f splice`

    regards,
    oliver.
     
    Oliver Gorwits, Dec 7, 2005
    #2
    1. Advertisements

  3. Also sprach Ronald Fischer:
    That's correct.
    This is a perfectly legitimate way of doing it and I'd do it thus
    myself. If you dislike the use of $#x, you could also do:

    reverse +(reverse @x)[0 .. $n];

    Clearly, here the cure is worse than the disease.

    You might also be interested in splice(), mentioned elsewhere in this
    thread. But note that this will remove the elements from the input array
    so it's destructive.

    Tassilo
     
    Tassilo v. Parseval, Dec 7, 2005
    #3
  4. Ronald Fischer

    Thomas Kratz Guest

    *slice* not *splice*!

    to the OP: @x[$n,$#x]

    I don't know about elegant but to me it is the obvious way.

    Thomas

    --
    $/=$,,$_=<DATA>,s,(.*),$1,see;__END__
    s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
    $_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
    '%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
    print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
     
    Thomas Kratz, Dec 7, 2005
    #4
  5. Ronald Fischer

    Anno Siegel Guest

    Another alternative is

    @x[ $n-@x .. -1];

    which is more concise, but confusing. The straight-but-narrow way is
    best here.

    Anno
     
    Anno Siegel, Dec 7, 2005
    #5
  6. Ronald Fischer

    Paul Lalli Guest

    For every problem, there is an answer which is simple, elegant,
    obvious, and wrong. This is one of them. This creates a two-element
    slice, consisting of $x[$n] and $x[-1]. Not what the OP asked for.

    Now, perhaps you should go read perldoc -f splice yourself, to see why
    it was recommended.

    Paul Lalli
     
    Paul Lalli, Dec 7, 2005
    #6
  7. Ronald Fischer

    xhoster Guest

    It is not bad in that case, but if the array you want sliced is anonymous
    (or not an array at all, but rather a list), then things are much uglier.
    Unfortunately, I don't know of a better way.

    Xho
     
    xhoster, Dec 7, 2005
    #7
  8. Ronald Fischer

    Anno Siegel Guest

    my @tail = grep -- $n < 0, qw( a b c d e f g h);

    works if $n is a variable (not a literal) which may be destroyed. It also
    takes two looks to see what it does. Using an extra variable works always
    and makes the code clearer:

    my @tail = do {
    my $i = 0;
    grep $i ++ >= $n, qw( a b c d e f g h);
    };

    Both aren't entirely satisfactory.

    Anno
     
    Anno Siegel, Dec 7, 2005
    #8
  9. Ronald Fischer

    Thomas Kratz Guest

    Yes. That's why I tried to cancel the message. I should have posted a
    correction instead.
    I only saw the first response to the OP quoting the first question and not
    the paragraph where he said he already had @x[$n..$#x].

    Not my day...

    Thomas

    --
    $/=$,,$_=<DATA>,s,(.*),$1,see;__END__
    s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
    $_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
    '%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
    print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
     
    Thomas Kratz, Dec 8, 2005
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.