1 / 13

CIT 383: Administrative Scripting

Hashes. CIT 383: Administrative Scripting. Topics. What is a Hash? Creating Hashes Hash Keys Accessing Hashes Hash Methods Iterators Hashes and Ordering Multi-valued hashes What are hashes good for?. 192.122.237.230. 192.122.237.7. 64.233.167.99. kosh.nku.edu. www.nku.edu.

rumor
Download Presentation

CIT 383: Administrative Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hashes CIT 383: Administrative Scripting CIT 383: Administrative Scripting

  2. CIT 383: Administrative Scripting Topics • What is a Hash? • Creating Hashes • Hash Keys • Accessing Hashes • Hash Methods • Iterators • Hashes and Ordering • Multi-valued hashes • What are hashes good for?

  3. CIT 383: Administrative Scripting 192.122.237.230 192.122.237.7 64.233.167.99 kosh.nku.edu www.nku.edu google.com What is a Hash? A hash is a dictionary, associating keys with values. Hashes are also called • Associative arrays • Dictionaries • Maps

  4. CIT 383: Administrative Scripting Creating a Hash Creating a Hash literal numbers = { ‘one’ => 1, ‘two’ => 2, ‘three’ => 3 } Creating a Hash element by element numbers = Hash.new numbers[‘one’] = 1 numbers[‘two’] = 2 numbers[‘three’] = 3

  5. CIT 383: Administrative Scripting Hash Keys Almost any ruby object can be a key • Integers • Floats • Strings • Arrays • Symbols Symbols are the fastest hash key • Similar to strings, but cannot be changed. • Written with colon prefix-- :one, :two, :three

  6. CIT 383: Administrative Scripting Accessing Hash Items Indexing (fast) numbers[‘one’] # 1 Reverse indexing (slow) numbers.index(1) # ‘one’ Keys numbers.keys #[‘one’,’two’,’three’] Values numbers.values # [1,2,3]

  7. CIT 383: Administrative Scripting Hash.methods Deletion numbers.delete(‘three’) # 3 numbers # [‘one’,’two’] Empty numbers.empty? # false {}.empty? # true Size numbers.length # 2

  8. CIT 383: Administrative Scripting Hash Iterators Iterate over keys numbers = {:two=>2, :three=>3, :one=>1} numbers.each_key do |key| puts “#{key} has value #{numbers[key]}” end Iterate over values numbers.each_value do |value| puts “#{value} has key #{numbers.index(value)” end

  9. CIT 383: Administrative Scripting Hash Iterators Iterate over keys and values numbers.each do |key,val| puts “#{key} has value #{val}” end

  10. CIT 383: Administrative Scripting Hashes and Ordering Hashes are not ordered like an array Output of iterator may be: two, three, one Solution: • Get array of keys. • Sort array of keys. • Iterate over array of keys. • Use hash[] index to get values. Code: numbers.keys.sort.each do |key| puts “#{key} has value #{numbers[key]}” end

  11. CIT 383: Administrative Scripting Multi-valued hashes What can you do if a key has many values? Use arrays as the values host2ip[‘www.nku.edu’] = [‘192.122.237.7’] host2ip[‘google.com’] = [ ’64.233.167.99’, ’64.233.187.99’, ’72.14.207.99’] host2ip[‘www.nku.edu’][0] host2ip[‘google.com’][1]

  12. CIT 383: Administrative Scripting What are hashes good for? Mapping one data item to another Characters to ASCII values Hostnames to IP addresses Fast, easy lookups To lookup an item in array primes = [2,3,5,7,9,11] primes.each do |num| puts “Found it” if num == 2 end To lookup an item in a hash primes = [2=>1,3=>1,5=>1,7=>1,9=>1,11=>1] puts “Found it” if numbers[2] Implementing sparse data sets zip = { 41099=>’NKU’, 41076=>’HH’, 43606=>’UT’}

  13. CIT 383: Administrative Scripting References • Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. • David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. • Hal Fulton, The Ruby Way, 2nd edition, Addison-Wesley, 2007. • Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005.

More Related