Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl list subset range

Tags:

list

perl

In another question I was asking, I was trying to find a way to get a subset list (or array) from a list, thinking this could be done with a slice or a splice. With an array it is as trivial as:

@arr[2 .. $#arr]

to get an array that has all the same elements starting from the element at index 2 to the end (ie, skipping the first 2 elements). However, how could this be done with a list, where you cannot use something like $#arr (since lists have no names). So here's the question, is it possible to do such a thing with a list without resorting to copying to arrays or using multiple lines of code? Is there some simple notation to just extract an arbitrary length list that reaches the end of the list (without needing to know the length of the list beforehand)? In other words something like this:

(1,2,3,4,5,6,7)[1 .. -1];

(which obviously doesn't work) to get

(2,3,4,5,6,7)

does such a notation or function exist (for lists)?

like image 629
insaner Avatar asked Aug 13 '14 03:08

insaner


People also ask

What is list in Perl and its types?

Perl List and its Types. A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol whereas list variables start with @ symbol.

How to use range operator in Perl?

Range operator in Perl is used as a short way to create a list. When used with list, the range operator simplifies the process of creating a list with contiguous sequences of numbers and letters. The range operator can also be used for slicing the list.

What is the difference between list and array in Perl?

In Perl, people use term list and array interchangeably, however there is a difference. The list is the data (ordered collection of scalar values) and the array is a variable that holds the list. How to define array?

How do you access elements of a list in Perl?

The following lists are the same: If you put a list, called internal list, inside another list, Perl automatically flattens the internal list. The following lists are the same: You can access elements of a list by using the zero-based index. To access the n th element, you put (n – 1) index inside square brackets.


1 Answers

sub { @_[1..$#_] }->( 1,2,3,4,5,6,7 )
like image 79
ikegami Avatar answered Sep 20 '22 17:09

ikegami