开发者

Perl idiom for getting a maximum number of elements in an array

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 ch开发者_JS百科anged 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?"


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;


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;
}


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜