Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Slice to end of array

Reply
Thread Tools

Slice to end of array

 
 
Ronald Fischer
Guest
Posts: n/a
 
      12-07-2005
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
 
Reply With Quote
 
 
 
 
Oliver Gorwits
Guest
Posts: n/a
 
      12-07-2005
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.
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      12-07-2005
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=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      12-07-2005
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^.-
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-07-2005
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.
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      12-07-2005
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

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      12-07-2005
(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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-07-2005
<> wrote in comp.lang.perl.misc:
> (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.
 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      12-08-2005
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^.-
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby string slice/[] w/ range, weird end behavior Gary Yngve Ruby 16 05-10-2009 05:09 PM
Why is slice ignoring my fields at the end of the string? Duane Morin Ruby 3 11-20-2006 08:37 PM
Array slice to end of array of indeterminate size niall.macpherson@ntlworld.com Perl Misc 9 05-22-2006 06:34 PM
array slice question kr Perl 3 08-13-2004 07:39 PM
slice of multidimensional array Dave Bazell Perl 2 07-24-2003 11:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57