Tom Kelliher, CS29
Mar. 6, 1997
List or array:
An ordered list of scalar data. Each element of the list is a separate scalar variable with an independent scalar value. Each element has an index associated with it --- its position within the list. The first element of the list has index 0. If a list has n elements, the last element has index n - 1. A list may have any number of elements.
Examples of lists:
@list1 = ("Dee", "Andy", "Mary"); @list2 = (6, 4, 8, 13); print "$list1[0]\n"; print "$list2[2]\n"; print "$#list1\n"; @list1 = (); print "$#list1\n"; $i = 0; while ($i <= $#list2) { print "$list2[$i]\n"; $i = $i + 1; }
Example program to read a list of lines from the keyboard, and print them in reverse order:
#!/usr/contrib/bin/perl -w $input = <STDIN>; $i = 0; while ($input) { $inputs[$i] = $input; $i = $i + 1; $input = <STDIN>; } $i = $#inputs; while ($i >= 0) { print "$inputs[$i]"; $i = $i - 1; } exit 0;