Hashes - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Hashes

Description:

A hash is composed of a set of key-value pairs. ... Examples of hash usage: ... For example, the hash %stoplight is populated as follows: $stoplight{red} = 'stop' ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 8
Provided by: Mitric
Learn more at: https://www.bios.niu.edu
Category:
Tags: hashes

less

Transcript and Presenter's Notes

Title: Hashes


1
Hashes
  • a hash is another fundamental data structure,
    like scalars and arrays.
  • Hashes are sometimes called associative arrays.
  • Basically, a hash associates a key with a value.
    A hash is composed of a set of key-value pairs.
  • A key is a string any collection of characters,
    generally enclosed in quotes. Any scalar can be
    a key, but they are all converted to strings.
  • A value can be almost anything the values are
    just scalar variables.
  • One hash oddity neither the keys nor the values
    is sorted or stored in a useful order. The order
    you enter hash items is not related to the order
    with which you retrieve them.

2
Why Use Hashes?
  • The C language doesnt have anything like a hash
    in it, and clearly C can do just about anything
    you need to do in programming.
  • The point of Perl is to make your life easier, to
    include useful tools, even if they are messy and
    clutter up the language.
  • Examples of hash usage
  • --keeping count of the number of times a
    word is used in a text, or that a particular
    sub-sequence appears in DNA. Use the word as the
    key and the number of appearances as the value.
  • --Associating ID numbers with peoples names
  • --Associating protein names with their
    properties.
  • And lots more. The hash is a tool that gets used
    very frequently once you understand them.

3
Hash Basics
  • The punctuation mark used to denote a hash is
    (percent sign).
  • Note that hashes, arrays, and scalars are
    completely different variables. The variables
    cat, _at_cat, and cat are all different and
    independent variables. I dont recommend using
    the same names for different variables, but it is
    legal.
  • Hash elements are accessed by enclosing the key
    in curly braces. For example, the hash
    stoplight is populated as follows
  • stoplightred stop
  • stoplightyellow caution
  • stoplightgreen go
  • In this hash, red, yellow, and green
    are the keys, and stop, caution, and go are
    the values.
  • Each key can refer to only a single value. You
    cant have duplicate keys. If you try, the first
    value will be lost and only the second will work
  • stoplightyellow speed up
  • print stoplightyellow\n
  • prints go faster
  • However, different keys can refer to the same
    value without any problem.

4
Alternative Way of Loading Hashes
  • A hash really is an array with alternating keys
    and values. You could load a hash by simply
    writing the keys and values the same way as you
    would load an array
  • stoplight (red, stop, yellow,
    caution, green, go)
  • This method is a bit annoying, because it is easy
    to lose track of keys and values. A better way
    is to use the gt operator (big arrow), which is
    really just a synonym for a comma
  • stoplight ( red gt stop,
  • yellow gt
    caution,
  • green gt go )
  • Note the use of newlines here--makes reading
    the code easier.

5
Hash Operators
  • keys gives a list of all the keys used in the
    hash. Heres a common use
  • foreach key (keys stoplight)
  • print key stands for
    stoplightkey\n
  • Note that the keys are not returned in a useful
    order. If you want them sorted you could write
  • foreach (sort keys stoplight)
  • or
  • foreach (sort a ltgt b keys
    stoplight)
  • Similarly, values gives a list of all the
    values, in some unusual order.
  • each is an operator that returns a 2 element
    list consisting of the key and the value. It
    needs to do this in tandem with while
  • while ( (key, value) each stoplight)
  • print key value\n

6
More Hash Operators
  • Removing elements in a hash is done with
    delete
  • delete stoplightred
  • both key and value are removed
  • Testing for existence with exists
  • exists stoplightred) returns true if
    that key-value pair exists, and false if it
    doesnt.

7
Counting
  • Heres a simple use of hashes to get word counts.
    Input is a file of words, one per line.
  • while (word ltSTDINgt)
  • chomp word
  • word_hashword
  • Note that word_hash was created implicitly,
    without ever being explicitly declared. This is
    a standard Perl feature, but I will shortly
    discourage its use.
  • Also, a nice feature of using hashes for counting
    is that each value is automatically set to 1 the
    first time a new key is encountered. That is,
    you dont have to initialize each key-value pair
    Perl does it for you automatically.
Write a Comment
User Comments (0)
About PowerShow.com