Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a hash value by numeric index

Tags:

ruby

hash

Have a hash:

h = {:a => "val1", :b => "val2", :c => "val3"} 

I can refer to the hash value:

h[:a], h[:c] 

but I would like to refer by numeric index:

h[0] => val1 h[2] => val3 

Is it possible?

like image 728
Lesha Pipiev Avatar asked Feb 28 '13 08:02

Lesha Pipiev


People also ask

Do hashes have indexes Ruby?

A Hash is a collection of key-value pairs. It is similar to an Array , except that indexing is done via arbitrary keys of any object type, not an integer index.

How do I get the hash value in Ruby?

In Ruby, the values in a hash can be accessed using bracket notation. After the hash name, type the key in square brackets in order to access the value.

Can a hash key be a number?

An explanation, why there are no examples with integers as Hash-keys. Hash-keys have (most of the times) a meaning. It may be an attribute name and its value (e.g. :color => 'red' ...). When you have an integer as a key, your semantic may be 'first, second ...' (1).

What is a Ruby hash?

In Ruby, Hash is a collection of unique keys and their values. Hash is like an Array, except the indexing is done with the help of arbitrary keys of any object type. In Hash, the order of returning keys and their value by various iterators is arbitrary and will generally not be in the insertion order.

How do I find the hash value of a string?

Online Hash Calculator. Online Hash Calculator lets you calculate the cryptographic hash value of a string or file. Multiple hashing algorithms are supported including MD5, SHA1, SHA2, CRC32 and many other algorithms.

What is a hash index?

Hash indexes are one of many ways to organize data in a database. They work by taking input and using it as a key for storage on a disk. These keys, or hash values, can be anything from string lengths to characters in the input. Hash indexes are most commonly used when querying specific inputs with specific attributes.

What is hash calculator online?

Hash Calculator Online Hash Calculator Online lets you calculate the cryptographic hash value of a string or file. Multiple hashing algorithms are supported including MD5, SHA1, SHA2, CRC32 and many other algorithms.

How to get elements by Index in a HashSet?

The three different ways we can get elements by index in a HashSet is by: Access elements by index. HashSet contains: [Geek, Geeks, For, Welcome, To] Element at index 3 is: Welcome HashSet contains: [Geek, Geeks, For, Welcome, To] Element at index 3 is: Welcome HashSet contains: [Geek, Geeks, For, Welcome, To] Element at index 3 is: Welcome


1 Answers

h.values will give you an array requested.

> h.values # ⇒ [ #  [0] "val1", #  [1] "val2", #  [2] "val3" # ] 

UPD while the answer with h[h.keys[0]] was marked as correct, I’m a little bit curious with benchmarks:

h = {:a => "val1", :b => "val2", :c => "val3"} Benchmark.bm do |x|   x.report { 1_000_000.times { h[h.keys[0]] = 'ghgh'} }    x.report { 1_000_000.times { h.values[0] = 'ghgh'} } end    # #       user     system      total        real #   0.920000   0.000000   0.920000 (  0.922456) #   0.820000   0.000000   0.820000 (  0.824592) 

Looks like we’re spitting on 10% of productivity.

like image 81
Aleksei Matiushkin Avatar answered Sep 23 '22 13:09

Aleksei Matiushkin