Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
A Hash problem
Valentino Lun
1/15/2009 8:24:00 AM
Dear all
I want to build a hash which store the values from database, but failed.
Please give me some hints please. Thank you.
services table
h_code s_code status
AHN BBS 3
AHN MBS 1
AHN HMS 1
CMC CRS 1
CMC CPS 1
CMC BTNS 1
GH CRS 1
GH CPS 1
My expectation
all = {"AHN" => {"BBS" => 3, "MBS" => 1, "HMS" => 1}
"CMC" => {"CRS" => 1, "CPS" => 1, "BTNS" => 1}
"GH" => {"CRS" = 1, "CPS" => 1}
}
all = {}
Service.find(:all).each do |s|
# How to do here?
# I try all[s.h_code][s.s_code]=s.status, but not work
end
Thank you very much
Valentino
--
Posted via
http://www.ruby-...
.
2 Answers
Andrew Timberlake
1/15/2009 9:00:00 AM
0
[Note: parts of this message were removed to make it a legal post.]
On Thu, Jan 15, 2009 at 10:23 AM, Valentino Lun <sumwo@yahoo.com> wrote:
> Dear all
>
> I want to build a hash which store the values from database, but failed.
> Please give me some hints please. Thank you.
>
> services table
> h_code s_code status
> AHN BBS 3
> AHN MBS 1
> AHN HMS 1
> CMC CRS 1
> CMC CPS 1
> CMC BTNS 1
> GH CRS 1
> GH CPS 1
>
>
> My expectation
> all = {"AHN" => {"BBS" => 3, "MBS" => 1, "HMS" => 1}
> "CMC" => {"CRS" => 1, "CPS" => 1, "BTNS" => 1}
> "GH" => {"CRS" = 1, "CPS" => 1}
> }
>
>
> all = {}
> Service.find(:all).each do |s|
> # How to do here?
> # I try all[s.h_code][s.s_code]=s.status, but not work
> end
>
> Thank you very much
>
> Valentino
> --
> Posted via
http://www.ruby-...
.
>
>
Try the following:
#Create a new hash where every key has a default entry of an empty hash
(hash of hashes)
all = Hash.new{|h,k| h[k] = {}}
Service.find(:all).each do |s|
end
h_code s_code status
--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...
"I have never let my schooling interfere with my education" - Mark Twain
Andrew Timberlake
1/15/2009 9:01:00 AM
0
[Note: parts of this message were removed to make it a legal post.]
On Thu, Jan 15, 2009 at 10:23 AM, Valentino Lun <sumwo@yahoo.com> wrote:
> Dear all
>
> I want to build a hash which store the values from database, but failed.
> Please give me some hints please. Thank you.
>
> services table
> h_code s_code status
> AHN BBS 3
> AHN MBS 1
> AHN HMS 1
> CMC CRS 1
> CMC CPS 1
> CMC BTNS 1
> GH CRS 1
> GH CPS 1
>
>
> My expectation
> all = {"AHN" => {"BBS" => 3, "MBS" => 1, "HMS" => 1}
> "CMC" => {"CRS" => 1, "CPS" => 1, "BTNS" => 1}
> "GH" => {"CRS" = 1, "CPS" => 1}
> }
>
>
> all = {}
> Service.find(:all).each do |s|
> # How to do here?
> # I try all[s.h_code][s.s_code]=s.status, but not work
> end
>
> Thank you very much
>
> Valentino
> --
> Posted via
http://www.ruby-...
.
>
>
Sorry about the previous post - I clicked ENTER too soon
Try the following:
#Create a new hash where every key has a default entry of an empty hash
# (hash of hashes)
all = Hash.new{|h,k| h[k] = {}}
Service.find(:all).each do |s|
all[s.h_code][s.s_code] = status
end
--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...
"I have never let my schooling interfere with my education" - Mark Twain
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
A Hash problem
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password