Monday, December 05, 2005
Perl: Lists
Perl's power is assisted by it's builtin data types and the builtins that
deal with them. Lists move us up the ladder of complexity from scalars.
A list is an ordered collection of scalars. Space for lists are
dynamically allocated and removed from the program's memory. Each element
can be addressed by its integer position in the list. Lists are 0-indexed;
the first element is called "0". Typical operators include
( ) (parenthesis)
The list constructor.
, (comma)
The comma is used to separate elements of the list.
[ ] (brackets)
Brackets are used to take slices of the list.
$biggestInstrument = ("violin","viola","cello","bass")[3];
print("orchestral brass: ",
join("
",("trumpet","horn","trombone","euphonium","tuba")[0,1,2,4]),
"\n");
Some list functions
sort(LIST) return a new list, the sorted from LIST
reverse(LIST) return a new list, the reverse of LIST
join(EXPR,LIST) return a string formed by concatenating each element of
LIST joined by EXPR
split(PATTERN,EXPR) return a list formed from each substring of EXPR
bordered by PATTERN.