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

Discussion in 'Ruby' started by Abe, Apr 15, 2006.

  1. Abe

    Abe Guest

    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
     
    Abe, Apr 15, 2006
    #1
    1. Advertising

  2. 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
     
    Bernhard 'elven' Stoeckner, Apr 15, 2006
    #2
    1. Advertising

  3. Abe

    Abe Guest

    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
     
    Abe, Apr 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
  2. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    186
    Robert Klemme
    Nov 9, 2005
  3. Shea Martin
    Replies:
    1
    Views:
    189
    Rodrigo Bermejo
    Jan 15, 2007
  4. Tore Aursand
    Replies:
    3
    Views:
    579
    Anno Siegel
    Sep 16, 2003
  5. Replies:
    14
    Views:
    272
    Tomi Häsä
    Jan 10, 2005
Loading...

Share This Page