Dr. David J. Ritchie, Mr. Jim Skrine, Mr. Bruce Webber
Washington Junior High Computer Club
April 15, 1998
Summary
This is the second of several handouts for a short course in learning how to program in a computer language called "perl".
This handout will cover features of the language in Parts 1 and 2. The treatment will be very schematic because it is assumed that you are trying out the features as you go along.
Perl itself will be your teacher. For that reason, I am trying not to get bogged down in the details. The material below will be approximately correct but may be slightly incorrect on the details. It is better that we get on with trying to learn the language and over time we will become familiar with the details.
In Part 3, it will cover the beginnings of our trying to understand how to write the "hangman spelling game" program.
Examples of four pieces of scaler data are: 1, 2, 3.14, abcd
Perl treats them all as if they are double precision real numbers so that even if you write an integer like 5 internally perl will be treating it as if it is 5.000000...
When you enter a number, you need a convention for how a number is written. This is called a literal. Sometimes it seems so obvious that you don't even realize you are using a convention. You have seen something of the convention for numbers above.
To write 1 Million, you write 1e6. To write 1 millionth, you write 1e-6. The e stands for "exponent" to which 10 must be raised. So 1e6 is 1*10**6 to use a different literal notation. What is -1.25e6? What is -3.1e-6?
Similarly, integers are written as literals in the way that you have been taught: 3, -3, 4, 20, -2000. These are all examples of integer literals.
What about octal and hexadecimal numbers? The literal format for an octal number is a number that starts with a leading zero. That is, 0777 is an octal number. Similarly, -0377 is an octal number.
Hexadecimal literal format is designated as 0x33 or 0X33. What would 0xf be if it were converted to a decimal format?
What about literal representations of strings? The single or double quotes are used. A string is represented literally as `abcd' or "abcd". To make a literal string which has a quote in it, we quote the quote: `abc''d' is a string with the three characters a, b, and c, followed by the character ` followed by the character d. You can also use the forward slash (/) to quote the quote. That is, `abc''d' could also be represented as `abc/'d'.
The double quotes act in similar fashion but they allow the computer to substitute certain things when it sees them. For example "abc/td" would be a string with the characters a, b, and c. Then, the string would contain a tab followed by a d.
Scalar operators are the means to do this. Some are so obvious it is hard to understand that an operator is being defined. For example, to add the literal 5 to the literal 6, you write 5 + 6! Duh! Similarly obvious things apply for subtraction, multiplication, division, and so on.
Now, how do you join together string `abcd' and string `xyz'? You use the ".". That is, you use the period. It is `abcd'.'xyz' and that represents the string `abcdxyz'.
But, wait, I want to do more than write literals! How do I do that? That's next!
A scalar container is a symbol that begins with a $ followed by a letter and then by any number of letters, numbers, or underscores (up to 256 total). So a scalar variable is $a, $fred, $sd203 but not $203!
You can store any of the previously discussed scalars in such a variable. Once you have done that, you can do various operations.
Some examples:
Assignment:
$a = 17 ; # assigns the literal 17
to the scalar variable $a.
$b = $a + 5 ; # assigns the sum of the value in $a and the literal 5 to $b
$a += 5 ; # which adds 5 to the value in $a and assigns the result to $a.
Note that case matters. $A is different from $a.
Some examples:
$a = <STDIN> ; # puts the next
line of input into the variable $a
$b = $a + <STDIN> ; # adds the input to the contents of $a and assigns
it to $b
Some examples:
print ("Your result is $a"); # what
will be printed if the value of $a is 17?
print "Your result is $a";
This second part focuses on collections of elements. For this reason, the word "list" is used. Lists are stored in arrays.
One example of list data is: 1, 2, 3.14, abcd
When you enter a list, you need a convention for how the list is written. This is called a literal. You have seen an informal convention in the example above.
The following are examples of literal lists and their meaning:
(1, 2, 3) - the list of the numbers 1, 2, and 3.
("fred", 4.1, 5) - the list of the string "fred", the number 4.1 and the number 5.
($a, $b) - the list of the values contained in the scalar variables $a and $b() - the null list (that is, the list with no elements).
(1 .. 5) - the list 1, 2, 3, 4, 5
Some examples:
@a - the array variable @a
@another - the array variable @another
@a = (1, 2, 3) ; # stores the list 1, 2, 3 into the array variable @a.
Note that @a is different from $a.
Examples:
@fred = (7, 8, 9) ; # puts the lists 7, 8, and 9 into the array variable @fred.
$b = $fred[0] ; # puts the first element of the list stored in @fred into $b.
$c = $fred[-1]; # puts the last element of the list stored in @fred into $c.
Examples:
push (@fred, 10) ; # adds the scalar literal 10 to the end of the list @fred
pop (@fred) ; # removes the last element of the list stored in @fred.
unshift(@fred, 10) ; # adds the scalar literal 10 to the beginning of the list @fred
shift(@fred) ; # removes the first element of the list stored in @fred.
reverse (@fred) ; # try this. What does it do?
sort (@fred) ; # try this. What does it do?
chomp(@fred) ; # how about this?
So, choose a partner and play the spelling game "Hangman" but write down each and every step you do.
Then, try to identify the operations that are done to that data.