Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Using values within arrays within a hash to gsub into an input word...

Reply
Thread Tools

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

 
 
Abe
Guest
Posts: n/a
 
      04-15-2006
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

 
Reply With Quote
 
 
 
 
Bernhard 'elven' Stoeckner
Guest
Posts: n/a
 
      04-15-2006
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
 
Reply With Quote
 
 
 
 
Abe
Guest
Posts: n/a
 
      04-15-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM
copying values from a hash into CGI.pm via tied hash reference ioneabu@yahoo.com Perl Misc 14 01-10-2005 01:22 PM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments