COMP364              Lecture 7                            Friday,   January 18/2008

    Announcements

HW1 due today (now)
Will put “passed-in” page on web
Another good text you can download is

“ Perl Programming for Biologists” by D. Curtis Jamison

Note: Can use both texts to prepare for class. Jamison explains more explicitly the different commands, and gives more details about topics in the text.
 A few 'typos'  found in Jamison, though.

         Today
More on numbers (Chapt  3 of text, Chapt 2 of Jamison)
Control flow (Chapt 3 of text , Chapt 4 of Jamison)
  -conditions
  -branching
  -looping

     Last class
Operations on numbers.

A few more examples:

Inside  Nano,  type :

$Num1 = 10;
print "$Num1\n";
$Num2 = $Num1 * 13; 
print "$Num2\n";
$Num3 = $Num2 / 5;
print "$Num3\n";
$Num1 += 10;
(the $Num1 += 10  means   Num1 (assigned a value 10 already) + 10 =20.  That is Num1 = Num1 +10=20
 This is just an explanation;not part of what you type inside Nano).

print "$Num1\n";

$Num1--;
( $Num1--  implies Num1= Num1 -1 . Now Num1 has a value 20. So Num 1-1 =  20 -1 = 19. Again this is not part of what you type inside Nano; is just an explanation).

print "$Num1\n";

  After running the command you will obtain this output:

10
130
26
20
19

A program to compute the length of hypotenuse

Consider this right triangle with sides a,b and c. C is the hypotenuse.

c

b

   a

To compute the side c, we have the following:

                                        C= √a2 + b2  

Inside Nano again type the following:

print "Input a: ";

$a = <>; (This command takes one line of input from the keyboard)
print "Input b: ";
$b = <>;
$c = sqrt($a*$a + $b*$b);

(Another way of writing the length of the hypotenuse above is)

$c2 = ($a**2 + $b**2)**(0.5);

(It will actually give the same answer ie. the output for $c and $c2 will be the same)

print "The length of the hypotenuse is $c.\n";
print "The length of the hypotenuse is $c2.\n";

(After running the commands, you will be prompted to enter  some inputs for side a and b from the keyboard).

(Type inside Nano these inputs:)

Input a: 3
Input b: 4
You shall obtain these output:
The length of the hypotenuse is 5.
The length of the hypotenuse is 5.

(Re-run the commands and  you will be asked to enter inputs for a and b again. Enter):
Input a: 5
Input b: 7

The output from this will be:

The length of the hypotenuse is 8.60232526704263.
The length of the hypotenuse is 8.60232526704263.

(Then type these inputs):
Input a: 1
Input b: 1

(The output will be)

The length of the hypotenuse is 1.4142135623731.
The length of the hypotenuse is 1.4142135623731.

(Next inputs)

Input a: 1
Input b: 2

(The output is)

The length of the hypotenuse is 2.23606797749979.
The length of the hypotenuse is 2.23606797749979.

Perl interpreting strings as numbers

Perl tries to find a number at the start of a string. It ignores blanks or 'white spaces' tabs, etc.
Examples:
“ 12” (Note there is a space between the double quotes and 12). Perl reads it as 12.
“12 tea” .Perl reads this as 12.
“ted 12” .Perl reads it as 0 (zero).

It it cannot find a number at the start of a string, it reads the character as zero(0).
An illustration of the above:
(Enter these as inputs)
Input a: ted
Input b: apple
(You will obtain this output:)

The length of the hypotenuse is 0.
The length of the hypotenuse is 0.
(Enter these inputs)
Input a: 3ted
Input b: 4apple

(Output is: )

The length of the hypotenuse is 5.
The length of the hypotenuse is 5.

(more inputs)
Input a:     3ted ted3
Input b:  4 0

(output)

The length of the hypotenuse is 5.
The length of the hypotenuse is 5.

Control flow

Allows execution of different commands based on inputs or previous calculations.
eg. Takes two numbers as inputs and tells which is bigger or if the two numbers are equal.

Demonstrating the "if " construct

Open Nano and type the following commands:
print "Input number 1: ";
$Num1 = <>;
chomp $Num1;

print "Input number 2: ";

$Num2 = <>;
chomp $Num2;

if ( $Num1 < $Num2 ) {
	print "$Num1 is less than $Num2.\n";
}
if ( $Num1 > $Num2 ) { print "$Num1 is greater than $Num2.\n"; }
if ( $Num1 == $Num2 ) {
	print "$Num1 and $Num2 are equal.\n";
}

If the condition in brackets is true, the commands in the parentheses are executed. 

(You will be prompted to enter inputs from the keyboard. Enter the following):
Input number 1: 10
Input number 2: 20

(The output will be)

10
 is less than 20
.

(Output   not written out nicely  because they are on two separate lines.)
 If you want output to be on the same line use the 'chomp' command for $ Num1 and$ Num 2  as below):

print "Input number 1: ";
$Num1 = <>;
chomp $Num1;

print "Input number 2: ";
$Num2 = <>;
chomp $Num2;

if ( $Num1 < $Num2 ) {
        print "$Num1 is less than $Num2.\n";
}
if ( $Num1 > $Num2 ) { print "$Num1 is greater than $Num2.\n"; }
if ( $Num1 == $Num2 ) {
        print "$Num1 and $Num2 are equal.\n";
}

(After re-running the above commands, you will be prompted to enter inputs as before. Enter these inputs).
Input number 1: 10
Input number 2: 20

( You obtain this as output):

10 is less than 20. (Output now nicely written out).
(“Chomp” replaces the “\n” from the end of the line.

(Comparing other inputs)
Input number 1: 23
Input number 2: 11
(Using inputs 23 and 11 ), you will obtain:

23 is greater than 11.

(Enter 112 and 112 as new inputs)
Input number 1: 112
Input number 2: 112
(Output gives)

112 and 112 are equal.

(Next enter 12 and 23 as  inputs)
Input number 1: 12
Input number 2: 23
(Output is)

12 is less than 23.

(Next inputs are 34 and 14)
Input number 1: 34
Input number 2: 14

(Output gives)

34 is greater than 14.

(Lastly, enter 5 and 5 as inputs)

Input number 1: 5  
Input number 2: 5

(You shall have this output)
5 and 5 are equal.

Demonstrating the  'if-else'  constructs

Similar to the 'if' statement. Within the 'else' statement, you can have different commands.

An example:

Open the Nano editor and type:

print "Input number 1: ";
$Num1 = <>;
chomp $Num1;

print "Input number 2: ";
$Num2 = <>;
chomp $Num2;

if ( $Num1 < $Num2 ) {
 print "$Num1 is less than $Num2.\n";
} else {
 if ( $Num1 > $Num2 ) { 
  print "$Num1 is greater than $Num2.\n"; 
 } else {
  print "$Num1 and $Num2 are equal.\n";
 }
}

Run the commands as before; you will be prompted to enter some inputs.

(Enter 112 and 112 as inputs)
Input number 1: 112
Input number 2: 112

(Output gives)

112 and 112 are equal.

( Next enter 12 and 23 as  inputs)
Input number 1: 12
Input number 2: 23
(Output is)

12 is less than 23.

( Next inputs are 34 and 14)
Input number 1: 34
Input number 2: 14
(Output gives)

34 is greater than 14.
(Lastly, enter 5 and 5 as inputs)
Input number 1: 5  
Input number 2: 5

(This will be the output)
5 and 5 are equal.

The "while" loop
While ([condition]) {[commands]}. If the condition is true, the command is executed again and again as many times you want. 
A 'countdown to zero' example:
Start counting down from 7:
Inside Nano, type the following commands:
print "Start count at: ";
$Start = <>;
chomp $Start;

$Count = $Start;
while ( $Count >= 0 ) {
  print "$Count\n";
  $Count--;
}

You will be asked inside the terminal to enter an input. Enter 7.
You will see this output:
7
6
5
4
3
2
1
0

Then enter 500 as the new input.
You will obtain this output.(Is very long so it has been shorten).
500
499
498

...
...
..

8
7
6
5
4
3
2
1
0

Next enter 10000 as the new input.
You will see this as output:(Output has been shorten,because it is too long)

10000
9999
9998
9997

....
...
..

5
4
3
2
1
0

                                     Some numeric conditions

Numbers	 Tests	
$a = = $b	Equality	
$a >$b	Greater than	
$a <$b	Less than	
$a > = $b	Greater than or equal to	
$a < = $b	Less than or equal to	
$a! = $b	Not equal to	
$a < = > $b	+1 if $a > $b ; 
-1 if $a < $b;
0 if $a=$b	

Note: $a is true if i ≠ zero.
      $a is false if = zero.

Logical formula
! not
&& and
\\ or

An example:
If (!($ Age ≥ 0) && ($Age ≤ 140 ))