tags:

views:

129

answers:

4

I wanted to chop off all but the first five elements of an array, so I stupidly did:

@foo = @foo[ 0 .. 4 ];

and heartily praised my own cleverness. But that broke once @foo ended up with only three elements, because then I ended up with two undefs on the end, instead of a three-element array. So I changed it to:

@foo = @foo > 5 ? @foo[ 0 .. 4 ] : @foo;

This works but is kinda ugly. Is there a better idiom for saying "give me everything up to the first five elements of the array?"

+6  A: 

If you don't care about mutations (implied by the self-referential lhs @foo = something referencing @foo) use the two-argument splice(), see perldoc -f splice for more info.

splice ARRAY,OFFSET

Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any. In list context, returns the elements removed from the array. In scalar context, returns the last element removed, or "undef" if no elements are removed. The array grows or shrinks as necessary. If OFFSET is negative then it starts that far from the end of the array. If LENGTH is omitted, removes everything from OFFSET onward. If LENGTH is negative, removes the elements from OFFSET onward except for -LENGTH elements at the end of the array. If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is past the end of the array, perl issues a warning, and splices at the end of the array.

Then watch the effect:

@_ = 1..10;
splice @_, 5;
say for @_;


@_ = 1..3;
splice @_, 5;
say for @_;

If you're using warnings, and I hope you'll have to check for the length (as in Axeman's suggestion) or disable the noisy warning (splice() offset past end of array):

{
  no warnings 'misc';
  splice @_, 5;
}
Evan Carroll
I always forget about `splice`
friedo
Yea, it seems kind of circular to do `@foo = @foo[ stuff; ]`, splice will be massively faster.
Evan Carroll
Unfortunately this produces the warning "splice() offset past end of array", so you may want to add a `local $SIG{__WARN__}` when you do this, or do a length check as Axeman said.
Ether
@Ether, that isn't unfortunate, it is probably a good idea. I've updated the question with the right way to disable the warning.
Evan Carroll
+1  A: 

This isn't that graceful, but you can express it like this:

@foo[ 0..( $#foo > 4 ? 4 : $#foo ) ];

A generalized min function might look better.

use List::Util qw<min>;    
@foo[ 0..min( $#foo, 4 ) ];

But if you just want to get rid of everything else then you just need to splice off the rest:

splice( @foo, 5 ) if 5 < @foo;
Axeman
+6  A: 

You can set the last index of an array to shorten or lengthen it. Like your code you'll need to check to make sure your not creating undef elements.

$#foo = 4 if $#foo > 4;
Ven'Tatsu
Interesting take on this!! I'm not sure I like it more than `splice()`, but I do like that it is unique.
Evan Carroll
I had forgotten about that! :) +1
Axeman
Awesome. I really like this one.
friedo
N.B. Without the if, this suffers from the same "then I ended up with two undefs on the end, instead of a three-element array" problem.
ysth
@ysth, right I don't see how this answers the question. You're still using a functionality that requires explicit checking. Even if the rw-ness of $#foo is nifty. He could modify his question to `@foo = @foo[0..4] if $#foo > 4;` and achieve very close to the same thing (if we're just trying to achieve not-*kinda ugly*)
Evan Carroll
+4  A: 

Yet another way:

@foo = splice(@foo, 0, 5);

Unlike the other suggestion for splice, this doesn't trigger a warning; the 5 explicitly means "up to 5".

ysth