Ruby array to hash
-
Hi is there a fast way or some ruby libraries how to convert array[] to hash{} ?
Example of array[]:
a=[] a << [test1, test2, test3, test4]How can I fill in hash{} with entity name and value?
-
#hash_to_array... including nested hashes def h2a(h={}) a=[] a=[*h] if h.is_a?(Hash) a.dup.each_with_index{|e, i| a[i][1]=h2a(e[1]) if e[1].is_a?(Hash) } return a end #array_to_hash... including nested arrays def a2h(a=[]) h={} a.each{|e| h[e[0]]=e[1] } if a.is_a?(Array) h.dup.each_with_index{|e, i| h[e[0]]=a2h(e[1]) if e[1].is_a?(Array) } return h end -
@krisjaniszakis said:
How can I fill in hash{} with entity name and value?
What do you mean by the name and value of an entity?
-
@krisjaniszakis said:
How can I fill in hash{} with entity name and value?
Assum,ing every component instance has a unique name assigned...
model = Sketchup.active_model comps = model.entities.grep(Sketchup;;ComponentInstance) pairs = comps.map {|i| [i.name,i] } hash = Hash[ *pairs.flatten! ] -
Thanks TIG your generous script prints out unique arrays and it is not realy the exact case.
thomthom -> name, value
h{"name" => value, "name" => value,}Looks like I havn't told all the details.
- I have multidimensional
array[]in Ruby - I want to send array[] to Webdialog using
dialog.execute_script() - I want to send to PHP and arrange and do stuf with
array[]
My
array[]looks like thisarray[[value1, value2, value3,...],[value1, value2,...],...]Im trying to transform
array[]tohash{}
hash{0=>[0_0=>value1,0_1=>value2,0_2=>value3,...],1=>[1_0=>value1,1_1=>value2,...],...}maybe I don't need transform, maybe I can send directly from Ruby to PHP without sending to Webdialog ?
- I have multidimensional
-
My code makes an array into a hash and all its nested arrays into hashes too.
And vice versa for hashes to arrays.It's specifically written to de-hash/re-hash hash data for use in attributes - which can contain arrays but not hashes...
In you case you just need to make simpler versions that doesn't iterate through the array's/hash's contents...
Also reading your OP and comments you could easily write all of this info directly into a hash initially and not worry about later manipulations

-
Thanks TIG it is a good point

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement