[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Lee Jarvis

10/2/2007 6:30:00 PM

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 = { :one =>
"something" } (im not sure i get what the :one part does..)

Thanks in advance
--
Posted via http://www.ruby-....

4 Answers

Sebastian Hungerecker

10/2/2007 6:51:00 PM

0

Lee Jarvis wrote:
> 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 = { :one =>
> "something" } (im not sure i get what the :one part does..)

:one is the key and "something" is the value. So hash[:one] would return
"something". If you're confused by :one 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
--
NP: Black Sabbath - Hand of Doom
Jabber: sepp2k@jabber.org
ICQ: 205544826

Lee Jarvis

10/2/2007 7:22:00 PM

0

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
--
Posted via http://www.ruby-....

Sebastian Hungerecker

10/2/2007 7:32:00 PM

0

Lee Jarvis wrote:
> 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>, "room2"=>#<struct Room topic="room2 topic", nicks=[], id=102>}
>> rooms["room1"].topic
=> "room1 topic"
>> rooms["room1"].topic = "bla"
=> "bla"
>> rooms["room1"].topic
=> "bla"


--
NP: Nevermore - Insignificant
Jabber: sepp2k@jabber.org
ICQ: 205544826

Lee Jarvis

10/2/2007 7:55:00 PM

0

> 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>, "room2"=>#<struct Room topic="room2 topic", nicks=[], id=102>}
>>> 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
--
Posted via http://www.ruby-....