sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Access to hashes

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 389 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.
    • T Offline
      tpoz
      last edited by

      Hello,
      I'm trying to build a database in which to store information and I'm using hashes and arrays to store the info. The only problem is that I'm having a hard time retrieving the info from the hashes.
      I've tried commands such as hash.index("key"), hash.fetch("key"), hash["key"] and have had no luck with accessing the values to the keys.
      Can someone please help me out? I've looked through a few Ruby books but the syntax don't seem to work.
      Thanks,
      tpoz

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

        Well, you've come to the right place! I'm sure someone around here will be able to help.

        However, you asked this rather advanced question in the "newbie" forum. So I'll see if I can get it changed over to the Ruby Discussion forum. I'm sure you'll get better responses there.

        Chris

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

        1 Reply Last reply Reply Quote 0
        • T Offline
          tpoz
          last edited by

          ooops!
          I can re-post if you like...

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            No need to repost, I finally managed to move it (sorry it took so long). Hopefully someone here will be able to help now,

            Chris

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              Laura - you have the correct syntax for accessing hash values which makes me think the problem is in the creation of the hash. Make sure your keys have valid values.

              Can you post some code which demonstrates the problem?

              Hi

              1 Reply Last reply Reply Quote 0
              • T Offline
                tpoz
                last edited by

                Hi Jim,

                I'm making a vegetable hash and tried two ways of creating the hash. Could it be that I have arrays in there?

                
                beaphash = Hash["companion" => %w{carrot corn pea}, "antagonist" => %w{beet broccoli onion}, "name" => "bean(pole)"] 
                carhash = {"companion" => %w{bean(bush) lettuce leek onion pea bean(pole) radish tomato}, "antagonist" => %w{}, "name" => "carrot"}
                spihash = {"companion" => %w{lettuce pea radish}, "antagonist" => %w{}, "name" => "spinach"}
                
                

                I have tried to do this with arrays (keeping track of the index) and I have found similar results (it gives me a number). As you suggested, I think there must be something fundamental going on.

                Thanks!
                laura

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  Those are all valid Hashes, and they work as expected for me. I can get the values by key:

                  
                  beaphash['name'] <ENTER>
                  => "bean(pole)"
                  
                  

                  I cut and paste the code right into SketchUp Ruby Console with no errors.

                  Hi

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    tpoz
                    last edited by

                    Hello,
                    I guess the problem actually coming from me putting it in a loop:

                    
                    beaphash = Hash["companion" => %w{carrot corn pea}, "antagonist" => %w{beet broccoli onion}, "name" => "bean(pole)"]
                    spihash = {"companion" => %w{lettuce pea radish}, "antagonist" => %w{}, "name" => "spinach"}
                    
                    list1 = %w{bean(pole) spinach}
                    list2 = %w{beaphash,spihash}
                    
                    list1.each do|x|
                      list2.each do|y|
                        puts "Hey,  hash length; #{y.length} and name from hash; #{y["name"]} and name from list1; #{x}"
                        puts "But beaphash name is #{beaphash["name"]}"
                      end
                    end
                    
                    

                    I cannot find documentation to tell me what to do with the y in y["name"], which I am assuming is the problem since i can access the hashes by naming them by hand...
                    Does anyone have some input?
                    laura

                    1 Reply Last reply Reply Quote 0
                    • fredo6F Offline
                      fredo6
                      last edited by

                      Laura,

                      I don't understand your code.
                      List1 and list2 are just list of strings, so are x and y, and there is no chance that you get to the Hash tables via the strings.
                      list1 = %w{bean(pole) spinach} --> ["bean(pole)", "spinach"]
                      list2 = %w{beaphash,spihash} --> ["beaphash,spihash"] (you probably have an error there, since the separator in %w is the space, not the comma)

                      It is quite easy to build a flexible database in Ruby (not terribly perfroming however if you have lot of data), but you need to be more explicit on what exactly you look for.

                      Fredo

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        tpoz
                        last edited by

                        Hello,

                        Sorry for the confusion! Thanks for pointing this out Fredo6. I'll try to better explain my predicament...
                        I have list2 (beaphash and spihash - a list of all hashes) and list1 (user-generated list where each of the elements refers to a hash). So, I have a user-generated list (this code is just a snippet) and I want to access the the hashes that correspond to list1 and pull, say, the length of the "companion" array from these hashes.

                            
                        beaphash = Hash["companion" => %w{carrot corn pea}, "antagonist" => %w{beet broccoli onion}, "name" => "bean(pole)"]
                        spihash = {"companion" => %w{lettuce pea radish}, "antagonist" => %w{}, "name" => "spinach"}
                        tomhash = {"companion"=> %w{bean(pole) potato}, "antagonist" => %w{carrot}, "name"=> "tomato"}
                        
                        list1 = %w{bean(pole) tomato}
                        list2 = %w{beaphash spihash tomhash}
                        list3 = Array.new
                        
                            list1.each do|x|
                              list2.each do|y|
                                if y["name"] == x
                                list3 << (y["companion"]).length
                                puts "Hey,  hash length; #{y.length} and name from hash; #{y["name"]} and name from list1; #{x}"
                                puts "But beaphash name is #{beaphash["name"]}"
                                else
                                end
                              end
                            end
                        
                        

                        ...then I can interleave list1 and list3 to make a new hash.
                        I realize that I may have made this more complicated than it needs to be and there may be a simpler solution...I'm really new to Ruby!
                        Any help would be very much appreciated!

                        Thanks,
                        laura

                        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