nike
March 25, 2010, 11:34am
1
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/"}
nike
March 25, 2010, 11:45am
2
On Mar 25, 10:34 am, Nike M. [email protected] wrote:
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/"}
–
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
nike
March 25, 2010, 12:23pm
3
Lee J. wrote:
On Mar 25, 10:34�am, Nike M. [email protected] wrote:
Hi i am having two arrays and i have append into a hash like this.Please
help me.
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=>‘1’,:b=>‘10’}{:a=>‘11’,:b=>‘101’}]
nike
March 25, 2010, 12:33pm
4
On Thu, Mar 25, 2010 at 12:23 PM, Nike M. [email protected]
wrote:
–
[{:a=>‘1’,:b=>‘10’}{:a=>‘11’,:b=>‘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 = [“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 ”]
irb(main):004:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {k => v}; result}
=> [{“Databases”=>“http://databases ”}, {“Network
Management”=>“http://network ”}, {“Security”=>“http://security ”}]
Is that what you want?
Jesus.
nike
March 25, 2010, 12:35pm
5
2010/3/25 Jesús Gabriel y Galán [email protected] :
{:name=>“Storage”,:url=>“http://www.examplw.com/ ”}
How to create mutiple hash like this
“http://security ”]
=> [“http://databases ”, “http://network ”, “http://security ”]
irb(main):004:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {k => v}; result}
=> [{“Databases”=>“http://databases ”}, {“Network
Management”=>“http://network ”}, {“Security”=>“http://security ”}]
Sorry, I meant this:
irb(main):005:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {:name => k, :url => v}; result}
=> [{:url=>“http://databases ”, :name=>“Databases”},
{:url=>“http://network ”, :name=>“Network Management”},
{:url=>“http://security ”, :name=>“Security”}]
Jesus.
nike
March 25, 2010, 12:32pm
6
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}
]
nike
March 25, 2010, 12:36pm
7
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.