-2
web_user_agents = {
    "linux" => [
            "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5) Gecko/2008122903 Gentoo Iceweasel/3.0.5",
            "Opera/5.0 (Linux 2.0.38 i386; U) [en]",
    ],
    "windows" => [
            "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko",
            "Opera/9.63 (Windows NT 5.2; U; en) Presto/2.1.1",
    ],
}

How can I iterate every element with key?

The output expected is:

"linux","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5) Gecko/2008122903 Gentoo Iceweasel/3.0.5"
"linux","Opera/5.0 (Linux 2.0.38 i386; U) [en]"
"windows","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko"
"windows","Opera/9.63 (Windows NT 5.2; U; en) Presto/2.1.1",
xlembouras
  • 8,215
  • 4
  • 33
  • 42
Alfons
  • 311
  • 1
  • 8
  • 17
  • Your question is hard to decipher. Show us expected output, the code you've already written to try to generate that output, and the actual output. In the meantime, look up the documentation on the Ruby `keys` method: http://www.ruby-doc.org/core-2.1.1/Hash.html#method-i-keys – Palpatim Apr 18 '14 at 14:56
  • The output expected are 4 rows: "linux","Mozilla/5.0..." "linux","Opera/5.0..." "windows","Mozilla/5..." "windows","Opera/9.63...", – Alfons Apr 18 '14 at 14:58
  • possible duplicate of [How to iterate over a hash in Ruby?](http://stackoverflow.com/questions/1227571/how-to-iterate-over-a-hash-in-ruby) – New Alexandria Apr 18 '14 at 15:09

3 Answers3

2

You can iterate through the hash

web_user_agents.map { |k,v| v.map { |val| [k, val] } }.flatten

#=>[
#=> "linux","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5) Gecko/2008122903 Gentoo Iceweasel/3.0.5"
#=> "linux","Opera/5.0 (Linux 2.0.38 i386; U) [en]"
#=> "windows","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko"
#=> "windows","Opera/9.63 (Windows NT 5.2; U; en) Presto/2.1.1"
#=>]
xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • +1 for one liner. I was thinking the same thing only down this way. `web_user_agents.map{|k,v| [k].cycle(v.count).zip(v)}.flatten(1)` so he would have an array of the key value pairs. – engineersmnky Apr 18 '14 at 19:25
1

Assuming you don't really want the terminating comma on your last row, try:

web_user_agents.keys.each do |agent|
    web_user_agents[agent].each do |line|
        puts %Q("#{agent}","#{line}")
    end
end

Be aware that hashes are stored in non-deterministic order; if it matters to you how the keys come out, you may want to order them, or use an array instead of a hash to store them.

[EDIT: In recent versions of Ruby, hash key order is preserved.]

Palpatim
  • 9,074
  • 35
  • 43
  • Actually hashes were stored in non-deterministic order only for ruby versions prior to 1.9.2, from then on, the insertion order is preserved. https://github.com/rubyspec/rubyspec/blob/master/core/hash/keys_spec.rb and http://stackoverflow.com/a/17355411/687142 – xlembouras Apr 18 '14 at 15:15
  • 1
    Man, you turn your back for one second! Thanks for the info, I'll edit my answer. – Palpatim Apr 18 '14 at 15:17
  • The comma in the last row is permitted, but has no effect. Some add the comma in case they later decide to add other elements. – Cary Swoveland Apr 18 '14 at 17:12
0

The following loops over the main hash.

 web_user_agents.each_pair do |k,v| 
   puts "#{k} => #{v}"
 end

You can nest this like so:

 web_user_agents.each_pair do |k,v|
   v.each do |value|
      puts "#{k} => #{value}"
   end
 end
musicmatze
  • 4,124
  • 7
  • 33
  • 48