Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I summarize hashes within a hash?
    primarykey
    data
    text
    <p>I have a hash like this: </p> <pre><code>Some_hash = {"Albania"=&gt;"Europe", "Andorra"=&gt;"Europe", "Austria"=&gt;"Europe", Lebanon"=&gt;"Asia", "Macau"=&gt;"Asia", "Malaysia"=&gt;"Asia", "Papua New Guinea"=&gt;"Asia", "Jamaica"=&gt;"North America", "Martinique"=&gt;"North America", "Argentina"=&gt;"South America", "Chile"=&gt;"South America", "Sao Tome and Principe"=&gt;"Africa", "Senegal"=&gt;"Africa", "Somalia"=&gt;"Africa",} </code></pre> <p>I would like to identify the five continents individually, and the countries that belong to them, such that I end up with something like this: </p> <pre><code>{"Africa" =&gt; ["Senegal", "Somalia"]} {"Europe" =&gt; ["Albania", "Andorra", "Austria"]} </code></pre> <p>for all of the continents.</p> <p>I tried this: </p> <pre><code>def country inflation_hash = {} XPath.match( data, "//country").map do |element| inflation_hash[element.attributes["name"]] = element.attributes["continent"] end inflation_hash.each do |country, continent| new_hash = {} if inflation_hash.has_value?("Africa") == true new_hash["Africa"] = inflation_hash.keys puts new_hash end end end </code></pre> <p>and it works much too well. I get a new hash:</p> <pre><code>{Africa =&gt; []} </code></pre> <p>but I have two problems: </p> <ol> <li>I create a new hash for each African country.</li> <li>Each new hash contains all the keys, which includes all non-African countries.</li> </ol> <p>I think the first problem has to do with the <code>each</code> method so I have to set some conditional, right? </p> <p>The second problem, I have no idea how to fix. </p> <p>Any pointers would be greater appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload