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 |
Exercises
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?
Exercise
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?".
#!/usr/local/bin/perl
#
# Program to read and print a file
#
open(ALICE, "alice.txt");
@lines = <ALICE> ;
close(ALICE);
print @lines;
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 |
Exercises
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.
# 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";
Exercise
9) Create a second hash (such as "age") and print it.
#!/usr/local/bin/perl -w
#
# input into an array from standard input
#
print "Type something ";
@something = <STDIN> ;
print "@something \n";
#!/usr/local/bin/perl
#
# Program to read and print a file
#
open(ALICE, "alice.txt");
while (<ALICE>){
print $_;
}
close(ALICE);