Hashes or Associative Arrays - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Hashes or Associative Arrays

Description:

'profession' = professor, 'location' = UW-Parkside' Print ' ... Profession = professor. Location = UW-Parkside. Another look: %hash1 = ( 'first name' = 'Rob' ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 10
Provided by: UWPar4
Category:

less

Transcript and Presenter's Notes

Title: Hashes or Associative Arrays


1
Hashes or Associative Arrays
  • Hashes begin with
  • Hashes are similar to an array. The difference
    is that an array uses integers as index values
    but hashes uses arbitrary scalars called keys.
  • Keys are used to retrieve values from a hash

2
For example
  • An array A hash
  • Element Value Key Value
  • 0 Rob first name Rob
  • 1 professor profession professor
  • 2 UW-Parkside location UW-Park
  • For the array, you have to access the contents
    using their index , for the hash you access
    values via keys

3
Hash syntax
  • hash1 (key, element, key, element)
  • Or
  • hash1 (
  • key gt element
  • key gt element
  • )
  • Or can assign keys and values line by line in
    FASTA formatted files ftp//ftp.tigr.org/pub/data/

4
Operators for Hashes
  • Keys() returns a list of all the current keys
  • Values() returns a list of all current values
  • Each() returns key-value pair as two element list
  • Delete() removes both key and value from hash

5
A hash in action
  • hash1 (
  • first name gt Rob,
  • profession gt professor,
  • location gt UW-Parkside
  • )
  • Print Contents of hash \n
  • Foreach k (keys hash1)
  • Print k gt hash1k\n
  • exit

Output Contents of hash First name gt
Rob Profession gt professor Location gt
UW-Parkside
6
Another look
  • hash1 (
  • first name gt Rob,
  • profession gt professor,
  • location gt UW-Parkside
  • )
  • Print Contents of hash \n
  • Foreach v (values hash1)
  • Print \v now contains v\n
  • exit

Output Contents of hash v now contains Rob v
now contains UW-Parkside v now contains
professor
7
There are many actions you can perform on a hash
  • Sort the hash by its keys or values
  • Sort (keys hash1)
  • Assign keys or values of a hash to an array
  • _at_ary1 keys (hash1)
  • Assign an individual hash entry to a variable
  • var1 hash1k
  • Delete an entry from a hash
  • Delete hash1k

8
Homework
  • Write a Perl program to transcribe and translate
    a FASTA-formatted DNA sequence (write the RNA to
    one file, write the translation (in all six
    frames) to another file). Use this program on
    AF315723 available in NCBI.

9
More homework
  • Write a Perl program to identify protein
    sequences in D.radiodurans with more than 4
    cysteines. Go to TIGR FTP, look for file GDR.pep
  • Hint
Write a Comment
User Comments (0)
About PowerShow.com