Data structures have always confused me. Either I get lost when I'm trying to access the nested elements or I just get frustrated take the easy way out to solve a problem.
Let's say I have this hash of hashes:
This is how I'm accessing the elements of the data structure via the command line:Code:config = { "somehost" => { "user" => "bivouac", "host" => "www.somehost.com", "r_log" => "/home/bivouac/logs" }, "another_host" => { "user" => "someone_else", "host" => "www.another_host.com", "r_log" => "/var/tmp/logs" } }
Seems too remedial. I already have the variable names (user, host, r_log) so it just seems overkill that I'm using them twice as var names and as hash keys. Anyway...Code:machine = ARGV[0] user = config[machine]["user"] host = config[machine]["host"] r_log = config[machine]["r_log"] puts [user, host, r_log].join("\t")
This is the output:
I was looking through the Ruby docs and the Pickaxe and I know that the 'hash' object has a 'each_key' method that you can use to access the keys of a hash, but what if those keys access another hash.Code:$ ruby hash.rb somehost bivouac www.somehost.com /home/bivouac/logs
Two questions really, what's a more elegant solution and anyone have a good tutorial on data structures could be in any language like PHP, Python or Perl. Just looking for better grounding when I run into these problems.




Bookmarks