Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating hash collection

Tags:

ruby

mapping

hash

{"Journal"=>[[4, -785.0], 
             [13, -21.9165000915527], 
             [14, -213.008995056152], 
             [15, -50.074499130249]]}

How can you to iterate this hash in Ruby, and how would you to separate out keys & values?

like image 758
RaviShankar Avatar asked Aug 16 '10 13:08

RaviShankar


People also ask

How to iterate HashSet in Java?

First, we make an iterator to iterate HashSet with the help of the iterator () method in Java. And then iterate HashSet with the help of hasNext () and Next () method in Java. Where hasNext () method check whether HashSet has elements or not and, Next () method access the data of HashSet.

How to iterate over a hash table in PowerShell?

There you go, two simple ways in which you can iterate over a hash table in PowerShell. As I indicated, I prefer GetEnumerator because I have access to both the key and the value in a single variable within my loop. But feel free to use the method that works best for your situation.

How to iterate through HashMap entryset using iterator map interface?

Iterate through a HashMap EntrySet using Iterator Map interface didn’t extend a Collection interface and hence it will not have its own iterator. entrySet () returns a Set and a Set interface which extends the Collection interface and now on top of it, we can use the Iterator.

How to iterate the Hashtable in C#?

How To Iterate the Hashtable in C#. Generally when we use the collections in C# , we use the object for iteration. Example ArrayList list = new ArrayList(); list.Add("india"); list.Add("bharat"); foreach (object gg in list) { Console.WriteLine("Value is " + gg);


2 Answers

Ruby has a uniform iteration interface. All collections in Ruby have a method called each, which allows you to iterate over each element of the collection. Note, however, that explicit iteration is a code smell. You should mostly use higher-level iterators like map, reduce, select, find, reject and such.

In this particular case where the collection is a Hash, each element that is being yielded to your block, is a two-element array consisting of the key and the value:

print hsh.reduce('') {|s, el|
  s << "The key is #{el.first} and the value is #{el.last}.\n"
}

Thanks to Ruby's destructuring bind, you can simply bind the two elements of the array to two variables in your block and you won't have the need to constantly take the array apart:

print hsh.reduce('') {|s, (k, v)|
  s << "The key is #{k} and the value is #{v}.\n"
}
like image 129
Jörg W Mittag Avatar answered Sep 20 '22 03:09

Jörg W Mittag


myHash.each do |key, value|
  // key holds the key, value holds the value
end

In case want to transform the arrays inside your array to a map do this:

myNewHash = {}
myArrayOfArrays = myHash["Journal"]
myArrayOfArrays.each do | item |
  myNewHash[item[0]] = item[1]
end
like image 31
haffax Avatar answered Sep 20 '22 03:09

haffax