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
ParameterDescription
$ithe 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

method join(Str $sep āŸ¶ Str)

Returns the result of removing the \n between each line and replacing it with a new separtor.

ParameterDescription
$sepThe 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($_);
}
ParameterDescription
$itemThe item to add to the list

set-pos

method set-pos(Int item āŸ¶ List)

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");
ParameterDescription
$itemThe item to add to the list

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.