Hash/2-d array help!

Would anyone happen to know how would I go about creating a hash where I
have more then one value equating to another?
i.e. England => Liverpool, England => London, France =>Rennes,
England=>Plymouth, France=>Paris.

I've tried all sorts of ways to do this but I always seem to overwrite
the old value of hash[england]=liverpool when I try and add London to
england too.
Is this even possible using a conventional hash? Or would I somehow have
to use a hash of arrays or something?
Also...Just how would I recall the values once I have them?
In my experiments I may well have stumbled across the answer but can't
retrieve them.

Sorry for this question being rather vague but I don't even know the
name of what I'm trying to do or anything about how to do it..I'd
appreciate any help here, at the very least even just a pointer in the
right direction.

···

--
Posted via http://www.ruby-forum.com/.

hash = {
  England => [Liverpool, London, Plymouth],
  France => [Rennes, Paris]
}

Jason

···

On 12/14/06, Just Maz <josquius@hotmail.com> wrote:

Would anyone happen to know how would I go about creating a hash where I
have more then one value equating to another?
i.e. England => Liverpool, England => London, France =>Rennes,
England=>Plymouth, France=>Paris.

I've tried all sorts of ways to do this but I always seem to overwrite
the old value of hash[england]=liverpool when I try and add London to
england too.
Is this even possible using a conventional hash? Or would I somehow have
to use a hash of arrays or something?
Also...Just how would I recall the values once I have them?
In my experiments I may well have stumbled across the answer but can't
retrieve them.

Sorry for this question being rather vague but I don't even know the
name of what I'm trying to do or anything about how to do it..I'd
appreciate any help here, at the very least even just a pointer in the
right direction.

--
Posted via http://www.ruby-forum.com/\.

Ah! Thanks. Got it totally working now!

One other minor thing- would it be possible for the the strings inside
'France' => ['Rennes', 'Paris']
to be variables instead?

i.e. instead of France= those two I earlier have a 'enter country name'
and 'enter city 1', 'enter city 2',etc...

Off the top of my head I think there may be some sort of symbol you'd
put before nation to show it is a actual stored string but I can't
remember what it was...

···

--
Posted via http://www.ruby-forum.com/.

If you're doing it incrementally, instead of all at once like demonstrated, the same end product can be done like:

hash = Hash.new

# LOOP
hash[key] ||= Array.new
hash[key] << value
# END LOOP

Just put the appropriate looping mechanism around those 2 lines, and define key and value (ie England=key, and Liverpool=value).

-Chris

···

On Dec 14, 2006, at 11:28 AM, Jason Roelofs wrote:

hash = {
England => [Liverpool, London, Plymouth],
France => [Rennes, Paris]
}

Jason

On 12/14/06, Just Maz <josquius@hotmail.com> wrote:

Would anyone happen to know how would I go about creating a hash where I
have more then one value equating to another?
i.e. England => Liverpool, England => London, France =>Rennes,
England=>Plymouth, France=>Paris.

I've tried all sorts of ways to do this but I always seem to overwrite
the old value of hash[england]=liverpool when I try and add London to
england too.
Is this even possible using a conventional hash? Or would I somehow have
to use a hash of arrays or something?
Also...Just how would I recall the values once I have them?
In my experiments I may well have stumbled across the answer but can't
retrieve them.

Sorry for this question being rather vague but I don't even know the
name of what I'm trying to do or anything about how to do it..I'd
appreciate any help here, at the very least even just a pointer in the
right direction.

--
Posted via http://www.ruby-forum.com/\.

Jason Roelofs wrote:

hash = {
  England => [Liverpool, London, Plymouth],
  France => [Rennes, Paris]
}

Jason

Ah....now that's a lot simpler then I thought.
How would I then retrieve these values?

puts hash gives error as does puts hash[England] or puts
hash{England}...Are they counted in anyway like hash{england[1]}?

···

--
Posted via http://www.ruby-forum.com/\.

Just Maz wrote:

Ah! Thanks. Got it totally working now!

One other minor thing- would it be possible for the the strings inside
'France' => ['Rennes', 'Paris']
to be variables instead?

Yes, certainly. You can easily create an array containing variables instead
of literal strings.

Off the top of my head I think there may be some sort of symbol you'd
put before nation to show it is a actual stored string but I can't
remember what it was...

Not in Ruby. Ruby doesn't have characters in variable names that give away
their type.

···

--
Paul Lutus
http://www.arachnoid.com

Just Maz wrote:

Ah! Thanks. Got it totally working now!

One other minor thing- would it be possible for the the strings inside
'France' => ['Rennes', 'Paris']
to be variables instead?

i.e. instead of France= those two I earlier have a 'enter country name'
and 'enter city 1', 'enter city 2',etc...

Off the top of my head I think there may be some sort of symbol you'd
put before nation to show it is a actual stored string but I can't
remember what it was...

Hi,

Is the following what you try to do?

Li

##script
#:england and :france (or :England and :France) are symbols in ruby

liverpool='Liverpool'
london='London'
plymouth='Pymouth'
rennes='Rennes'
paris='Paris'

hash = {
      :england => [liverpool, london, plymouth],
      :france => [rennes, paris]
}

puts hash.fetch(:england)[0]
puts hash.fetch(:england)[2]

#output

ruby forum3.rb

Liverpool
Pymouth

···

Exit code: 0

--
Posted via http://www.ruby-forum.com/\.

Just Maz wrote:

Jason Roelofs wrote:

hash = {
  England => [Liverpool, London, Plymouth],
  France => [Rennes, Paris]
}

Jason

Ah....now that's a lot simpler then I thought.
How would I then retrieve these values?

puts hash gives error as does puts hash[England] or puts
hash{England}...Are they counted in anyway like hash{england[1]}?

If you create a hash as the prior poster described:

hash = {
  England => [Liverpool, London, Plymouth],
  France => [Rennes, Paris]
}

Then you retrieve an ordinary array (not a hash) by using one of the keys:

array = hash["England"]

Now "array" contains [Liverpool, London, Plymouth], and you retrieve one of
them using an index number -- array[0] retrieves "Liverpool".

···

--
Paul Lutus
http://www.arachnoid.com

Ah....now that's a lot simpler then I thought.
How would I then retrieve these values?

puts hash gives error as does puts hash[England] or
puts
hash{England}...Are they counted in anyway like
hash{england[1]}?

Hi,

This is right syntax to create a hash(with single or
double quotes), actually you declare a hash of
arrays(HOA):

hash = {
      'England' => ['Liverpool', 'London',
'Plymouth'],
      'France' => ['Rennes', 'Paris']
}

If you just write
England=>[Liverpool, London, Plymouth]

Ruby thinks every of them is a constant due to
capitalization.

hash.fetch('England') will return an array then you
index the array to get the element you want.

Li

C:\>irb
irb(main):001:0> hash = {
irb(main):002:1* 'England' => ['Liverpool',
'London', 'Plymouth'],
irb(main):003:1* 'France' => ['Rennes', 'Paris']
irb(main):004:1> }
=> {"France"=>["Rennes", "Paris"],
"England"=>["Liverpool", "London", "Plymouth"]}
irb(main):005:0> hash.fetch('England')[0]
=> "Liverpool"
irb(main):006:0> hash.fetch('England')[2]
=> "Plymouth"
irb(main):007:0>

···

____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

Oh right thanks, got it working.
It turns out I'd just forgot to change the puts statement to puts
nation...*jumps from metaphorical window*

I think its about time I took a break from staring at a computer screen
for one day.

···

--
Posted via http://www.ruby-forum.com/.