Class 2
Send programs and assignments to
gogarten@uconn.edu
Grades: based on homework assignments
Check for problems (compare class 1):
- ssh?
- connecting to biotech cluster?
- basic unix commands?
- vi?
- vi and color?
One problem is that in the ssh program under windows, the ANSI setting of the ssh program produces black error messages on a dark black background. So far I have not found a good solution. There are two workarounds: look very closely at the black box that pops up, if you do something "wrong" in vi; or set the foreground color to dark gray. (gray on dark blue is sill readable).
- basic vi commands?
- which PERL?
- End of line problems?
To run PERL script:
perl myprogram.pl
In unix
chmod 755 *.pl
or
chmod a+x *.pl
turns the files with extension into pl into executables. Then
./myprogram.pl
executes the program.
DON'T RUN THINGS ON THE MASTER NODE ! (At least not things that take longer than a second)
qrsh
or
ssh node010
Check notes for class1 regarding student projects
Old Assignments:
- Read handouts on variables, <>, and chomp
- Read the above
- Install ssh on your computer
- log into your account at bbcxsrv1.biotech.uconn.edu
(if you use ssh under windows go to the "profile" menu, add a profile, THEN edit the profile, THEN use the profile - this is somewhat backwards, but it works, in the future you only need to select the profile)
- Once you logged in change your password. To do so type he command passwd <return>
- start the vi editor and create a .vimrc file that allows you to use context dependent colors (see above)
- ex1.1: Create first Perl Program- “hello, world!” [make file executable using chmod 755 *.pl]
#!/usr/bin/perl -w
print ("Hello, world!\n");
What does the -w stand for? What happens if you leave out the new line character?
You can run the program by typing ./program_name.pl, if the file containing the program is an executable.
- ex1.2 modify the program so that it asks for your name and then greets you by a phrase like "hello $your_name.
- ex2.1: Write a program that computes the circumference of a circle with a radius of 12.5. The circumference is 2[pi] times the radius, or about 2 times 3.141592654 times the radius.
- ex2.2: Modify the program from the previous exercise to prompt for and accept a radius from the person running the program.
- Email the programs as separate attachments to gogarten@uconn.edu (i.e. one email, with several independent files as attachments).
Name the attachments so that they have .pl as extension (then my mail program reconizes them as perl scripts and I can execute them directly in a terminal window).
Don't use last year's sample programs as a study guide!
Don't give up on use strict -- missing ";" are easy to identify, but keeping track of variables created somewhere along a line is more difficult.
Sample Programs are at ex1-1, ex1-3, ex2-1, ex 2-2, ex2-2mod, ex2-3
Discuss modules
New things - annotated example
Numerical Operators:
| Operator |
Associativity |
Description |
| ** |
right |
exponentiation |
| *, /, % |
left |
multiplication, division, modulus |
| +, - |
left |
addition, subtraction |
| +=,-=,*= |
|
|
| ++,-- |
none |
autoincrement/decrement |
Examples
my $x = 5 * 2 + 3; # $x is 13
my $y = 2 * $x / 4; # $y is 6.5
my $z = (2 ** 6) ** 2; # $z is 4096
my $a = ($z - 96) * 2; # $a is 8000
my $b = $x % 5; # 3, 13 modulo 5
my $d = $b++; #$b is 4, $d is 3
$d = ++$b; #$b and $d are both 5
Script is here
Binary Assignment Operators
(= **= += *= .= )
(if you want to ask is something equal to, you need to use == see below)
my $a = $a + 5; # without the binary assignment operator
$a += 5; # with the binary assignment operator
my $b = $b * 3; # without the binary assignment operator
$b *= 3; # with the binary assignment operator
my $str
$str
= $str . " "; # append a space to $str
$str .= " 1"; # same thing with assignment operator
$a = 3;
$b = ($a += 4); # $a and $b are both now 7
$a=2;
$a **= 3; # with the binary assignment operator
$a=2;
$a = $a** 3;# withou the binary assignment operator
Script here
Numeric and String Comparison Operators:
Comparison | Numeric | String |
|---|
Equal | ==
| eq
|
Not equal | !=
| ne
|
Less than | <
| lt
|
Greater than | >
| gt
|
Less than or equal to | <=
| le
|
Greater than or equal to | >=
| ge
|
logical AND in perl: &&
logical or in perl: ||
New Assignments:
-
A) What is the value of $i after each step of the following script
$i = 1;
$i++;
$i *= $i;
$i .= $i;
$i=$i/11;
$i=$i . "score and" . $i+3 ;
$i=$i +3 . "score and" . $i ;
-
B) Write a script that tests your answers for A - explain any discrepancies.
-
C) If $a =1 and $b = 2, what is the type and value of the scalar stored in $c after each statement?
(you might need to turn the warnings off to force the code to execute)
$c
= $a +$b;
$c =
$a / $b;
$c = "$a + $b";
$c = '$a + $b';
$c = $a + $b++;
$c += $a
- D) Write a script that tests your answers for C - explain any discrepancies.
E) For the following array declaration
@myArray = ('A', 'B', 'C', 'D', 'E');
what is the value of the following expressions:
$#myArray
length(@myArray)
$myArray[1]
$n=@myArray
reverse (@myArray)
- F) Add all numbers between 1 and 50 using the following control structures
while () {};
(while ($n<50){print "$n\n"; $n +=2;} where ($n<50) is the condition and {print "$n\n"; $n +=2;} is what is being done while the condition is true.
for () {};
(for ($i=0; $i<51; $i += 1) {print "$i"}where $i=0 is the initialization, $i<51 is the test and $i += 1 is the increment and {print "$i"}is what is being done.)
foreach () {}; # you first need to assign the numbers 1 through 50 to an array. An easy way to do this is to use the following assignment:
@numbers = (1..50);
(foreach $n (@numbers) {$sum+=$n;} where $n is the name of the variable that is successivly assigned the values form the list (@numbers). If $n is not specified, the variable is $_ (see here for emaple)
- G) Do the following statements evaluate true or false?
a) 1
b) 0&&1
c) 0||1
d) 45
e) 45-45
f) 45/45
g) 45==45
h) $a=45
i) 45 <=> 45
An example for the if () {}else {} is here (Note: no ; between {} else {})
- READ HANDOUT