Hashes - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Hashes

Description:

– PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 27
Provided by: acmCseB
Category:
Tags: hashes

less

Transcript and Presenter's Notes

Title: Hashes


1
Hashes
  • What are they?
  • Why are they cool (or why arent they cool?)?
  • When would I use one vs. an array?

2
What are hashes?
  • Used to be called associative arrays, but thats
    too many syllables to say. So now we just say
    hash.
  • The word hash is from the CS concept of a hash
    table.

3
What are hashes?
  • Like an array its a collection of scalar data
    with individual elements selected by some index
    value.
  • But, the index value doesnt have to be a number.
    Instead it can be an arbitrary scalar. These
    are called keys. Each key has a corresponding
    value

4
Hashes vs Arrays
  • Key Value
  • joel grow
  • bill clinton
  • al gore
  • 2 two
  • what_the heck

Key Value 1
joel 2 bill 3
al 4
john
Array
Hash
5
More on hashes
  • Hashes are designed for very fast lookups for a
    particular element
  • The elements of a hash do not have a particular
    order.

6
Hashes
  • Hashes use the sign, instead of or _at_.
  • Hashes are initialized using a list.
  • emails ('joelg', 'Joel Grow', 'igor', 'Igor
    Stravinsky')
  • create emails with 'joelg' and
  • 'igor' as keys, and 'Joel Grow' and
  • 'Igor Stravinsky' as values

7
Accessing a hash element
  • Each element is accessed using , in a similar
    way to arrays.
  • _at_emails qw(joelg billc algore)
  • emails3 'monica' assign
  • emails ('joelg','Joel Grow','billc', 'Bill
    Clinton')
  • emails'algore' 'Al Gore' assign
  • print "emailsjoelg\n" prints 'Joel Grow'
  • print "emails'Joel Grow'\n" prints what?

8
Hash assignment
  • Assigning hashes with lists Perl will take every
    other element as a key, and every other element
    as a value
  • noises qw/dog bark cat meow sheep baa/
  • _at_foods ('carrot', 'orange', 'lemon', 'yellow',
    'orange', 'orange')
  • foods _at_foods
  • print noises'cat'."\n"
  • print foods'orange'."\n"
  • print noisessheep."\n"
  • print foods4."\n"

9
gt
  • 2 bits of syntactic sugar
  • You can use the gt syntax instead of a comma
  • You don't have to quote the key (as long as there
    aren't spaces in it)
  • emails (joelg gt 'Joel Grow',
  • billc gt 'Bill Clinton')

10
Literal representation of a hash
  • Perl doesnt really have a literal representation
    of a hash (meaning you can't print it out as a
    hash).
  • So, Perl unwinds the key/value pairs into a list.
  • For example
  • bar (Joel gt 'Grow', Al gt 'Gore')
  • _at_foo bar foo now contains
  • ('Joel','Grow','Al','Gore')
  • bar2 _at_foo bar2 now equals bar
  • or just do
  • bar2 bar

11
Examples
  • names ( Joel gt 'Grow',
  • Al gt 'Gore',
  • Bill gt 'Clinton'
  • Foo gt 7 )
  • print names'Joel', "\n"
  • print "Al names'Al'\n"
  • namesGeorge 'Dubya'
  • names'Foo'
  • print "Joel has namesFoo toes\n"

12
Swap Keys and Values
  • my names (Joel gt 'Grow',
  • Al gt 'Gore')
  • names2 reverse names
  • print names2'Gore', "\n"
  • whats going on behind the scenes
  • here? think about the array unwinding.
  • next slide will explain why...

13
Careful with reverse...
  • names ( John gt 'Smith',
  • Susan gt 'Smith' )
  • names2 reverse names
  • print names2'Smith', "\n"
  • what does this print??

14
Hash as a mapping
  • Some people refer to them as "mappings"
  • ex you want to map states to their abbreviation,
    so that you can just say directly "what is the
    abbreviation for Oregon?" instead of "loop
    through this whole list until you find Oregon"
  • to do this in Perl, setup a hash with state names
    as keys, and abbreviations as values

15
Hash Functions -- Keys
  • Keys -- yields a list of all the keys in the
    hash.
  • Perl first builds a list of the keys and then
    feeds them to you one at a time
  • foo (Joel gt 'Grow',
  • Bill gt 'Clinton')
  • _at_first_names keys foo
  • for first_name ( keys foo )
  • print "first_name\tfoofirst_name\n"

16
More on keys
  • Remember, you can NOT assume a particular order
    with keys (unless you sort them yourself)
  • However, you can assume that Perl will correctly
    keep the same key/value pair together.

17
More hash functions
  • Values-- returns a list of all the values
  • foo (Joel gt 'Grow',
  • Bill gt 'Clinton')
  • _at_last_names values foo
  • print "The guilty parties are "
  • for my lastname ( _at_last_names )
  • print "lastname, "
  • print "\n"

18
More hash functions
  • Each -- iterates over a hash. Similar to keys,
    but is more efficient because it doesnt first
    build a list of all the keys.
  • If your hash is huge, don't use keys, use each.
  • foo (Joel gt 'Grow',
  • Bill gt 'Clinton')
  • while ( (first, last) each foo )
  • print I found first last\n

19
More hash functions
  • Delete -- used to remove hash elements (different
    than assigning a key the value undef!)
  • my foo ('Joel' gt 'Grow',
  • 'Bill' gt 'Clinton')
  • delete fooJoel
  • delete fooClinton
  • what does this do?
  • fooBill undef does not delete

20
More hash functions
  • Defined -- tells whether the key has been defined
    yet.
  • my foo ('Joel' gt 'Grow',
  • 'Bill' gt undef )
  • if ( defined fooJoel )
  • print "Joel fooJoel\n"
  • if ( defined fooBill )
  • print "Bill fooBill\n"

21
More hash functions
  • Exists -- Returns true if the key exists, even if
    the value is undef! Careful
  • my foo ('Joel' gt 'Grow',
  • 'Bill' gt undef)
  • if ( exists fooJoel )
  • print "Joel fooJoel\n"
  • if ( exists fooBill )
  • print "Bill fooBill\n"

22
Careful
  • foo ( Joel gt 'Grow',
  • John gt 0,
  • Fred gt undef )
  • for my key ( keys foo )
  • print "True\n" if fookey
  • print "Exists\n" if exists fookey
  • print "Defined\n" if defined fookey
  • what does this print? (everyone hates this
  • slide the first time they see it -)

23
Empty Hash?
  • To find out if a hash is empty, can use keys in
    scalar context
  • if ( keys foo )
  • do something
  • You can actually just do
  • if ( foo )
  • do something

24
How do you print a hash?
  • Or more generally, how do you do something to all
    the elements of a hash?
  • Use a for loop on the keys of the hash
  • names ( Joel gt 'Grow',
  • Al gt 'Gore' )
  • for name ( keys names )
  • print "name\tnamesname\n"

25
Sorted Keys?
  • names (joel gt 'Grow',
  • bill gt 'Clinton',
  • al gt 'Gore')
  • for name ( sort keys names )
  • print "First name\t".
  • "Last namesname\n"

26
Final word on hashes...
  • Hashes are very powerful, and one of the Perl's
    greatest strengths.
  • "Until you start thinking in terms of hashes,
    you're not thinking in Perl."
  • They can often replace lengthy loops or complex
    algorithms.
  • Use a hash whenever you want to represent a set,
    a relation, a table, a structure, or a record.
  • When faced with a new problem, it's a good idea
    to consider a hash first.
Write a Comment
User Comments (0)
About PowerShow.com