Hashes

L

Lee Jarvis

I want to create a large hash kind of information field.. basically i
have a room (with a specific name, topic, nicknames and id) it might
look like this

name = room1
topic = room1 topic
nicks = [nick1, nick2, nick3 ..]
id = 101

but i have more then 1 room, and would like something like this

rooms =
room1 =
topic = room1 topic
nicks = [nick1, nick2, nick3]
id = 101
room2 =
topic = room2 topic
nicks = [nick1, nick2, nick3 ..]
id = 102


But i wouldn't know how to do it, i need to be able to access all of the
values easily according to the room, is there an easy way to do this?

also, why do i see a lot of hashes like this format: hash = { :eek:ne =>
"something" } (im not sure i get what the :eek:ne part does..)

Thanks in advance
 
S

Sebastian Hungerecker

Lee said:
but i have more then 1 room, and would like something like this

rooms =
room1 =
topic = room1 topic
nicks = [nick1, nick2, nick3]
id = 101
room2 =
topic = room2 topic
nicks = [nick1, nick2, nick3 ..]
id = 102

Hash of structs:
Room=Struct.new:)topic,:nicks,:id)
rooms={"room1" => Room.new("room1 topic", [nick1, nick2, nick3], 101),
"room2" => Room.new("room2 topic", [nick1, nick2, nick2], 102) }

rooms["room1"].topic #=> "room1 topic"
rooms["room2"].id #=> 102


Hash of hashes:
rooms={"room1" => { :topic => "room1 topic",
:nicks => [nick1,nick2],
:id => 101 },
"room2" => { :topic => "room2 topic",
:nicks => [nick1,nick2],
:id => 102 } }

rooms["room1"][:topic] #=> "room1 topic"
rooms["room2"][:id] #=> 102

I prefer the first version.

also, why do i see a lot of hashes like this format: hash = { :eek:ne =>
"something" } (im not sure i get what the :eek:ne part does..)

:eek:ne is the key and "something" is the value. So hash[:eek:ne] would return
"something". If you're confused by :eek:ne being a symbol (instead of e.g. a
string), be aware that that is in no way mandatory. As you see in the above
code, the keys can as well be strings (or anything else). Usually you use
strings when the keys are arbitrary (like roomnames) and symbols otherwise
(like in the nested hashs where the keys are always :topic,:nicks and :id).


HTH,
Sebastian
 
L

Lee Jarvis

Thanks a lot, that was perfect :)

One query though, i used structs and had this code

Room=Struct.new:)topic,:nicks,:id)
rooms={"room1" => Room.new("room1 topic", [nick1, nick2, nick3], 101),
"room2" => Room.new("room2 topic", [nick1, nick2, nick2], 102) }


lets say the topic of room 1 changes, well

rooms["room1"].topic = "new topic"

won't work, is there a reason? or another way to change it?

Thanks again for the help
 
S

Sebastian Hungerecker

Lee said:
Room=Struct.new:)topic,:nicks,:id)
rooms={"room1" => Room.new("room1 topic", [nick1, nick2, nick3], 101),
"room2" => Room.new("room2 topic", [nick1, nick2, nick2], 102) }


lets say the topic of room 1 changes, well

rooms["room1"].topic = "new topic"

won't work, is there a reason?

Works here:
Room=Struct.new:)topic,:nicks,:id) => Room
rooms={"room1" => Room.new("room1 topic", [],101),
?> "room2" => Room.new("room2 topic", [], 102) }
=> {"room1"=>#<struct Room topic="room1 topic", nicks=[],
id=101> said:
rooms["room1"].topic => "room1 topic"
rooms["room1"].topic = "bla" => "bla"
rooms["room1"].topic
=> "bla"
 
L

Lee Jarvis

Works here:
Room=Struct.new:)topic,:nicks,:id) => Room
rooms={"room1" => Room.new("room1 topic", [],101),
?> "room2" => Room.new("room2 topic", [], 102) }
=> {"room1"=>#<struct Room topic="room1 topic", nicks=[],
id=101> said:
rooms["room1"].topic => "room1 topic"
rooms["room1"].topic = "bla" => "bla"
rooms["room1"].topic
=> "bla"

Ahh yeah, i was being silly and working with 2 files, and running the
un-modified one, its been a long day

That's a lot for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top