How to create mutiple hash dynamicaly

L

Lee Jarvis

Hi i am having two arrays and i have append into a hash like this.Please
help me.

    {:name=>"Databases",:url=>"http://www.examplw.com"},
    {:name=>"Network management",:url=>"http://www.examplw.com"},
    {:name=>"Security",:url=>"http://www.examplw.com"},
    {:name=>"Service",:url=>"http://www.examplw.com"}
    {:name=>"Storage",:url=>"http://www.examplw.com/"}

You need to provide more information before anyone can help you. Your
intentions aren't clear from this message. What exactly are you trying
to do?

Regards,
Lee
 
N

Nike Mike

Lee said:
You need to provide more information before anyone can help you. Your
intentions aren't clear from this message. What exactly are you trying
to do?

Regards,
Lee

How to create mutiple hash like this

[{:a=>'1',:b=>'10'}{:a=>'11',:b=>'101'}]
 
P

Peter Hickman

[Note: parts of this message were removed to make it a legal post.]

Something like

a = Array.new

(1..10).each do |x|
a << { :name => x, :value => rand()}
end

gives

[
{:value=>0.275365921616629, :name=>1},
{:value=>0.908041472109624, :name=>2},
{:value=>0.456925255668977, :name=>3},
{:value=>0.960411659941877, :name=>4},
{:value=>0.322421277822909, :name=>5},
{:value=>0.566391285531641, :name=>6},
{:value=>0.658230197869362, :name=>7},
{:value=>0.319688834896862, :name=>8},
{:value=>0.0955639492351847, :name=>9},
{:value=>0.794232897318298, :name=>10}
]
 
J

Jesús Gabriel y Galán

Lee said:
You need to provide more information before anyone can help you. Your
intentions aren't clear from this message. What exactly are you trying
to do?

Regards,
Lee

How to create mutiple hash like this

[{:a=3D>'1',:b=3D>'10'}{:a=3D>'11',:b=3D>'101'}]

It would be clearer if you specified with a code example the input
data and the expected output (btw, the above is not correct syntax).
But I'll take a stab:

irb(main):001:0> first_array =3D ["Databases", "Network Management", "Secur=
ity"]
=3D> ["Databases", "Network Management", "Security"]
irb(main):002:0> second_array =3D ["http://databases", "http://network",
"http://security"]
=3D> ["http://databases", "http://network", "http://security"]
irb(main):004:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {k =3D> v}; result}
=3D> [{"Databases"=3D>"http://databases"}, {"Network
Management"=3D>"http://network"}, {"Security"=3D>"http://security"}]

Is that what you want?

Jesus.
 
J

Jesús Gabriel y Galán

2010/3/25 Jes=C3=BAs Gabriel y Gal=C3=A1n said:
Lee said:
Hi i am having two arrays and i have append into a hash like this.Plea= se
help me.

{:name=3D>"Databases",:url=3D>"http://www.examplw.com"},
{:name=3D>"Network management",:url=3D>"http://www.examplw.com"},
{:name=3D>"Security",:url=3D>"http://www.examplw.com"},
{:name=3D>"Service",:url=3D>"http://www.examplw.com"}
{:name=3D>"Storage",:url=3D>"http://www.examplw.com/"}
--
Posted viahttp://www.ruby-forum.com/.

You need to provide more information before anyone can help you. Your
intentions aren't clear from this message. What exactly are you trying
to do?

Regards,
Lee

How to create mutiple hash like this

[{:a=3D>'1',:b=3D>'10'}{:a=3D>'11',:b=3D>'101'}]

It would be clearer if you specified with a code example the input
data and the expected output (btw, the above is not correct syntax).
But I'll take a stab:

irb(main):001:0> first_array =3D ["Databases", "Network Management", "Sec= urity"]
=3D> ["Databases", "Network Management", "Security"]
irb(main):002:0> second_array =3D ["http://databases", "http://network",
"http://security"]
=3D> ["http://databases", "http://network", "http://security"]
irb(main):004:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {k =3D> v}; result}
=3D> [{"Databases"=3D>"http://databases"}, {"Network
Management"=3D>"http://network"}, {"Security"=3D>"http://security"}]

Sorry, I meant this:

irb(main):005:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {:name =3D> k, :url =3D> v}; result}
=3D> [{:url=3D>"http://databases", :name=3D>"Databases"},
{:url=3D>"http://network", :name=3D>"Network Management"},
{:url=3D>"http://security", :name=3D>"Security"}]

Jesus.
 
J

Jesús Gabriel y Galán

irb(main):001:0> first_array = ["Databases", "Network Management", "Security"]
=> ["Databases", "Network Management", "Security"]
irb(main):002:0> second_array = ["http://databases", "http://network",
"http://security"]
=> ["http://databases", "http://network", "http://security"]

Sorry for the third post in a row, but this is much better done like this:

irb(main):006:0> first_array.zip(second_array).map {|(k,v)| {:name =>
k, :url => v}}
=> [{:url=>"http://databases", :name=>"Databases"},
{:url=>"http://network", :name=>"Network Management"},
{:url=>"http://security", :name=>"Security"}]

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top