Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to grab a slice to the end of an anonymous array in Perl?

Tags:

perl

So this has been making me go bonkers for the last half hour. Is there any way for me to grab an array slice to the end of an anonymous array? I've tried:

(split(' ',$test_line))[1..$#_]

and I've tried: (split(' ',$test_line))[1..-1]

but aggravatingly, neither of those work. I really don't want to have an extra temp variable instantiated to the intermediate array (which I don't need). And I really don't want to use an ugly and unreadable one liner (found a few of those online). Is there really no straight forward way to do this?

like image 297
Eli Avatar asked Nov 01 '10 21:11

Eli


People also ask

How will you get the Slice of array in Perl?

Array slicing can be done by passing multiple index values from the array whose values are to be accessed. These values are passed to the array name as the argument. Perl will access these values on the specified indices and perform the required action on these values. print "Extracted elements: " .

Can you slice arrays?

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array. The original array will not be modified.

What is Perl splice?

In Perl, the splice() function is used to remove and return a certain number of elements from an array. A list of elements can be inserted in place of the removed elements. Syntax: splice(@array, offset, length, replacement_list) Parameters: @array – The array in consideration.

How do you find the length of an array in Perl?

Note: In Perl arrays, the size of an array is always equal to (maximum_index + 1) i.e. And you can find the maximum index of array by using $#array. So @array and scalar @array is always used to find the size of an array.


2 Answers

A list, which is what you have in your example, can not be sliced from the end. This is mainly because lists are not proper data structures in Perl, but more a construct that the interpreter uses to move data around. So knowing that you can only slice a list from the begining, your options are to either put it in an array variable and then slice, change your algorithm to return what you want, or the following:

If you are assigning this value to something, you can use undef in each slot you dont want:

 my (undef, @list) = split ' ' => $test_line;

If you post some more code, I can revise.

Alternatively, you can use some tools from functional programming. The pair of functions drop and take can be useful to resize a list without additional variables:

sub take {
    my $n = shift;
    @_[0..$n-1]
}
sub drop {
    my $n = shift;
    @_[$n..$#_]
}

and then your example becomes

drop 1, split ' ' => $test_line;

drop 1 is also commonly called tail

sub tail {drop 1, @_}

and of course, since all of these are so short, if you wanted to inline it:

sub {shift; @_}->(split ' ' => ...)
like image 79
Eric Strom Avatar answered Oct 25 '22 14:10

Eric Strom


When the OP said slice, I thought of splice:

@allTheWordsExceptTheFirstTwo = splice @{[split' ', $test_line]}, 2;
@allExceptTheFirstAndLastTwo = splice @{[split' ', $test_line]}, 2, -2;
like image 31
socket puppet Avatar answered Oct 25 '22 14:10

socket puppet