Class 3

Send programs and assignments to
gogarten@uconn.edu

Difference between lists and arrays?

Using arrays in scalar context. Example here

See biotech server for possible answers to old assignments:

Answers:
$i= 1
$i= 2
$i= 4
$i= 44
$i= 4
$i= 7
$i= 10score and7

B) Write a script that tests your answers for A - explain any discrepancies.

#!/usr/bin/perl #-w
my $i='';
print "\$i= $i\n";
$i = 1;
print "\$i= $i\n";
$i++;
print "\$i= $i\n";
$i *= $i;
print "\$i= $i\n";
$i .= $i;
print "\$i= $i\n";
$i = $i/11;
print "\$i= $i\n";
$i = $i . "score and" . $i+3 ;
print "\$i= $i\n";
$i = $i+3 . "score and" . $i;
print "\$i= $i\n";

C) If $a =1 and $b = 2, what is the type and value of the scalar stored in $c after each statement?

$c = $a +$b;
$c = $a / $b;
$c = "$a + $b";
$c = '$a + $b';
$c = $a + $b++;
$c += $a

Answer:
$c= 3
$c= 0.5
$c= 1 + 2
$c= $a + $b
3 ($b is 3)
4

D) Write a script that tests your answers for C - explain any discrepancies.

#!/usr/bin/perl
$a=1;
$b=2;
$c = $a +$b;
print "\$c= $c\n";
$c = $a / $b;
print "\$c= $c\n";
$c = "$a + $b";
print "\$c= $c\n";
$c = '$a + $b';
print "\$c= $c\n";
$c = $a + $b++; # better use parenthesis $b is 3 at the end of this line
print "\$c= $c\n";
$c += $a ; #add the value of $a to $c and stores the ressult in $c
print "\$c= $c\n";

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)

#!/usr/bin/perl -w
print "\n\n";
@myArray = ('A', 'B', 'C', 'D', 'E');
print $#myArray; # returns highest number of field in array
print "\n";
print length($myArray[0]); # returns lenght of scalar - no idea what it does with an array
print "\n";
print $myArray[1]; #returns value in slot 1 (the 2nd entry - perl starts a 0)
print "\n";
print $n=@myArray; #one way to get the number of elements in an array
print "\n";
print reverse (@myArray); #comes in handy for DNA sequences.
print "\n";

results in the following output:

4
1
B
5
EDCBA

From Perl in a Nutshell:

length

length val
Returns the length in bytes of the scalar value val. If val is omitted, the function returns the length of $_.

Do not try to use length to find the size of an array or hash. Use scalar @array for the size of an array, and scalar keys %hash for the size of a hash.

 

F) Add all numbers between 1 and 50 using at least 3 different control structures (Check chapter 10 for the syntax of the for loop - briefly for ($i=0; $i<51; $i += 1) where $i=0 is the initialization, $i<51 is the test and $i += 1 is the increment.)
while () {};
for () {};
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);
How many other control structures can you come up with?

1) simple while loop (sorry lost my indentation)

#!/usr/bin/perl/
$sum=0;
$count=0;
while ($count <50) {
$count=$count+1; # what else could be have written?
$sum=$sum+$count; # what else could be have written?
};
print "$sum\n"

2) for loop

#!/usr/bin/perl/
$sum=0;
$count=0;

for ($count =0; $count < 51; $count++) {
#$sum=$sum+$count;
$sum += $count
};
print "$sum\n"

3) foreach on (1..50)

#!/usr/bin/perl/
$sum=0;
@array = (1..50);
foreach (@array) {
#$sum=$sum+$_;
$sum += $_;
};
print "$sum\n"

4) infinite while loop with last

#!/usr/bin/perl/
$sum=0;
$count=0;
while () {
$sum += $count;
$count+=1;
if ($count >50) {last};
}
print "$sum\n"

5) using while (defined()) #advantage: this works on arrays of different size

#!/usr/bin/perl/
$sum=0;
@array = (1..50);
$count=0;
while (defined($array[$count]))
{
$sum += $array[$count];
$count += 1;
#print "$array[$count]\t $sum\n";
};
print "$sum\n"

6) Using for loop on array:

#!/usr/bin/perl -w
$sum=0;
@array = (0..50);
$count=0;
for ($count=1; ($count<51); $count++){
$sum += $array[$count];
}
print "$sum\n";

G) Do the following statements evaluate true or false?

a) 1 true
b) 0&&1 false
c) 0||1 true
d) 45 true
e) 45-45 false
f) 45/45 true
g) 45==45 true
h) $a=45 true
i) 45 <=> 45 false (-1 0 +1 -- see script)
An example for the if () {}else {} is here (Note: no ; between {} else {})

A script that runs through everything is here

 

New Assignments:

3-1: The Sieve of Eratosthenes of Cyrene can be used to calculate prime numbers.
Additional reading is here and here
Go through principle here.

Write a script that uses the Sieve of Eratosthenes to calculate all prime numbers up to a a number $n provided by the user.
Once you have a working version, how might you be able to accelerate the script?
Try to come up with a nice representation of of the results.

To test your results, you might want to use the mudulo operation % (i.e. a percentage sign followed by a space).
$test = $array[$k] % $array[$i]; #divides the value stored in $array[$k]by the value stored in $array[$i]and assigns the remainder to $test
For example 10 % 3 is 1

3-2: read chapter 4 on subroutines

3-3: write a subroutine that takes a DNA sequence and calculates the reverse complement.
This is what you might need:

reverse ($text) reverses the order of characters e.g.:

#!/usr/bin/perl/
$text='ATGCC';
$rev= reverse $text;
print " $rev \n" ;
output CCGTA

tr/ATGC/TACG/ translates every A into a T, T into A ....
$seq =~ tr/ATGC/TACG/ # Takes the string in $seq, performs the translation and returns the result into $seq. E.G.:

#!/usr/bin/perl/
$text=$comp='ATTGTCCCCGG';
$comp =~ tr/ATGC/TACG/;
print " the complement of \$text=$text is $comp\n" ;

output: the complement of $text=ATTGTCCCCGG is TAACAGGGGCC

Additional levels of sophistication:
    Can you modify the subroutine so that it uses ambiguity codes?
    Can you modify the subroutine so that it allows the sequence to contain upper and lower case as input, but returns uppercase as output?

3-4 If time, go through class3.pl, assignment at end