Monday, December 05, 2005

 

Perl: Arrays

The lists on the previous page weren't very useful - we need some way of
saving entire lists for later use and modification.
An array is a named list. As with lists, its space is dynamically
allocated and removed, it is 0-indexed, and shares all the operators, and
some new accessors

@ (at sign)
Refers to the entire array or slice of an array (when used in conjuction
with [ ]).
$ (dollar sign)
Refers to one element of the array, used in conjunction with [ ]

@stringInstruments = ("violin","viola","cello","bass");
@brass = ("trumpet","horn","trombone","euphonium","tuba");
$biggestInstrument = $stringInstruments[3];
print("orchestral brass: ",
join(" ",@brass[0,1,2,4] ),
"\n");

Some Array functions
in addtion to list functions
push(@ARRAY,LIST) add LIST to the end of @ARRAY
pop(@ARRAY) remove and return the last element of @ARRAY
unshift(@ARRAY,LIST) add LIST to the front of @ARRAY
shift(@ARRAY) remove and return the first element of @ARRAY
scalar(@ARRAY) return the number of elements in the array


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?