Nov 23, 2006

Basic #7, Arrays

Assignment:
@animals = ('cats','dogs','rabbits');

Retrieval:
$animals[2] -> returns rabbits
counting starts from 0

Length:
$sum = @animals;
print $sum;
prints 3;

Last Index:
$#animals -> returns 2

Printing:
print @animals -> prints catsdogsrabbits
print "@animals" -> prints cats dogs rabbits

Adding:
push(@animals, "mouse"); -> adds "mouse" to the list

$marsupial = 'kangaroo';
push(@animals, $marsupial); -> adds "kangaroo" to the list

@predators = ("tigers","leopards");
push(@animals, @predators); -> adds "tigers" and "leopards" to the list

Remove last value:
$cute = pop(@animals); -> remove the last item from the list, and assigns it to $cute

No comments: