Hi Chris,
Chris Brown wrote:
My_hash contains standard data of an RSS file:
channel_title, channel URl and items.
Items contains: title, link, author and description.
so My_hash[‘title’] returns the right RSS channel
Title e.g. CNN To stories
But how can I return only all the links? or only one
description? I’m not familiar with hashes so I’m confused.
I just had to work through this a week or so ago (accessing a hash of
hashes, not the RSS part) so I’ll pass along what I learned in the hope
it
will help.
In general, a hash is a set of (key, value) pairs. The value can be a
singleton value, another hash, an array… Those values can,
themselves, be
any of the same. To access a values in a multi-level hash, you iterate
through the levels. So conditionally accessing a hash of hashes looks
like…
level1.each do |level1_key, level1_value| # iterate through the top
level
set of pairs
if level1_key == what_you_want # test each key to see if
you
want to process it’s values
level1_value.each do |level2_key, level2_value| # if you do, and
it’s a
hash itself, iterate through it
if level2_key == another_thing_you_want # test each key to see if
…
level2_value.each do |level2_key, level3_value| # and so on
# and down you go…
end
end
end
end
end
HTH,
Bill