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




    --
    Sent by mn-pg-p-e-b-consultant-3.com from siemens in field com
    This is a spam protected message. Please answer with reference header.
    Posted via http://www.usenet-replayer.com
    Ronald Fischer, Dec 7, 2005
    #1
    1. Advertising

  2. Ronald Fischer wrote:
    > Given an array @x, what is the most elegant way to get a slice from index
    > $n to the end of the array?


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

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

  3. Also sprach Ronald Fischer:

    > 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.


    That's correct.

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


    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
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Dec 7, 2005
    #3
  4. Ronald Fischer

    Thomas Kratz Guest

    Oliver Gorwits wrote:
    > Ronald Fischer wrote:
    >
    >>Given an array @x, what is the most elegant way to get a slice from index
    >>$n to the end of the array?

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


    *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

    Tassilo v. Parseval <> wrote in comp.lang.perl.misc:
    > Also sprach Ronald Fischer:
    >
    > > Given an array @x, what is the most elegant way to get a slice from
    > > index $n to the end of the array?


    [...]

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

    >
    > 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.


    Another alternative is

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

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

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 7, 2005
    #5
  6. Ronald Fischer

    Paul Lalli Guest

    Thomas Kratz wrote:
    > Oliver Gorwits wrote:
    > > Ronald Fischer wrote:
    > >
    > >>Given an array @x, what is the most elegant way to get a slice from index
    > >>$n to the end of the array?

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

    >
    > *slice* not *splice*!
    >
    > to the OP: @x[$n,$#x]
    >
    > I don't know about elegant but to me it is the obvious way.


    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

    Guest

    -mobilphone.net (Ronald Fischer) wrote:
    > 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?


    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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Dec 7, 2005
    #7
  8. Ronald Fischer

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > -mobilphone.net (Ronald Fischer) wrote:
    > > 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?

    >
    > 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.


    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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 7, 2005
    #8
  9. Ronald Fischer

    Thomas Kratz Guest

    Paul Lalli wrote:
    > Thomas Kratz wrote:
    >
    >>Oliver Gorwits wrote:
    >>
    >>>Ronald Fischer wrote:
    >>>
    >>>
    >>>>Given an array @x, what is the most elegant way to get a slice from index
    >>>>$n to the end of the array?
    >>>
    >>>
    >>>See the splice builtin's man page: `perldoc -f splice`

    >>
    >>*slice* not *splice*!
    >>
    >>to the OP: @x[$n,$#x]
    >>
    >>I don't know about elegant but to me it is the obvious way.

    >
    >
    > 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.


    Yes. That's why I tried to cancel the message. I should have posted a
    correction instead.

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


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,064
  2. kr

    array slice question

    kr, Aug 10, 2004, in forum: Perl
    Replies:
    3
    Views:
    646
  3. Duane Morin
    Replies:
    3
    Views:
    95
    Hugh Sasse
    Nov 20, 2006
  4. Gary Yngve
    Replies:
    16
    Views:
    253
    Gary Yngve
    May 10, 2009
  5. Replies:
    9
    Views:
    232
    David Squire
    May 22, 2006
Loading...

Share This Page