Presentation is loading. Please wait.

Presentation is loading. Please wait.

13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);

Similar presentations


Presentation on theme: "13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);"— Presentation transcript:

1 13r.1 Revision (Q&A)

2 13r.2 $scalar = @arr;

3 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

4 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 = ; my @sorted = sort(@lines); print OUT @sorted; close(IN); close(OUT); Working with files - example © 1999 - 20th Century Fox - All Rights Reserved

5 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 = ; 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"

6 13r.6 Subroutines

7 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

8 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...

9 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

10 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" $reversed

11 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"

12 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"

13 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"

14 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'

15 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

16 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

17 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 Liko Good Emma Good Louis

18 13r.18 Referencing, De-referencing

19 13r.19 %bookHash References You 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;

20 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

21 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 5012"Phone" "Swiss""Addrs" "Eyal" 6023"Phone" "Yavne""Addrs" "Neta"

22 13r.22 Referencing array : $arrayRef = [@grades]; $gradesRef = \@grades; (careful) Referencing – Dereferencing Arrays Dereferencing array : @arr = @{$arrRef}; $element1 = $arrRef->[0]; BCA @grades $gradesRef BCA $arrRef BCA @arr $element1 = $arrRef->[0] = A

23 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"

24 13r.24 While & Foreach

25 13r.25 Loops: while (defined …) Let's observe the following code : open (IN, "<numbers.txt"); my $line = ; while (defined $line) { chomp $line; if ($line > 10) { print $line; } $line = ; } close (IN);

26 13r.26 Foreach loops open (IN, "<numbers.txt"); my @lines = ; chomp @lines; foreach my $num (@lines) { if ($num <= 10) { next; } print $num; } close (IN);


Download ppt "13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);"

Similar presentations


Ads by Google