9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array...

29
9.1 Hash revision
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array...

Page 1: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.1

Hash revision

Page 2: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.2 Variable types in PERL

Scalar Array Hash

$number-3.54

$string"hi\n"

@array %hash

>=

>=

>=$array[0]

$hash{key}

Page 3: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.3

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 always start with a “%”:

my %hash;

Inserting values: $hash{"a"} = 5;

$hash{"bob"} = "zzz";

$hash{50} = "John";

Accessing:

you can access a value by its key:

print $hash{50}; John

Tip you can reset the hash (to an empty one) by %hash = ();

Hash – an associative array

%hash

5"a" >=

"zzz""bob" >=

"John"50 >=

Page 4: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.4

modifying :

$hash{bob} = "aaa"; (modifying an existing value)

You can ask whether a certain key exists in a hash:

if (exists $hash{50} )...

You can delete a certain key-value pair in a hash:

delete($hash{50});

Hash – an associative array

%hash

5"a" >=

"zzz""bob" >=

"John"50 >=

%hash

5"a" >=

"aaa""bob" >=

"John"50 >=

%hash

5"a" >=

"aaa""bob" >=

Page 5: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.5

To iterate over all the values in %hash

my @hashVals = values(%hash);

foreach $value (@hashVals)...

To iterate over the keys in %hash

my @hashKeys = keys(%hash);

foreach $key (@hashKeys)...

Iterating over hash elements

%hash

5"a" >=

"zzz""bob" >=

"John"50 >=

@hashVals

5 "zzz" "John"

@hashKeys

"a" "bob" 50

Page 6: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.6 References &Complex Data Structures

Page 7: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.7

References are your friends…

Page 8: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.8 Variable types in PERLScalar Array Hash

$number-3.54

$string"hi\n"

@array %hash

$reference0x225d14

@array1

%hash

@array2

@array3

Page 9: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.9

A reference to a variable is a scalar value that “points” to another variable.

[@array] creates a copy of the array and return a reference to this copy:

my @grades = (85,91,67);

my $arrayRef = [@grades];

References

91 6785

@grades

91 6785

$arrayRef

Page 10: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.10

A reference to a variable is a scalar value that “points” to another variable.

[@array] creates a copy of the array and return a reference to this copy:

my @grades = (85,91,67);

my %gradeHash;

$gradeHash{"Eyal"} = [@grades];

References

91 6785

@grades

%gradesHash

"Eyal" 91 6785

Page 11: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.11

A reference to a variable is a scalar value that “points” to another variable.

[@array] creates a copy of the array and return a reference to this copy:

my @grades = (85,91,67);

my %gradeHash;

$gradeHash{"Eyal"} = [@grades];

@grades = (100,82);

$gradeHash{"Neta"} = [@grades];

@grades = (56,99,77);

$gradeHash{"Era"} = [@grades];

%gradesHash

"Eyal"

91 6785

@grades

82100

@grades

References example

91 6785

82100

99 7756

@grades

99 7756

%gradesHash

"Eyal"

"Neta"

%gradesHash

"Eyal"

"Neta"

"Era"

Page 12: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.12

A reference to a variable is a scalar value that “points” to another variable.

\@array return a reference to the array itself. THIS MIGHT BE DANGEROUS.

my @grades = (85,91,67);

my $arrayRef = \@grades;

my %gradeHash;

$gradeHash{"Eyal"} = \@grades;

References

91 6785

@grades

$arrayRef

%gradesHash

"Eyal"

Page 13: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.13

A reference to a variable is a scalar value that “points” to another variable.

\@array return a reference to the array itself. THIS MIGHT BE DANGEROUS.

my @grades = (85,91,67);

my %gradeHash;

$gradeHash{"Eyal"} = \@grades;

@grades = (100,82);

$gradeHash{"Neta"} = \@grades;

@grades = (56,99,77);

$gradeHash{"Era"} = \@grades;

%gradesHash

"Eyal"

%gradesHash

"Eyal"

"Neta"

%gradesHash

"Eyal"

"Neta"

"Era"

91 6785

@grades

82100

@grades

References (bad) example

99 7756

@grades

Page 14: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.14

To access the data from a reference we need to dereference it:

my @grades = (85,91,67);

my $arrayRef = [@grades];

print $arrayRef; ARRAY(0x225d14)

my @arr = @{$arrayRef};

print "@arr"; 85 91 67

De-referencing91 6785

@grades

91 6785

$arrayRef

91 6785

@arr

To get the array use @{$reference}To get the array use @{$reference}

Page 15: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.15

To access the data from a reference we need to dereference it:

my @grades = (85,91,67);

my $arrayRef = [@grades];

my $firstGrade = $arrayRef->[0];

print $firstGrade; 85

De-referencing91 6785

@grades

91 6785

$arrayRef

Use ->[x] to get to the x element of the

referenced array

Use ->[x] to get to the x element of the

referenced array

Page 16: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.16

Get all the grades of Eyal:

my @EyalGrades = @{$gradeHash{"Eyal"}}

Get second grade of Neta:

my $Neta2 = $gradeHash{"Neta"}->[1];

Change first grade of Era:

$gradeHash{"Era"}->[0] = 72;

De-referencing examples%gradesHash

"Eyal"

"Neta"

"Era"

91 6785

82100

99 775672

Page 17: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.17

Get sorted grades of Eyal:

my @sortedGrades = sort(@{$gradeHash{"Eyal"}});

Push another grade to Neta:

my $grade = 97;

push (@{$gradeHash{"Neta"}},$grade);

More de-referencing examples

%gradesHash

"Eyal"

"Neta"

"Era"

91 6785

82100

99 775672

97

Page 18: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.18

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

Page 19: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.19 Class exercise 9a1. Write a script that reads a file with a list of protein names, and their levels

measured in different time points, such as:AP_000084 0.93,0.54,0.90,0.04,0.04AP_000155 0.96,0.20,0.50Store the information in a hash. The names of the proteins as hash keys, and the protein levels as referenced arrays.

a) Ask the user for a protein name and print out the sorted array of the levels measured of that protein. For example, if the user enter AP_000155, The script should print 0.20 0.50 0.96

b) Ask the user for a protein name and a protein level, and add this level as the last measurement of the appropriate protein, and print out the updated array of level.

2. Read the adenovirus genome file and build a hash of genes, where the key is the "product" name and the CDS start and end coordinates are an array referenced to by that key. Ask the user for a product and print its coordinated. For example if the user types "E1B 19K", the script should print out: "1542..2033". (note that the CDS lines appear before the

product line…)

Page 20: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.20

A reference to a variable is a scalar value that “points” to another variable.

{%hash} creates a copy of the hash and return a reference to this copy:

my %details;

$details{"phone"} = 5012;

$details{"address"} = "Swiss";

my $hashRef = {%details};

References

5012"Phone"

"Swiss""Addrs"

%details

5012"Phone"

"Swiss""Addrs"

$hashRef

Page 21: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.21

A reference to a variable is a scalar value that “points” to another variable.

{%hash} creates a copy of the hash and return a reference to this copy:

my %details;

$details{"phone"} = 5012;

$details{"address"} = "Swiss";

my %bookHash;

$ bookHash{"Eyal"} = {%details};

References

5012"Phone"

"Swiss""Addrs"

%details

5012"Phone"

"Swiss""Addrs"

%bookHash

"Eyal"

Page 22: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.22

%bookHash%bookHash

my %details;

$details{"phone"} = 5012;

$details{"address"} = "Swiss";

my %bookHash;

$bookHash{"Eyal"} = {%details};

$details{"phone"} = 6023;

$details{"address"} = "Yavne";

$bookHash{"Neta"} = {%details};

References example5012"Phone"

"Swiss""Addrs"

%details

5012"Phone"

"Swiss""Addrs"

"Eyal" 6023"Phone"

"Yavne""Addrs"

6023

"Yavne"

"Neta"

Page 23: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.23

%bookHash%bookHash

References exampleAnother way to build the same data structure:

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

Page 24: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.24

To access the data from a reference we need to dereference it:

my $hashRef;

$hashRef->{"Phone"} = 5012;

$hashRef->{"Address"} = "Swiss;

my %details = %{$arrayRef};

my @vals = values (%details);

print "@vals"; 5012 Swiss

De-referencing

To get the hash use %{$reference}To get the hash use %{$reference}

5012"Phone"

"Swiss""Addrs"

$hashRef

5012"Phone"

"Swiss""Addrs"

%details

Page 25: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.25

To access the data from a reference we need to dereference it:

my $hashRef;

$hashRef->{"Phone"} = 5012;

$hashRef->{"Address"} = "Swiss;

my $phone = $hashRef->{"Phone"};

print $phone; 5012

De-referencing

5012"Phone"

"Swiss""Addrs"

$hashRef

Use ->{key} to get the value of key in the referenced hash

Use ->{key} to get the value of key in the referenced hash

Page 26: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.26

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"

Page 27: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.27

Change Neta's address:

$bookHash{"Neta"}->{"Address"} = "Tel-Aviv";

Get all the phones:

@names= keys(%bookHash)

forach my $name (@names){

print "Phone of $name: ";

print $bookHash{$name}->{"Phone"}."\n";

}

De-referencing examples

%bookHash%bookHash

5012"Phone"

"Swiss""Addrs"

"Eyal"

6023"Phone"

"Yavne""Addrs"

"Neta"

"TelAviv"

Page 28: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.28

Referencing hash :

$hashRef = {%phoneBook};

$bookRef = \%phoneBook; (careful)

Referencing – Dereferencing HashesDereferencing hash :

%hash = %{$hashRef};

$myVal = $hashRef->{"A"};

$bookRef %phoneBook

XA

YB

ZC

$hashRef

XA

YB

ZC

%hash

XA

YB

ZC

$myVal = $hashRef->{"A"} = "X"

Page 29: 9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

9.29 Class exercise 9b1. Write a script that reads a file with a list of protein names, lengths and location

(such as in proteinLengthsAndLocation.txt ), with lines such as:AP_000081 181 NucAP_000174 104 Cyt

Stores the names of the sequences as hash keys, and use "length" and "location" as keys in an internal hash for each protein. For example:$proteins{"AP_000081"}->{"length"} should be 181$proteins{"AP_000081"}->{"location"} should be "Nuc".

a) Ask the user for a protein name and print its length and location. b) Print for each protein its name and location.

2. Read the adenovirus GenBank file and build a hash of genes, where the key is the product name: For each gene store an internal hash with two keys, one contains the protein_id and the other contains the db_xref.

1. Ask the user for a product, and print its protein_id and db_xref.2. Use the CDS line to decide whether the coding sequence is on the positive or

negative stands ("complement" before the coordinates marks a sequence coded on the negative strand). Add a key strand to the hash of each gene that contains

"+" if the coding sequence is coded on the positive strand or "-" if it is on the negative.

print all the product names of the proteins coded on the negative strand.