sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Iterating over multiple hashes

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 212 Views 2 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 all,
      I have a number of hashes to store info for me, they all have the same keys but each has a different value for each key. I've made a user interface which enables a user to input a list of strings corresponding to a value in the hash. Because the list is input by the user, it is important for me to have the ability to iterate over the hashes (say, from a list of the hash names) without having to call each one in an "if" statement every time I need to do an operation (one could imagine how tedious and inefficient it has been to call all my hashes individually ).

      I thought something like this...
      ` a={:name => "eh", :word=> %w{apple, ascend}}
      b={:name => "bee", :word=> %w{ball}}
      c={:name => "sea", :word=> %w{}}
      d={:name => "d", :word => %w{door dog down}}

      word_size_array = Array.new
      user_specified_list = ["a", "c"]
      array_of_hash_names = %w{a b c d}

      user_specified_list.each do |x|
      array_of_hash_names.each |y|
      if y[:name] == x
      word_size_array << y[:word].length
      end
      end`
      would have worked but it doesn't.

      Instead, I have to do something like this...
      user_specified_list.each do |x| if a[:name] == x word_size_array << a[:word].length elsif b[:name] == x word_size_array << b[:word].length elsif c[:name] == x word_size_array << c[:word].length elsif d[:name] == x word_size_array << d[:word].length else end end
      ...for about 15 (and counting) hashes!

      I would like to be able to call the hash using a ".each" or "do" loop but I have not been able to find the trick that would allow me to do this. Does someone have a way to do this (or an easier way to do this database-type stuff;) ?)

      All input is welcome,
      Thanks,
      laura

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

        Laura,

        The following line creates an Array of String objects.

        array_of_hash_names = %w{a b c d}
        ==> %w{a b c d}.inspect
        ["a", "b", "c", "d"]
        
        

        You want an Array of the Hash objects:

        array_of_hashes = [a, b, c, d]

        Hi

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

          Thank you so much for the response!
          It works great!

          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