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
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