[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Array to Hash (a better way?

skibud2

8/1/2007 5:27:00 PM

Hey Guys,

I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).

Thanks

["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}



My Current Code:

def array_to_hash(array)
count = 0
hash = Hash.new
(array.length / 2).times do
hash[array[count]] = array[count+1]
count += 2
end
return hash
end

6 Answers

Gordon Thiesfeld

8/1/2007 5:31:00 PM

0

On Aug 1, 12:26 pm, skibud2 <mike.hallo...@gmail.com> wrote:
> Hey Guys,
>
> I want to convert an array like the following to a hash. Below is my
> current code (I am assuming there is a better way).
>
> Thanks
>
> ["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
> {"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}

Use Hash#[]

>> Hash["foo", "bar", "foo1", "bar1","foo2", "bar2"]
=> {"foo1"=>"bar1", "foo2"=>"bar2", "foo"=>"bar"}

hth

Gordon

Marcin Mielzynski

8/1/2007 5:35:00 PM

0

skibud2 pisze:
> Hey Guys,
>
> I want to convert an array like the following to a hash. Below is my
> current code (I am assuming there is a better way).
>
> Thanks
>
> ["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
> {"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}
>

given:
a = ["foo", "bar", "foo1", "bar1","foo2", "bar2"]

puts Hash[*a]

or:

require 'enumerator'
h = {}
a.each_slice(2){|k,v|h[k]=v}
puts h


lopex

Kyle Schmitt

8/1/2007 7:09:00 PM

0

I've been using Ruby for awhile, and I'm afraid I've never used the *
notation...

I can see that Hash[a] wouldn't work, and that Hash[*a] expands the
array out so that Hash can use it elegantly.

But what, in a general, does the * do?

Or is it just for special cases like this?

Thanks,
Kyle

Sebastian Hungerecker

8/1/2007 7:13:00 PM

0

Kyle Schmitt wrote:
> But what, in a general, does the * do?

It turns an array into a list of parameters (or, if used in a method
definition, a list of parameters into an array).


HTH,
Sebastian
--
NP: Dream Theater - Only A Matter Of Time
Ist so, weil ist so
Bleibt so, weil war so

David A. Black

8/1/2007 7:13:00 PM

0

Robert Klemme

8/1/2007 9:58:00 PM

0

On 01.08.2007 19:34, Marcin Mielzynski wrote:
> skibud2 pisze:
>> Hey Guys,
>>
>> I want to convert an array like the following to a hash. Below is my
>> current code (I am assuming there is a better way).
>>
>> Thanks
>>
>> ["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
>> {"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}
>>
>
> given:
> a = ["foo", "bar", "foo1", "bar1","foo2", "bar2"]
>
> puts Hash[*a]
>
> or:
>
> require 'enumerator'
> h = {}
> a.each_slice(2){|k,v|h[k]=v}
> puts h

If you are using Enumerator anyway you can as well do

irb(main):001:0> a = ["foo", "bar", "foo1", "bar1","foo2", "bar2"]
=> ["foo", "bar", "foo1", "bar1", "foo2", "bar2"]
irb(main):002:0> a.to_enum(:each_slice, 2).inject({}) {|h,(k,v)| h[k]=v; h}
=> {"foo1"=>"bar1", "foo2"=>"bar2", "foo"=>"bar"}
irb(main):003:0>

Kind regards

robert