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)?
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.
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.
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?
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.
sub { @_[1..$#_] }->( 1,2,3,4,5,6,7 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With