1) Operators for Numbers

\$a = \$b; ##### Assign \$b to \$a
\$a = 7.84e45 ##### 7.84 times 10 to the 45th power
\$a = 1 + 2; ##### Add 1 and 2 and store in \$a
\$a = 3 - 4; ##### Subtract 4 from 3 and store in \$a
\$a = \$a + \$b; ##### Add \$b to \$a
\$a = 5 * 6; ##### Multiply 5 and 6
\$a = 7 / 8; ##### Divide 7 by 8 to give 0.875
\$a = 9 ** 10; ##### Nine to the power of 10
\$a = 5 % 2; ##### Remainder of 5 divided by 2
\$a++; ##### Increment \$a by one
\$a--; ##### Decrement \$a by one

Exercise: Operating with Numbers

#!/usr/local/bin/perl -w
#
# This program converts from US \$ to Canadian \$
#
print "Money value in US \\$ ";
\$us_money = <STDIN> ;
chomp \$us_money;
\$can_money = \$us_money /0.6;
print "US\\$ \$us_money = Canandian \\$ \$can_money\n";

a) In analogy to the example, write a script that asks users for the temperature in F and prints the temperature in C. (Conversion: Celsius = (F - 32) * 5/9 )

2) Strings

A string is delimited by double quotes (""). Certain special characters can be used, such as "\n" and "\t". Variables are interpreted, i.e. 'print "\$name"' prints the value of '\$name' and not \$-sign followed by 'name'. To print the characters \$, ", \, @, they must be preceded by a backslash (\).

print "hello\n";
print "hello\\n";
print "\\$5.00\n";
print "She said: \"hello\"\n";
print "\tThis is indented\n";
\$a = "apples"; \$b = "pears";
print "\$a and \$b\n";

Exercise:

a) Write Perl print statements for the following text (with the same linebreaks, etc):

Snoopy bought a CD called "Greatest Hits" on the WWW for
\$10.00. To buy the CD he had to send an email message to
orders@hits-online.net. This is what the design on the CD
cover looked like:

```       \  |  /
@ @
*
\---/
```

3) Operators and functions for strings

chop \$a; ##### removes the last character
chomp \$a; ##### removes the last character if it is a newline character
\$a = \$b . \$c; ##### concatenate \$b and \$c
\$a = \$b x \$c; ##### \$b repeated \$c times

Example:

#!/usr/local/bin/perl -w
#
# program that demonstrates operating with strings
#
\$color= <STDIN>;
chomp \$color;
\$color = \$color . "***";
chop \$color; chop \$color;
\$color = \$color . "!";
\$color = \$color x 5;
print "\$color\n";

a) Add 'print "\$color\n";' after every line so that you can see how the content of the variable is changed after each step.

b) Write a program that asks users for their favourite color. Create the following output (assuming "red" is the chosen color).

```red red red red red red red red red red
red 					 red
red 					 red
red red red red red red red red red red
```
Hints: For the first and last lines: concatenate the string with "." and use the x-operator. For the second and third line use "\t".

4) Debugging

Adding print statements for variables can be a good strategy to debug a piece of code. For example, the following piece of code is mostly nonsense. Do not try to understand it! But if you add 'print "\$color\n";' after every line, you can identify the line which changes the variable to a number. Delete that line and the output will contain another color. (This shows that it is sometimes possible to use a piece of code without fully understanding every symbol in it - as long as you know what every line does!)

\$color= "red" || die;
chop \$color if 1;
\$color = "g".\$color . "en";
0 && chomp \$color;
\$color = \$color . "!";
\$color = print "";
\$color = \$color x 5;
print "\$color\n";

5) Control structures: if

This is just a first introduction to control structures. They will be covered in more detail during the next two weeks.

#!/usr/local/bin/perl -w
#
# if statement
#
print "Do you like Perl? ";
print "That is great!\n";
}
else {
print "That is disappointing!\n";
}

Exercises

Modify the program so that it answers "That is great!" if the answer was "yes", "That is disappointing" if the answer was "no" and "That is not an answer to my question." otherwise. Use "if ... elsif ... else ...". Note that "if" and "elsif" are followed by an expression in parenthesis () and then a statement in curly brackets {}, "else" is followed only by a statement in curly brackets.