11.1 variable types in perl scalararrayhash $number -3.54 $string "hi\n" @array %hash...
Post on 19-Dec-2015
231 views
TRANSCRIPT
11.1 Variable types in PERL
Scalar Array Hash
$number-3.54
$string"hi\n"
@array %hash
$array[0]
$hash{key}
11.2
An associative array (or simply – a hash) is an unordered set of pairs of keys and
values. Each key is associated with a value. A hash variable name starts with a “%”:
my %hash;
Initialization:
%hash = ("a"=>5, "bob"=>"zzz", 50=>"John");
Accessing:
you can access a value by its key:
print $hash{50}; John
modifying:
$hash{bob} = "aaa"; (modifying an existing value)
adding:
$hash{555} = "z"; (adding a new key-value pair)
Hash – an associative array
%hash
5"a"
"zzz""bob"
"John"50
"aaa"
%hash
5"a"
"aaa""bob"
"John"50
"z"555
11.3
It is possible to get a list of all the keys in %hash
my @hashKeys = keys(%hash);
Similarly you can get an array of the values in %hash
my @hashVals = values(%hash);
Obtaining a list of the elements in a hash
@hashVals
5 "John" "zzz"
@hashKeys
50 "bob" "a"
%hash
5"a"
"zzz""bob"
"John"50
11.4
You can use combinations of hashes (and arrays) together to construct more
complex data structures.
If the information is best represented in two levels it is useful to use a hash within a
hash:
my %hash;
$hash{Key_level_1}{Key_level_2};
Hash within Hash
11.5
Example: each name in the phone book, has both phone number and address:my %phoneBook;
$phoneBook{"Eyal"}{"Phone"} = "09-9545995";
$phoneBook{"Eyal"}{"Address"} = "115 Menora St., Hulun";
$phoneBook{"Ofir"}{"Phone"} = "054-4898799";
$phoneBook{"Ofir"}{"Address"} = "31 Horkanus St., Eilat";
Hash within Hash
%phoneBook
"Eyal"
"Ofir"
"Dudu"
"09-9"…"Phone"
"Hulun""Addrs"
"054"…-"Phone"
"Eilat""Addrs"
9245"Phone"
TAU"Addrs"
%phoneBookNAME => {Phone => PHONE Address => ADDRESS}
Key I: name Key II: dataType CorrespondingValue
11.6
Assignment:my %phoneBook;
$phoneBook{"Eyal"}{"Phone"} = "09-9545995";
$phoneBook{"Eyal"}{"Address"} = "115 Menora St., Hulun";
Access: print $phoneBook{"Eyal"}{"Phone"}; 09-9545995
Modify:$phoneBook{"Eyal"}{"Phone"} = 03-6407963;
Hash within Hash
%phoneBook
"Eyal"
"Ofir"
"Dudu"
"09-9"…"Phone"
"Hulun""Addrs"
"054"…-"Phone"
"Eilat""Addrs"
9245"Phone"
TAU"Addrs"
%phoneBookNAME => {Phone => PHONE Address => ADDRESS}
11.7
More Complex Data Structures
11.8
“How to keep the phone number, address and list of grades for each student in a course?”
$phoneBook{"Eyal"}{"Phone"} = 7693;
$phoneBook{"Eyal"}{"Address"} = "34 HaShalom St.";
$phoneBook{"Eyal"}{"Grades"}[0]= 93;
$phoneBook{"Eyal"}{"Grades"}[1]= 72;
$phoneBook{"Eyal"}{"Grades"}[2]= 87;
print $phoneBook{"Eyal"}{"Grades"}[2];
87
Array within hash within hash…
%phoneBookNAME => {"Phone" => PHONE "Address" => ADDRESS "Grades" => [GRADES]}
%phoneBook
"Eyal"
"Ofir"
7693"Phone"
"34"…"Addrs"
"Grades" 72 8793
9245"Phone"
TAU"Addrs"
"Grades" 82 10090
11.9
An alternative way to insert the array of grades:
$phoneBook{"Eyal"}{"Phone"} = 7693;
$phoneBook{"Eyal"}{"Address"} = "34 HaShalom St.";
@grades = (93,72,87);
$phoneBook{"Eyal"}{"Grades"} = [@grades];
print $phoneBook{"Eyal"}{"Grades"}[2];
87
Array within hash within hash…
%phoneBookNAME => {"Phone" => PHONE "Address" => ADDRESS "Grades" => [GRADES]}
%phoneBook
"Eyal"
"Ofir"
7693"Phone"
"34"…"Addrs"
"Grades" 72 8793
9245"Phone"
TAU"Addrs"
"Grades" 82 10090
The will create a copy of @grades
11.10
How would you make a matrix?
my @matrix;
matrix[0][0] = 23;
matrix[0][1] = 5;
...
How would you make a 3D matrix??
How would you make a array of hashes???
To Infinity and Beyond!!
11ex.11
%genesPRODUCT => {"protein_id" => PROTEIN_ID "strand" => STRAND "CDS" => [START, END]}
%genesPRODUCT => {"protein_id" => PROTEIN_ID "strand" => STRAND}
%genesPRODUCT => {"protein_id" => PROTEIN_ID}
Class exercise 11a1. Read the adenovirus genome file and build a hash of genes, where the key is the "product" name: For each gene store a hash with the protein ID. Print all keys (names) in the hash.
2. Add to the hash the strand of the gene on the genome: “+” for the sense strand and “-” for the antisense strand. Print all antisense genes.
3. Add to the hash an array of two coordinates – the start and end of the CDS. Print genes shorter than 500bp.
4*. Print the product name of all genes on the sense strand whose CDS spans more than 1kbp, and all genes on the antisense strand whose CDS spans less than 500bp.
11.12
Subroutines
11.13
A subroutine is a user-defined function. Subroutine definition:
sub SUB_NAME {# Do something
...
}
Note:
Subroutine definitions may be placed anywhere in a script, but they are
usually placed together at the beginning or the end.
Subroutines
For example:
sub printHello {
print "Hello world\n";
}
11.14
To invoke (execute) a subroutine:
SUB_NAME(PARAMETERS);
Subroutines
For example:
printHello();
Hello world
print reverseComplement("GCAGTG");
CGTCAC
11.15
Code in a subroutine is reusable (i.e. it can be invoked from several points
in the script, no code duplication)
e.g. a subroutine that reverse-complement a DNA sequence A subroutine can provide a general solution that may be applied in
different situations.
e.g. read a FASTA file
Why use subroutines?
11.16
Encapsulation: A well defined task can be done in a subroutine, making the
main script simpler and easier to read and understand.
For example:
$seq = readFastaFile($fileName); # reads a FASTA sequence
$revSeq = reverseComplement($seq); # reverse complement
the
sequnce
printFasta($revSeq); # prints the sequence in
FASTA format
Why use subroutines? - Example
11.17
A subroutine may be given arguments through the special array variable @_:
sub bartFunc {
my ($string, $times) = @_;
print $string x $times;
}
my $bart4today = "I will not eat things for money\n";
bartFunc($bart4today ,100);
I will not eat things for money
I will not eat things for money
I will not eat things for money
I will not eat things for money
I will not eat things for money
I will not eat things for money
I will not eat things for money
Subroutine arguments
11.18
Definition:
sub reverseComplement {
my ($seq) = @_;
$seq =~ tr/ACGT/TGCA/;
$seq = reverse $seq;
return $seq;
}
Usage:
my $revSeq = reverseComplement("GCAGTG"); CACTGC
Notes: The return function ends the execution of the subroutine and returns a value. If there is no (explicit) return statement, the value of the last statement in the subroutine is returned.
Return value
11.19
A subroutine may also return a list value:
sub integerDivide {
my ($a,$b) = @_;
my $mana = int($a/$b);
my $sheerit = $a % $b;
return ($mana,$sheerit);
}
my ($myMana,$mySheerit) = integerDivide(7,3);
print "mana= $mana, sheerit= $sheerit";
mana= 2, sheerit= 1
Return value a list
11.20
When a variable is defined using my inside a subroutine:
* It does not conflict with a variable by the same name outside the subroutine
* It’s existence is limited to the scope of the subroutine
sub printHello {
my ($name) = @_;
print "Hello $name\n";
}
my $name = "Yossi";
printHello("Moshe");
print "Bye $name\n";
Note:
This effect also holds for my variables in any other “block” of statements in curly brackets – {…}
(such as in if-else controls and in loops)
Variable scope
Hello MosheBye Yossi
11ex.21Class exercise 11b
1. Write a subroutine that takes two numbers and prints their sum to thescreen (and test it with an appropriate script!)
2. Write a subroutine that takes two numbers and return a list of their sum, difference, and average.For example:@arr = numbersFunc(5,7);print "@arr"; 12 -2 6
3. a. Write a subroutine that takes a sentence and returns the last word. b.* Return the longest word!