Overview |
Last week's lab introduced you to variables - named locations in which we can
store values. We learned about the most basic type of variable in Perl, the
scalar, and that the name of a scalar always starts with In class, you learned about two more types of variable:
This lab will give us some practice with all these concepts. |
Intro: the foreach loop
|
One more thing to know before you get started is how to use a foreach loop. A foreach loop lets you repeat an action for each element of a list of elements. You might use it like this:#!/usr/bin/perl -w use strict; my @names = qw( Alice Bob Cindy ); foreach my $name (@names) { print "One of the names is $name\n"; } jpr@sulu (i211/lab3) $ ./printnames.pl One of the names is Alice One of the names is Bob One of the names is Cindy We will use |
Intro: Changing a scalar to a list with split
|
A simple way to change a scalar into a list is with the #!/usr/bin/perl -w use strict; my $food = "apple banana orange strawberry"; my @items = split / /, $food; foreach my $item (@items) { print "One of the items is $item.\n"; } jpr@sulu (i211/lab3) $ ./printfruits.pl One of the items is apple. One of the items is banana. One of the items is orange. One of the items is strawberry. Now that we have those two tools, we can get started with this lab! |
Exercise 1: Using Lists |
Your first exercise is to write a simple program that should
give you some practice in creating and manipulating lists. Do this:
Create a program called double_list.pl that does the following:
jpr@sulu (i211/lab3) $ ./double_list.pl Enter some numbers => 1 2 3 4 5 12 Here are your numbers, doubled : 2 4 6 8 10 24 jpr@sulu (i211/lab3) $ |
Exercise 2: Using Hashes |
This exercise should give you some practice using hashes, an immensely useful data type that tracks associations between variables. Other languages call hashes "associative arrays" or "associative lists" or "alists", so you'll sometimes see these used in Perl as well. An important thing about hashes is that the keys in hashes are unique - each key may only appear once. This can come in handy in a lot of different situations, including this one. Do this:
Write a program called Hints:
jpr@sulu (i211/lab3) $ ./unique.pl Enter some words => apple apple banana orange banana qwerty smooth qwerty Unique words are banana qwerty apple smooth orange jpr@sulu (i211/lab3) $ ./unique.pl Enter some words => apple apple apple apple Unique words are apple jpr@sulu (i211/lab3) $ |
Making change |
Time to tie everything together. Let's write a program that performs a semi-useful
function - counting change.jpr@sulu (i211/lab3) $ ./makechange.pl Give me a list of coins => quarter dime quarter nickel penny penny You have 0 dollar(s) and 67 cent(s). jpr@sulu (i211/lab3) $ ./makechange.pl Give me a list of coins => quarter quarter dime quarter dime quarter penny You have 1 dollar(s) and 21 cent(s). jpr@sulu (i211/lab3) $ Here are some hints:
Do this:
Write makechange.pl . First try to create a version that just prints
out the number of cents the person has (e.g. "You have 121 cents"). Once
you have that version done, figure out how to separate the dollar amount from the
penny amount (e.g. "You have 1 dollar(s) and 21 cent(s)").
|
Turn in your assignment |
As usual, let's make a tarball of your assignment files (make sure they are ALL in the lab3 directory!) For this lab, you should have the following files:
jpr@sulu (i211/lab3) $ cd .. jpr@sulu (i211) $ tar czv lab3 > lab3.tar.gz double_list.pl unique.pl makechange.pl jpr@sulu (i211) $ You should then download this Okay, that's it! Have a good weekend. |