Python & Perl                         PL Hashing                       Scalar → Scalar,              Lists → Hashes & Hash...
Outline   ●   Scalar → Scalar Hashes   ●   Lists → Hashes & Hashes → Lists   ●   Access to Keys, Values, and Key/Value Pai...
Scalar → Scalar Hasheswww.youtube.com/vkedco     www.vkedco.blogspot.com
Scalar → Scalar Hash Construction       ## This hash maps integers to names.       ## The hash is created from a list of k...
Scalar → Scalar Hash Access        ## $cs_phd_students{1} refers to Chaitanya Gharpure        print $cs_phd_students_hash_...
Scalar → Scalar Hash Construction       ## The => operator can be used to to create a hash.        ## What appears to the ...
Scalar → Scalar Hash Construction with =>       ## the => operator can be used to to create a hash.        ## What appears...
Scalar → Scalar Hash Access       ## equivalent ways of referencing the same value       print $cs_phd_students_hash_02{st...
Lists → Hashes & Hashes → Listswww.youtube.com/vkedco   www.vkedco.blogspot.com
Lists → Hashes & Hashes → Lists            ## Hashes can be constructed from qw lists. In this             ## case, each p...
Lists → Hashes and Hashes → Lists      ## A hash can be converted to an array so long as you       ## do not care about th...
Access to Keys, Values, & Key/Value                    Pairswww.youtube.com/vkedco   www.vkedco.blogspot.com
Getting Hash Keys with KEYS()        ## keys function: keys(%hash) ­ list of keys in %hash         ## this prints out 4, 3...
Getting Hash Values with VALUES()         ## values function: values(%hash) ­ list of values in %hash         my @last_nam...
Hash Iteration with EACH()         ## each(%hash) is a hash iterator: it returns each          ## key/value pair in %hash....
Hashes of Named Arrays,           Hashes of Unnamed Arrays,               Hashes of Hasheswww.youtube.com/vkedco   www.vke...
Problem    Uses hashes to represent the table of contents    of the first three chapters of D. Maki & M.                  ...
Solution    Since it is impossible to fit large hashes onto    single slides, refer to commented source code    here.www.y...
References  ●   www.python.org  ●   http://docs.python.org/2/  ●   www.perl.org  ●   http://perldoc.perl.org/www.youtube.c...
Upcoming SlideShare
Loading in …5
×

Python & Perl: PL Hasing - Scalar -> Scalar, List -> Hashes & Hashes -> Lists, KEYS(), VALUES(), EACH(), Hashes of Named Arrays, Unnamed Arrays, & Hashes

427 views
336 views

Published on

Python & Perl: PL Hasing - Scalar -> Scalar, List -> Hashes & Hashes -> Lists, KEYS(), VALUES(), EACH(), Hashes of Named Arrays, Unnamed Arrays, & Hashes

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
427
On SlideShare
0
From Embeds
0
Number of Embeds
71
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: PL Hasing - Scalar -> Scalar, List -> Hashes & Hashes -> Lists, KEYS(), VALUES(), EACH(), Hashes of Named Arrays, Unnamed Arrays, & Hashes

  1. 1. Python & Perl PL Hashing Scalar → Scalar, Lists → Hashes & Hashes → Lists, Access to Keys, Values, & Key/Value Pairs, Hashing Named Arrays, Unnamed Arrays, & Hashes Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Scalar → Scalar Hashes ● Lists → Hashes & Hashes → Lists ● Access to Keys, Values, and Key/Value Pairs ● Hashes of Named Arrays ● Hashes of Unnamed Arrays ● Hashes of Hasheswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. Scalar → Scalar Hasheswww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Scalar → Scalar Hash Construction ## This hash maps integers to names. ## The hash is created from a list of key/value pairs.  my %cs_phd_students_hash_01 =    (    1, Chaitanya Gharpure,    2, John Nicholson,    3, Aliasgar Kutiyanawala   );www.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Scalar → Scalar Hash Access ## $cs_phd_students{1} refers to Chaitanya Gharpure print $cs_phd_students_hash_01{1}, "n";  print $cs_phd_students_hash_01{2}, "n"; print $cs_phd_students_hash_01{3}, "n"; ## print $cs_phd_students_hash_01{4}, "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Scalar → Scalar Hash Construction ## The => operator can be used to to create a hash.  ## What appears to the left of => it treated as a  ## double­quoted string. It cannot have spaces. my %cs_phd_students_hash_02 =   (    student_01 => Chaitanya Gharpure,    student_02 => John Nicholson,    student_03 => Aliasgar Kutiyanawala    );www.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Scalar → Scalar Hash Construction with => ## the => operator can be used to to create a hash.  ## What appears to the left of => it treated as a  ## double­quoted string. It cannot have spaces. my %cs_phd_students_hash_02 =   (    student_01 => Chaitanya Gharpure,    student_02 => John Nicholson,    student_03 => Aliasgar Kutiyanawala    );www.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Scalar → Scalar Hash Access ## equivalent ways of referencing the same value print $cs_phd_students_hash_02{student_01}, "n"; print $cs_phd_students_hash_02{student_01}, "n"; print $cs_phd_students_hash_02{"student_01"}, "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Lists → Hashes & Hashes → Listswww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Lists → Hashes & Hashes → Lists ## Hashes can be constructed from qw lists. In this  ## case, each pair of elements is treated as a key/value ## pair. For example, qw(a, b, c, d) contains two key/value ## a/b and c/d. my @cs_phd_student_names =    qw(Chaitanya Gharpure John Nicholson Aliasgar Kutiyanawala); ## %cs_phd_student_hash_03 includes 3 key/value pairs: ## key/value pair 01: Chaitanya/Gharpure ## key/value pair 02: John/Nicholson ## key/value pair 03: Aliasgar/Kutiyanawala my %cs_phd_students_hash_03 = @cs_phd_student_names; print $cs_phd_students_hash_03{Chaitanya}, "n"; print $cs_phd_students_hash_03{John}, "n"; print $cs_phd_students_hash_03{Aliasgar}, "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Lists → Hashes and Hashes → Lists ## A hash can be converted to an array so long as you  ## do not care about the exact order of the keys. If you do, ## you have to sort. Check here on how. my @hash_cs_phd_student_names = %cs_phd_students_hash_03; print "nArray of Names: "; print "$_ " foreach(@hash_cs_phd_student_names); print "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Access to Keys, Values, & Key/Value Pairswww.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Getting Hash Keys with KEYS() ## keys function: keys(%hash) ­ list of keys in %hash  ## this prints out 4, 3, 2 on my machine but the order of keys ## is not guaranteed. print "$_n" foreach(keys(%cs_phd_students_hash_01)); ## this sorts the keys as numbers my @sorted_keys = sort { $a <=> $b }       keys(%cs_phd_students_hash_01); print "@sorted_keysn"; ## this sorts keys as strings and prints out  ## Aliasgar Chaitanya John my @sorted_first_names = sort { $a cmp $b }    keys(%cs_phd_students_hash_03); print "@sorted_first_namesn";www.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Getting Hash Values with VALUES() ## values function: values(%hash) ­ list of values in %hash my @last_names = values(%cs_phd_students_hash_03); ## prints out "Gharpure Kutiyanawala Nicholson" print "@last_namesn"; my @sorted_last_names = sort { $a cmp $b }     values(%cs_phd_students_hash_03); print "@sorted_last_namesn";www.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Hash Iteration with EACH() ## each(%hash) is a hash iterator: it returns each  ## key/value pair in %hash. ## Chaitanya ­­> Gharpure ## Aliasgar ­­> Kutiyanawala ## John ­­> Nicholson while ( my ($key, $val) = each(%cs_phd_students_hash_03) ) {   print "$key ­­> $valn"; }www.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Hashes of Named Arrays, Hashes of Unnamed Arrays, Hashes of Hasheswww.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Problem Uses hashes to represent the table of contents of the first three chapters of D. Maki & M. th Thompsons “Finite Mathematics,” 4 Edition, McGraw-Hill.www.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Solution Since it is impossible to fit large hashes onto single slides, refer to commented source code here.www.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. References ● www.python.org ● http://docs.python.org/2/ ● www.perl.org ● http://perldoc.perl.org/www.youtube.com/vkedco www.vkedco.blogspot.com

×