#!/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";
@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 |
2) (Optional Exercise) Add
$"="!";
after the line that starts with "push" in the zoo program
. How does it change the output?
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 |
6) Write a Perl script that copies one file to another file. Then change the script so that it appends one file to another 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";
#!/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);