Slice to end of array

R

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.

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

Ronald
 
O

Oliver Gorwits

Ronald said:
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.
 
T

Tassilo v. Parseval

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
 
T

Thomas Kratz

Oliver said:
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^.-
 
A

Anno Siegel

Tassilo v. Parseval said:
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
 
P

Paul Lalli

Thomas said:
Oliver said:
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
 
X

xhoster

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
 
A

Anno Siegel

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
 
T

Thomas Kratz

Paul said:
Thomas said:
Oliver said:
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^.-
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top