13r.1 revision (q&a). 13r.2 $scalar = @arr; 13r.3 multiple assignment my ($a,$b,@c,@d,@e);...
TRANSCRIPT
13r.1
Revision (Q&A)
13r.2
$scalar = @arr;
13r.3 Multiple assignment
my ($a,$b,@c,@d,@e);
$(a,$b'( = )cow','dog;)'
$(a,$b,@c( = )1,2,3,4,5;)
@e = (6,7,8,9,10);
$(a,$b@ = )e;
$a = @e;
$(a@ = )e;
@d = ($a, $b);
@e = (@c,@d);
Multiple declarations
$a='cow' $b='dog'
$a=1; $b=2; @c=(3,4,5)
@e=(6,7,8,9,10);
$a=6; $b=7
$a=5;
$a=6;
@d=(6,7)
@e=(3,4,5,6,7);
will be useful for home assignment
13r.4
use strict;
my ($inFile,$outFile) = ('D:\fight club.txt','D:\sorted.txt');
open(IN, "<$inFile") or die "cannot open $inFile";
open(OUT, ">$outFile") or die "cannot open $outFile file";
my @lines = <IN>;
my @sorted = sort(@lines);
print OUT @sorted;
close(IN);
close(OUT);
Working with files - example
© 1999 - 20th Century Fox - All Rights Reserved
13r.5
use strict;
my ($inFile,$outFile) = @ARGV;
open(IN, "<$inFile") or die "cannot open $inFile";
open(OUT, ">$outFile") or die "cannot open $outFile file";
my @lines = <IN>;
my @sorted = sort(@lines);
print OUT @sorted;
close(IN);
close(OUT);
Working with files - example
© 1999 - 20th Century Fox - All Rights Reserved
D:\> perl -w sortFil.pl "D:\fight club.txt" "D:\sorted.txt"
13r.6
Subroutines
13r.7
A function is a portion of code that performs a specific task.
Functions
Functions can have arguments and return values:
$start = substr ($str,1,4);
Arguments:(STRING, OFFSET,
LENGTH)
Return value:This function returns a string
13r.8
my $bart4today = "I do not have diplomatic immunity ;"
bartFunc($bart4today ,100);
sub bartFunc {
my ($string, $times) = @_;
print $string x $times;
}
Subroutine example
I do not have diplomatic immunity
I do not have diplomatic immunity
I do not have diplomatic immunity
I do not have diplomatic immunity
...
13r.9
$reversed = reverseComplement("ACGTTA");
A subroutine may be given arguments through the special array variable _@
Return value
sub reverseComplement {
my ($seq) = @_;
$seq =~ tr/ACGT/TGCA/;
my $revSeq = reverse $seq;
return $revSeq;
}
When a subroutine is invoked, perl begins to run it
separately from the main program
$reversed
13r.10
$reversed = reverseComplement( );
Passing arguments :
Return value
sub reverseComplement {
my ($seq) = @_;
$seq =~ tr/ACGT/TGCA/;
my $revSeq = reverse $seq;
return $revSeq;
}Arguments are passed using the special array
_@
_@
"ACGTTA""ACGTTA"
$reversed
13r.11
$reversed = reverseComplement( );
Passing arguments :
Return value
sub reverseComplement {
($seq) = @_;
$seq =~ tr/ACGT/TGCA/;
my $revSeq = reverse $seq;
return $revSeq;
}We can then read the
arguments from _@
"ACGTTA"$reversed
_@
"ACGTTA"
$seq "ACGTTA"
13r.12
$seq "ACGTTA"
$reversed = reverseComplement( );
Return value
sub reverseComplement {
my ($seq) = @_;
$seq =~ tr/ACGT/TGCA/;
my $revSeq = reverse $seq;
return $revSeq
} We can now add whatever content we want to the
subroutine
"ACGTTA"
_@
"ACGTTA"
$reversed
$seq "TGCAAT"
$revSeq "TAACGT"
13r.13
$reversed = reverseComplement( );
Returning return values :
Return value
sub reverseComplement {
my ($seq) = @_;
$seq =~ tr/ACGT/TGCA/;
my $revSeq = reverse $seq;
return $revSeq;
}
"ACGTTA"
The return statement ends the execution of the
subroutine and returns a value
$reversed
_@
"ACGTTA"
$seq "ACGTTA"$seq "TGCAAT"
$revSeq "TAACGT" "TAACGT"
13r.14
my @petArr = ('Liko','Emma','Louis');
printPets (\@petArr);
Passing references:
Passing variables by reference
sub printPets {my ($petRef) = @_;foreach my $pet (@{$petRef}) { print "Good $pet\n";}
}
@petArr
We create a reference to the array
'Emma''Liko' 'Louis'
13r.15
my @petArr = ('Liko','Emma','Louis');
printPets (\@petArr);
Passing references:
Passing variables by reference
sub printPets {my ($petRef) = @_;foreach my $pet (@{$petRef}) { print "Good $pet\n";}
}
_@
@petArr
Then we pass the reference to the
subroutine
13r.16
my @petArr = ('Liko','Emma','Louis');
printPets (\@petArr);
Passing references:
Passing variables by reference
sub printPets {my ($petRef) = @_;foreach my $pet (@{$petRef}) { print "Good $pet\n";}
}
_@
$petRef
@petArr
13r.17
my @petArr = ('Liko','Emma','Louis');
printPets (\@petArr);
Passing references:
Passing variables by reference
sub printPets {my ($petRef) = @_;foreach my $pet (@{$petRef}) { print "Good $pet\n";}
}
_@
$petRef
@petArr
Good LikoGood EmmaGood Louis
13r.18
Referencing, De-referencing
13r.19
%bookHash%bookHash
ReferencesYou can think of it as folders that contain inner folders that contains some data…
$bookHash{"Eyal"}->{"phone"} = 5012;
$bookHash{"Eyal"}->{"address"} = "Swiss;"
$bookHash{"Neta"}->{"phone"} = 6023;
$bookHash{"Neta"}->{"address"} = "Yavne;" 5012"phone"
"Swiss""addrs"
"Eyal"
6023"phone"
"Yavne""addrs"
"Neta"
$bookHash{"Eyal"}{"phone"} = 5012 ;
$bookHash{"Eyal"}{"address"} = "Swiss;"
$bookHash{"Neta"}{"phone"} = 6023 ;
$bookHash{"Neta"}{"address"} = "Yavne;"
Change Neta's address:
$bookHash{"Neta"}{"address"} = "Tel-Aviv;"
Change Eyal's phone:
$bookHash{"Eyal"}{"phone"} = 2209;
13r.20
The general structure of the data structure:
$ #bookHash{$name}{"address"} = $address
$ #bookHash{$name}{"phone"} = $phone
Get all the phones:
@names= keys(%bookHash)
foreach my $name (@names)}
print "Phone of $name;" :
print $bookHash{$name}{"phone"}."\n;"
{
De-referencing examples
13r.21
Get all the details of Neta:
my %NetaDetails= %{$bookHash{"Neta"}}
Get the phone of Eyal:
my $EyalPhone = $bookHash{"Eyal"}->{"Phone"};
De-referencing examples
%bookHash%bookHash
5012"Phone"
"Swiss""Addrs"
"Eyal"
6023"Phone"
"Yavne""Addrs"
"Neta"
13r.22
Referencing array:
$arrayRef = [@grades];
$gradesRef = \@grades; (careful)
Referencing – Dereferencing ArraysDereferencing array:
@arr = @{$arrRef};
$element1 = $arrRef->[0];
B CA
@grades$gradesRef
B CA
$arrRef
B CA
@arr
$element1 = $arrRef->[0] = A
13r.23
Referencing hash:
$hashRef = {%phoneBook};
$bookRef = \%phoneBook; (careful)
Referencing – Dereferencing Hashes - summary
Dereferencing hash:
%hash = %{$hashRef};
$myVal = $hashRef->{"A"};
$bookRef %phoneBook
XA
YB
ZC
$hashRef
XA
YB
ZC
%hash
XA
YB
ZC
$myVal = $hashRef->{"A"} = "X"
13r.24
While & Foreach
13r.25Loops: while (defined …)
Let's observe the following code :open (IN, "<numbers.txt");my $line = <IN>;while (defined $line) {
chomp $line;if ($line > 10) {
print $line;}$line = <IN>;
}close (IN);
13r.26Foreach loops
open (IN, "<numbers.txt");
my @lines = <IN;<
chomp @lines;
foreach my $num (@lines)} if ($num <= 10) }next ;
{print $num ;
{
close (IN);