P
Paul Argentoff
Hi all.
How can I create $Subject for future population?
How can I create $Subject for future population?
How can I create $Subject for future population?
not sure i understand your question. does this help you?Paul said:Let's put it this way: I need to create a nested hash which will be populated
later by referencing the keys in the subhashes. How can I do this?
irb(main):001:0> a = {}
{}
irb(main):002:0> a["blue"] = {}
{}
irb(main):003:0> a["blue"]["red"] = 3
3
irb(main):004:0> a["blue"]
{"red"=>3}
irb(main):005:0> a["blue"]["red"]
3
irb(main):006:0>
i think you need ruby 1.8's block form for creating hashes. i asked thatPaul said:irb(main):001:0> a = {}
{}
irb(main):002:0> a["blue"] = {}
{}
irb(main):003:0> a["blue"]["red"] = 3
3
irb(main):004:0> a["blue"]
{"red"=>3}
irb(main):005:0> a["blue"]["red"]
3
irb(main):006:0>
I need something like this:
a = {{}} # this doesn't work, but i've just written it here to emph. the
# subject
...
# somewhere in the iteration
a[foo][bar] = baz
...
not sure i understand your question. does this help you?
irb(main):001:0> a = {}
{}
irb(main):002:0> a["blue"] = {}
{}
irb(main):003:0> a["blue"]["red"] = 3
3
irb(main):004:0> a["blue"]
{"red"=>3}
irb(main):005:0> a["blue"]["red"]
3
irb(main):006:0>
I need something like this:
a = {{}} # this doesn't work, but i've just written it here to emph. the
# subject
...
# somewhere in the iteration
a[foo][bar] = baz
...
hash_maker = proc do |h, k|
h[k] = Hash.new(&hash_maker)
end
Dave said::
: > How can I create $Subject for future population?
:
: Let's put it this way: I need to create a nested hash which will be populated
: later by referencing the keys in the subhashes. How can I do this?
You mean, er,
irb(main):001:0> a=Hash.new({})
=> {}
irb(main):002:0> a['foo']
=> {}
?
--Dave
Joel VanderWerf said:hash_maker = proc do |h, k|
h[k] = Hash.new(&hash_maker)
end
h = Hash.new(&hash_maker)
h[1][2][3][4][5] = 6
p h # {1=>{2=>{3=>{4=>{5=>6}}}}}
Joel VanderWerf said:hash_maker = proc do |h, k|
h[k] = Hash.new(&hash_maker)
end
h = Hash.new(&hash_maker)
h[1][2][3][4][5] = 6
p h # {1=>{2=>{3=>{4=>{5=>6}}}}}
My head hurts That needs to go on the Ruby Idioms page.
Paul Argentoff said:not sure i understand your question. does this help you?
irb(main):001:0> a = {}
{}
irb(main):002:0> a["blue"] = {}
{}
irb(main):003:0> a["blue"]["red"] = 3
3
irb(main):004:0> a["blue"]
{"red"=>3}
irb(main):005:0> a["blue"]["red"]
3
irb(main):006:0>
Here is my example:
irb(main):008:0> a = Hash.new(Hash.new(Array.new(2,0)))
=> {}
irb(main):009:0> a['foo']['bar'][1] = 2
=> 2
irb(main):010:0> a['foo']['bar']
=> [0, 2]
irb(main):011:0> a.keys
=> []
irb(main):012:0>
After this I can't iterate over a's keys -- what I've written the whole mess
for
Joel VanderWerf said:Dave said::
: > How can I create $Subject for future population?
:
: Let's put it this way: I need to create a nested hash which will be populated
: later by referencing the keys in the subhashes. How can I do this?
You mean, er,
irb(main):001:0> a=Hash.new({})
=> {}
irb(main):002:0> a['foo']
=> {}
?
--Dave
That's like, a real bad trip, dude:
irb(main):002:0> a=Hash.new({})
=> {}
irb(main):003:0> a[1]
=> {}
irb(main):004:0> a[1]["x"] = "y"
=> "y"
irb(main):005:0> a
=> {}
irb(main):006:0> a[1]
=> {"x"=>"y"}
irb(main):007:0> a[1] = 2
=> 2
irb(main):008:0> a[2]
=> {"x"=>"y"}
irb(main):009:0> a[1]
=> 2
It would help to do
a = Hash.new {{}}
since you get a new hash each time...
Robert said:Not really:
yourself.Joel VanderWerf said:Oops. Of course you're right, since the value returned by the block is
never assigned to any part of the hash. You always have to do that
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.