List
List
The List type represents strings separated by newlines \n
. It provides a familiar way of working with array-like data. However, because of the limitations of shell, you can't have discrete elements with newlines in them. You can type the List's elements by declaring a @
variable with type before it or by putting [type]
after List
.
my Int @ints = 1..10;
my List[Int] $ints = 1..10;
say @ints[0].WHAT #-> Int
Int
method Int( ā¶ Int)
The list in Int context returns .elems
my @a = <one two three>;
say +@a; #-> 3
at-pos
method at-pos(Int $i ā¶ Elem-Type)
at-pos
is the internal method called when the a list is accessed with the postcircumfix syntax [..]
. It returns data typed as the element type (Str by default).
my @list = <one two three>;
say @list.at-pos(1); #-> two
say @list[1]; #-> two
Parameter | Description |
$i | the index to return the line at |
elems
method elems( ā¶ Int)
Returns the number notional elements in the list. This is equal to 0 if the list is the empty string otherwise the number of \n
+ 1.
join
Returns the result of removing the \n
between each line and replacing it with a new separtor.
Parameter | Description |
$sep | The separator to join on |
pop
method pop( ā¶ List)
Removes a line from the end of the list
my @a = <one two three>;
@a.pop;
push
method push(Elem-Type $item ā¶ List)
Push an element onto the end of the list. If the list doesn't end in a newline one will be added before adding the new data.
my @a;
for <one two three> {
@a.push($_);
}
Parameter | Description |
$item | The item to add to the list |
set-pos
set-pos
is the internal method called which you set a list element using the postcircumfix syntax [..]
.
my @a = <one two three>;
@a[1] = "deux";
shift
method shift( ā¶ List)
Removes the first line from the list
my @a = <one two three>;
@a.shift;
say @a;
unshift
method unshift(Elem-Type $item ā¶ List)
Adds a line to the front of the list.
my @a = <one two three>;
@a.unshift("zero");
Parameter | Description |
$item | The item to add to the list |