• Login
sketchucation logo sketchucation
  • Login
๐Ÿค‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Ruby array to hash

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 4 Posters 1.2k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    krisjaniszakis
    last edited by 29 May 2013, 14:57

    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?

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 29 May 2013, 15:17

      #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
      
      
      

      TIG

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 29 May 2013, 20:04

        @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?

        Thomas Thomassen โ€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 29 May 2013, 23:55

          @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! ]
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • K Offline
            krisjaniszakis
            last edited by 30 May 2013, 06:45

            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.

            1. I have multidimensional array[] in Ruby
            2. I want to send array[] to Webdialog using dialog.execute_script()
            3. I want to send to PHP and arrange and do stuf with array[]

            My array[] looks like this array[[value1, value2, value3,...],[value1, value2,...],...]

            Im trying to transform array[] to hash{}
            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 ?

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 30 May 2013, 08:03

              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 ๐Ÿ˜•

              TIG

              1 Reply Last reply Reply Quote 0
              • K Offline
                krisjaniszakis
                last edited by 30 May 2013, 16:39

                Thanks TIG it is a good point ๐Ÿ˜„

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  7/7
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement