Computer Science Canada

Hashes...

Author:  Sorb [ Thu Dec 14, 2006 7:16 am ]
Post subject:  Hashes...

I'm a bit new to ruby and I'm currently struggling over hashes.
I can create a hash from scratch just fine with 2 => "robot" and all that however what I'm wanting to do is read a txt then create a hash of the information within.
i.e. it says
1 John Smith 25 UK
2 Fred O'Neill 31 UK
I'd want it so in the hash I have a few 1s with John, Smith, 25 and UK and 2s with Fred's data.
The most I can get however right now is just taking out one of the pieces of data and shoving it into a string. All attempts to make a hash are failing...
How would I do this?

Author:  Sorb [ Thu Dec 14, 2006 8:11 am ]
Post subject: 

hmm can't edit...

What I mean basically is I want to create a 2d array where I can somehow later in the code put in what ever code is relevant to compare the natioanlity (UK for these two).

I can pass one value into a array each so for 1 it will give me John.
Any further though and though it seems to accept 1 => Smith I cannot retrieve that Smith in anyway.

Author:  rdrake [ Thu Dec 14, 2006 12:37 pm ]
Post subject:  Re: Hashes...

Sorb wrote:
I'm a bit new to ruby and I'm currently struggling over hashes.
I can create a hash from scratch just fine with 2 => "robot" and all that however what I'm wanting to do is read a txt then create a hash of the information within.
i.e. it says
1 John Smith 25 UK
2 Fred O'Neill 31 UK
I'd want it so in the hash I have a few 1s with John, Smith, 25 and UK and 2s with Fred's data.
The most I can get however right now is just taking out one of the pieces of data and shoving it into a string. All attempts to make a hash are failing...
How would I do this?
Something tells me a hash is not what you want.... A hash stores a value, but it gives us a nice, pretty name in order to access that value. Take this, for example.
code:
irb(main):005:0> colour = {"favourite" => "green", "leastfav" => "pink"}
=> {"leastfav"=>"pink", "favourite"=>"green"}
irb(main):006:0> puts "My favourite colour is #{colour["favourite"]} while my least favourite is #{colour["leastfav"]}."
My favourite colour is green while my least favourite is pink.
=> nil
Basically a hash is a fancy array with a named index vs. a numbered one.

What you could potentially do is store all information inside of a hash, then create an array of the hash. Take this, for example.
code:
irb(main):007:0> lots_of_info = Array.new
=> []
irb(main):008:0> lots_of_info[0] = {"id" => 1, "name" => "John Smith", "age" => 25, "location" => "UK"}
=> {"name"=>"John Smith", "id"=>1, "age"=>25, "location"=>"UK"}
irb(main):009:0> lots_of_info[1] = {"id" => 2, "name" => "Fred O'Neill", "age" => 31, "location" => "UK"}
=> {"name"=>"Fred O'Neill", "id"=>2, "age"=>31, "location"=>"UK"}
irb(main):010:0> lots_of_info[0]["name"]
=> "John Smith"
Now I'm not saying it's the best solution, just saying that it can be done if you wish Smile.

Author:  wtd [ Thu Dec 14, 2006 12:46 pm ]
Post subject: 

Something like...?

Note: I do this because I highly doubt Ruby is for a school assignment.

code:
my_hash = {}

# Iterate through each line.
IO.foreach("my-file") do |line|
   # Split the line into an array on spaces.
   info = line.split(/\s+/)
   
   # Grab the first piece of info.  Also, convert to integer.
   key = info[0].to_i
 
   # The name.
   name = "#{name[1]} #{name[2]}"

   # Age.
   age = info[3]

   # Location.
   location = name[5]

   # Create a hash of the data.
   info = {:name => name, :age => age, :location => location}

   # Put that hash into the main hash.
   my_hash[key] = info
end


Then you could get Fred's age with something like:

code:
my_hash[2][:age]


: