sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    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

      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
      • TIGT Offline
        TIG Moderator
        last edited by

        #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
        • thomthomT Offline
          thomthom
          last edited by

          @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
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @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

              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
              • TIGT Offline
                TIG Moderator
                last edited by

                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

                  Thanks TIG it is a good point πŸ˜„

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

                  Advertisement