Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

idioms

perl

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

like image 626
friedo Avatar asked Jul 27 '10 18:07

friedo


2 Answers

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;
like image 70
Ven'Tatsu Avatar answered Nov 15 '22 07:11

Ven'Tatsu


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;
}
like image 25
NO WAR WITH RUSSIA Avatar answered Nov 15 '22 07:11

NO WAR WITH RUSSIA