#!/usr/local/bin/perl -w
# animals in a zoo
@zoo=("monkey", "tiger", "eagle");
print "The zoo has the following ".@zoo." animals: @zoo\n";
print "Which animal would you like to add? ";
$new_animal = <STDIN> ;
chomp $new_animal;
push(@zoo, $new_animal);
$length = @zoo;
print "The zoo now has the following $length animals: @zoo\n";
print "The 3rd animal in the list is $zoo[2]\n";

Array operators and functions

a) Important ones

@zoo = ("monkey", "tiger", "eagle"); defining an array
push(@zoo,"parrot"); add elements at end of array
@zoo = ("zebra","lion", @zoo);add elements at beginning
$length = @zoo; length of @zoo
$animal = $zoo[0]; the first element of @zoo
$animal = $zoo[2]; the third element of @zoo
$animal = $zoo[@zoo-1]; the last element of @zoo
$lastelement = pop(@zoo); remove last element from @zoo
@other_zoo = reverse(@zoo); new array with reverse order
@other_zoo = sort(@zoo); new array in alphabetical order

b) Optional ones

@zoo = qw(monkey tiger eagle);
@numbers = (1 .. 5);
defining an array
$number = $#zoo; index of the last element
$animal = $zoo[-1];
$animal = $zoo[$#zoo];
the last element of @zoo
@zoo[0,1] = @zoo[1,0]; swap the first two elements


1) Create an array that contains the names of 5 students of this class. (You don't need a loop to do that. Simply assign the 5 values.) Print the array. Remove (pop) the last name from the array. Print the array. Ask a user to type in her/his name. Add (push) that name to the array. Print the array. Ask a user to input a number. Print the name that has that number as index. Add "John Smith" and "Mary Miller" at the beginning of the array. Print the array. Print the array in alphabetical and in reverse order.
2) (Optional exercise) Add
after the line that starts with "push" in the zoo program . How does it change the output?


#!/usr/local/bin/perl -w
# if statement
@zoo = ("tiger", "elephant", "monkey");
foreach $animal (@zoo){
    $animal = "_".$animal."_";
    print "$animal";
print "\n";


3) Create a second copy of the array in reverse order. Add a second foreach loop that prints the reverse copy.

4) Use the array of student names from exercise 1. Create a foreach loop that prints for each student "hello $student, how are you?".

Input from a file

Click here and save the file in your current directory under the name "alice.txt".

# Program to read and print a file
open(ALICE, "alice.txt");
@lines = <ALICE> ;
print @lines;

File handling

open(FILEHANDLE, "alice.txt");
open(FILEHANDLE, "<alice.txt");
open for input
open(FILEHANDLE, ">alice.txt"); open for output
open(FILEHANDLE, ">>alice.txt"); open for appending
print FILEHANDLE "Some text.\n"; print to an open file
open(FILEHANDLE, "alice.txt") || die "cannot open file"; open file, print error message


5) Modify the program so that the lines are printed in reverse order.
6) Output to another file instead of the screen. First, let your script overwrite the output file, then change the script so that it appends the output to an existing file.
7) Optional: Modify the program so that each line is printed with a line number at the beginning. (Hint: use a foreach loop and a counter.)
8) Optional: Print the lines with a # character at the beginning. Hints: a) what does adding "chomp @lines" change? b) Use the $" variable. c) You need to escape "#" so that it is not interpreted as a comment. d) There is a difference between 'print @lines' and 'print "@lines"'. As usual, the double quotes cause Perl to interpret the variables, such as the $" variable.

Optional: Hashs

Hashs are like arrays whose indices are arbitrary scalars or like relational database tables with two columns (a "key" and a "value" column).

# defining a hash
%relatives =("Lisa" => "daughter", "Bart" => "son", "Marge" => "mother", "Homer" => "father", "Santa" => "dog");

# to print a single element:
print "Lisa is a $relatives{Lisa}\n";

# to print an entire hash it should be converted into an array
@array = %relatives;
print "@array\n";


9) Create a second hash (such as "age") and print it.

Optional Materials

Input from STDIN can also be read into an array. The user has to type Ctrl-D (the end-of-file character) to terminate the input.

#!/usr/local/bin/perl -w
# input into an array from standard input
print "Type something ";
@something = <STDIN> ;
print "@something \n";

Alternative method of reading a file. This method should be used for files that are too large to be read into an array at once.

# Program to read and print a file
open(ALICE, "alice.txt");
while (<ALICE>){
print $_;