Comment convertir deux tableaux en hash

12 views
Skip to first unread message

oliviermo75

unread,
Jul 11, 2014, 3:58:15 PM7/11/14
to rubyfr...@googlegroups.com

I'm parsing a website with two functions: href_link and get_price. Each of them adds the value on its own array.

After I call the function get_hash on both.

I can get one element like http but i cant get two element http and price . for example what i expect value ======> ["http://www.ama...."] value --> ["125,40"]

<pre> 
 def get_hash(arr= [])
   linkArray = []
    if arr.grep(/^http/)
      linkArray << arr
      linkArray.each{|f| puts "value ======> #{f}" }
     else
      arrayPrice = []
      arrayPrice << arr
      puts "arrayPrice --> #{arrayPrice}"
    end
end

the output :
value ======> ["http://www.amazon.fr/Motorola-Smartphone-d%C3%A9bloqu%C3%A9-pouces-Android/dp/B00GSNTQ6W/ref=sr_1_1?s=electronics&ie=UTF8&qid=1405090902&sr=1-1"]
value ======> ["http://www.amazon.fr/Samsung-Galaxy-S4-GT-I9195-Smartphone/dp/B00DJ338PE/ref=sr_1_2?s=electronics&ie=UTF8&qid=1405090902&sr=1-2"]
value ======> [["EUR",  "123,35"], ["75,50","EUR"]]

Philippe Creux

unread,
Jul 11, 2014, 5:31:20 PM7/11/14
to rubyfr...@googlegroups.com
Bonjour Olivier,

Si je comprends bien, tu veux combiner ces deux arrays en une hash?

```
prices = [123.35, 75.50 ]

combine(links, prices)
# => { "http://amazon/motorola" => 123.35, "http://amazon/samsung" => 75.50 }
```



-- 
φ



--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "RubyFR public".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse rubyfr-publi...@googlegroups.com.
Pour envoyer un message à ce groupe, envoyez un e-mail à l'adresse rubyfr...@googlegroups.com.
Visitez ce groupe à l'adresse http://groups.google.com/group/rubyfr-public.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages