Using values within arrays within a hash to gsub into an input word...

A

Abe

Hi there,

I'm a beginner at Ruby so please bear with me.

My question is how can I identify how many values are in an array
within my hash? Also, how can I utilize those values separately?

Here's what I have so far:

mtrules_cons = {
's' => s_sounds = %w[s c sc sch],
'f' => f_sounds = %w[f ph],
'k' => k_sounds = %w[ch c k cch qu],
'z' => z_sounds = %w[z s x],
'ch' => ch_sounds = %w[ch t tch ct],
'zh' => zh_sounds = %w[c ch sc sch sh]
}

puts mtrules_cons['s']

....prints out the values in the array s_sounds, one value on each line.
It appears that the hash has no knowledge that there's an array within
it...which, if true, means that I have no way of using each of those
values separately--like in looping processes. Am I wrong?

Let me pose this question: without accessing the array directly, is
there a way that I can pull the 3rd value out of the array associated
with the hash key 's'?

I'm hoping there's a way because I'm trying to craft a search and
replace loop which will loop through a word looking for letters which
match the hash key (i.e. 's') and replace every instance of 's' with
the each of the values in the associated array. For instance:

If I put in the word "say" I want the script to loop through that word
and give me:
Say
Cay
Scay
Schay


Any pointers on how to go about doing so? I'm stuck since I don't know
how to loop through the values in an array within a hash.

Thanks in advance!

Regards,

Abe
 
B

Bernhard 'elven' Stoeckner

Abe scribbled on Saturday 15 Apr 2006 10:14:
...prints out the values in the array s_sounds, one value on each line.
It appears that the hash has no knowledge that there's an array within
it...which, if true, means that I have no way of using each of those
values separately--like in looping processes. Am I wrong?
Let me pose this question: without accessing the array directly, is
there a way that I can pull the 3rd value out of the array associated
with the hash key 's'?

It is just an array, you can access it as you would a non-hashkeyed-one:
p hash['s'][0] # => 's'
p hash['s'][1] # => 'c'
p hash['s'].class # => "Array"

ary = hash['s']
p ary # => ['s', 'c', 'sch', ..]

hash['s'].each do |k|
puts "loop"
puts k
end
If I put in the word "say" I want the script to loop through that word
and give me:
Say
Cay
Scay
Schay


Any pointers on how to go about doing so? I'm stuck since I don't know
how to loop through the values in an array within a hash.

word = 'say'
key = 's'
results = hash[key].map {|i| word.gsub(key, i)}
p results
 
A

Abe

AH, I need to put the array reference OUTSIDE the hash reference. I see
now.

Man, you make it look easy. Thanks for the assistance--your examples
are highly educational.

Thanks again!

Abe
 

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
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top