130 likes | 272 Views
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.
E N D
Hashes CIT 383: Administrative Scripting CIT 383: Administrative Scripting
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?
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
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
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
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]
CIT 383: Administrative Scripting Hash.methods Deletion numbers.delete(‘three’) # 3 numbers # [‘one’,’two’] Empty numbers.empty? # false {}.empty? # true Size numbers.length # 2
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
CIT 383: Administrative Scripting Hash Iterators Iterate over keys and values numbers.each do |key,val| puts “#{key} has value #{val}” end
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
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]
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’}
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.