Examples:
"banjo", ' fiddle', "Pick'n Grin", 'Pick\
'n Grin', "ATGTTATACG", "1 + 2 + 3", ' ', ''
\
) can be used to ``escape'' special meaning
of quotes.
Exercises:
Let's see the differences between double- and single-quotes.
\
n" so that one string per line is printed.
"Pick'n Grin", 'Pick\'n Grin', "ATGC\n", 'ATGC\n', '"\t"\n', "\"\t\"\n", '"'
\
)?
Modify the script to print this out.
Note that single quotes are more literal, and it does not ``interprete'' many special or escape characters. Inside of single quotes, only two special characters can be used. What are these two?
$x = 4 ; $x = $x - 2 ;
#!/usr/bin/perl -w $message = "5 beer, please.\n"; print ($message); exit;
This program will do exactly same as the previous version without a variable:
print ("5 beer, please.\n");
=
means assignment. Distinguish it from ``eqaul'' sign in mathematical equations.
$x=$x+2
This dosn't make a mathematical sense, does it?
$numSeqsFromFish = 2 ; $SCREAMER100 = 0 ; $buZZ_bEE = 4 ;
_
$var1
is a different variable from $Var1
.
$this_variable_contains_length_of_ADH_gene_before_splicing = 589; $popSize = 100 ; $ocRate = 0.3 ;
Concatenate two DNA sequences:
#!/usr/bin/perl -w $seqA = "ATATA"; $seqB = "GCGCG"; # concatenating 2 seqs print "1st method:\t", $seqA, $seqB, "\n"; $joinedSeq = "$seqA$seqB"; print "2nd method:\t$joinedSeq\n"; $joinedSeq = $seqA . $seqB; # using concatenate operator (.) print "3rd method:\t", $joinedSeq,"\n"; exit;
#
indicates comment. Anything behind #
to the end
of the line is ignored
print "5 + 6 = ", 5+6, "\n"; print "2/3 = ", 2/3, "\n";
$joinedSeq = "$seqA$seqB";
$seqA = "${seqA}nnn";If you write "$seqAnnn", perl thinks that there is a variable called $seqAnnn, instead of adding ``nnn'' at the end of $seqA
\
n and \
t).
Try adding the following line before exit; statement to confirm this.
print 'single quotes: $seqA$seqB\n';
+, -, *, / | Arithmetic operators |
$a % $b | Modulus |
$a ** $b | Raises $a to the power of $b |
$a . $b | Concatenates two strings. |
(some operation) | The operation in the parentheses get executed first |
$a = $b | Assignment |
$a += $b | same as $a = $a + $b. |
-=, *=, /=, .= | Similar to above |
$a++, $a-- |
Incremeny, decrement by one. |
"abc" x 4 | String multiplication (results in "abcabcabcabc") |
Exercises:
Make the following perl script. Pay attention to what the opertors are doing to the variables (containers).
$a = 16; print "$a\n"; $a += 4; print "Added 4: $a\n"; $b = $a; $mod = $b % 4; print "$b mod 4 = $mod\n"; $a--; print "decremented: $a\n"; $codon = "ATG"; $codon .= " CCC"; print "$codon\n";
$popSize = 1000; # integer $probability = 0.9; # floating point $smallVal = 2.11E-2; # floating point with engineering notation $name = "Bob"; # character string
$var1 = "9"; $var2 = "10"; print $var1 / $var2, "\n";
@animals = ("camel", "llama", "owl"); @numbers = (23, 42, 69); @mixed = ("camel", 42, 1.23);
print $animals[0]; # prints "camel" print $animals[1]; # prints "llama"The elements we're getting from the array start with a $ because we're getting just a single value out of the array -- you ask for a scalar, you get a scalar.
Exercises:
What happens if you negative integer (e.g. -1 or -2) as the indices? Try printing them out.
Try following, and see the difference:
print @animals; # Array NOT enclosed in double quotes print "\n"; # interpolating an array into char string print "@animals"; # Array enclosed in double quotes print "\n";
$trees[0] = 'spruce'; $trees[1] = 'birch'; $trees[2] = 'aspen'; $trees[99] = 'alder';
This will create an array @trees
.
@animals = qw( camel llama owl );
This creates exactly same array as defined above.
You can reduce typing lots of quote marks and commas.
@seqNum = 11..15;
exactly same as:
@seqNum = (11, 12, 13, 14, 15);
@trees
array, what happens if you print 50-th element? How about printing an element with -100?
'
) or ("
)?
Try the following, and note how elements of @a
changes at each step:
@a = (1, 2, 3, 4, 5); print "orig: @a\n"; $a[4] = 10; print "1st : @a\n"; $a[2] = $a[1] ** $a[2]; print "2nd : @a\n"; $a[-2] = $a[1]; print "3rd : @a\n";
#
array tells you the index of
the last element of an array:
print $mixed[$#
mixed]; #
last element,
prints 1.23
#
array + 1 can tell you how many items
there are in an array.
$numAnimals = @animals; if (@animals < 5) { ... }
@subset = @animals[0,1]; # gives ("camel", "llama"); @subset = @animals[0..2]; # gives ("camel", "llama", "owl"); @subset = @animals[1..$#animals]; # gives all except the first element
Similar to arrays because a hash can contain multiple data (values).
Different from array because hash values are looked up (indexed) by names (keys), instead of numerical indecies in arrays.
Extremely useful, and central to Perl programming.
%fruit_color = ("apple", "red", "banana", "yellow");Following will achieve the same, but it looks nicer:
%fruit_color = ( "apple" => "red", "banana" => "yellow" );
It's like a look-up table.
$fruit_color{"apple"}; # gives "red"NOTE: the entire hash table is refered as
%fruit_color
with
%. But to access an element which is scalar, you use ``$''.
You can use expression or variable names as the key. If the key is not a simple character string, interpolation and evaluation of the expression occurs BEFORE the look-up.
$thisFr = 'apple'; print "Color of $thisFr is $fruit_color{$thisFr}\n"; # interpolation $name = 'ana'; $frCol = $fruit_color{'ban' . $name};
%sound = (); # empty hash table $sound{'ukulele'} = 'happy'; $sound{'banjo'} = 'loud';
delete $sound{'ukulele'};It removes the given key, and the corresponding value from the hash
%sound
.
@fruits = keys(%fruit_colors); # ("banana", "apple") @colors = values(%fruit_colors); # ("yellow", "red")
This is NOT OK because when you access $fruit_color{"apple"}
,
perl doesn't know if you want the first one or second one.
%fruit_color = ( apple => "red", apple => "green", banana => "yellow" );
Duplicated values are OK:
%fruit_color = ( apple => "red", banana => "yellow", lemon => "yellow", );
Type in the following program, and think what it is doing.
#!/usr/bin/perl -w my %recognitionSite = ( "EcoRI" => "G^AATTC", "HindIII" => "GG^CC" ); $recognitionSite{"AatII"} = "GACGI^C"; # adding more key/value combos to $recognitionSite{"XbaI"} = "T^CTAGA"; # the hash my @knownEnzymes = keys(%recognitionSite); print "Known enzymes: ", join(",", @knownEnzymes), "\n"; for( ; ; ) { print STDERR "Restriction Enzyme?: "; my $enzyme = <STDIN>; chomp ($enzyme); # remove the newline; print "Recognition Site = $recognitionSite{$enzyme}\n\n"; } exit;
Note: join function: join($glue, @list) takes a list of values, and ``glue'' them with $glue string and return a scalar (string).
my $var = "value";
$globalVar = "Everyone knows me.";
Create a directory with your first name inside of /scratch/compbio/hw. Then create a directory called hash in there (e.g. /scratch/compbin/hw/naoki/hash for me). After completing the following homework, please copy your 3 scripts into this directory.
species | DNA sequence |
camel | ATGCCTATTGGA |
llama | ATGACTACCGGC |
owl | ATGAAGAGGGTC |