f



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

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

0
4/15/2006 8:14:50 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

2 Replies
355 Views

Similar Articles

[PageSpeed] 5

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
0
elven (16)
4/15/2006 10:36:53 AM
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

0
4/15/2006 8:16:39 PM
Reply:

Similar Artilces:

Searching within the hash values of an array of hashes
I have an array of hashes that looks like this: my @media = ( { 'Name' => 'foo' 'Format' => 'mp3', }, { 'Name' => 'bar' 'Format' => 'ogg', }, { 'Name' => 'baz' 'Format' => 'wav', }, ); I want to know if I have any ogg formatted media. If this was a plain hash then it would be simple to grep the values. Is there a way I can do something simple like this on this AoH so I don't have to use a for loop or something to iterate over the array? ...

ACCESS VALUES WITHIN CLASS WITHIN AN ARRAY
I have the following construct (printed with print_r): Array ( [0]=> Classes Object ( [_my_id] => 001 [_my_name] => John ) [1]=> Classes Object ( [_my_id] => 003 [_my_name] => Jill ) ) What is the syntax to access _my_id and _my_name in for loop. I have this but it doesn't work: -for ($I = 0; $I < count($myarrays_name); $I++) { print $myarrays_name[$I]=>Classes->_my_id } Thanks for the help. On Wed, 21 Jun 2006 15:26:39 +0000, Shawn Hamzee wrote: > What is the synt...

How to access all values within a structure within a cell using a colon argument
Hello All, I've got a multi-layered structure in MATLAB that contains many fields and many values within those fields. I want to add a time dimension to this structure, so that at given times, the structure (which still has the same field architecture) has specific values. I did this by using a cell with two "columns": the first column being a time value (e.g. 1 second, 2 second, 3 second, etc.) and the second column containing the entire structure, whose values are updated/changed at every new time. So it would look something like this: % Iteration 1 SomeStruct.SomeField.Som...

How to use different arrays within a loop, based on user input
In my ideal world, the user would be prompted to enter a parameters (i.e. a frequency to use), based on that input different arrays would be used... Here is what I have: ... Dim sArrayName As Variant sArrayName = "sXml_" & sFreq 'sFreq is the user input Dim sXml_A(1 To 20) As String Dim sXml_D(1 To 10) As String sXml_A(1) = "CFMHSCP" ... sXml_A(20) = "CFMHSCP" sXml_D(1) = "CFMHSCP" ... sXml_D(10) = "CFMHSCP" ... '**************************************************** '...

Ruby-Python; using python from within ruby
At the moment I'm looking at rails, it seems like a great framework. The only downside it has is its rather limited availability of 3rd party libraries. At the moment there are at least two libraries I need that are available to python but not to ruby (search engine, either xapian or lucene( python has bindings to both, ruby has neither), pydns and another custom python script) After looking for a solution I came across ruby-python (http://raa.ruby-lang.org/project/ruby-python/). Sadly enough it is a little bit outdated. It seems to need ruby 2.6 and python 1.5 while I'm using ruby 1....

use a hash from a module within a main program without passing by reference or value
Hello, i have a module and my program that uses it. i want to use the hash which is only declared in my module within the main program. I want the output to be one two two one but it prints nothing anyone can help me out and teach me the scope of variables when using modules? in other words: how can i make %MYCOOLHASH available in test.pl without passing it by reference or value. thanks in advance Sebastian --- test.pl ------------------ use mycoolmodule; print "$MYCOOLHASH{'test1'}\n"; print "$MYCOOLHASH{'test2'}\n"; &changeValues(); print "$M...

Rearranging array values based on location within array
Hello, I am new to Matlab and trying to learn as much as I can. I've imported a multiframe TIFF image using the imread command and now have a 3 dimensional matrix. I'm looking for the most efficient/easiest way to reorganize this M by N by P matrix into a 4-column matrix where columns 1,2,3 are the M, N, P coordinates of values (the pixel locations) within the array and column 4 is the value (in this case grayscale value of the image) at that given pixel. Thank you for your time and any insight you can provide to me. Best, Eric "Eric " <ethorhauer@gmail.com> wrot...

Issues using array.delete within a loop of the same array
This is a simplified example for what I'm trying to do but gets the point across: irb(main):001:0> a = [1,2,3,4,5] => [1, 2, 3, 4, 5] irb(main):002:0> a.each {|e| a.delete e} => [2, 4] I would expect the array to be emptied. Insight please... Yes, I know that there are other ways to empty an array (i.e. a = []). My code is more complex and I'm more interested in why this is happening and if there is a work-around for removing array items while iterating over the same array. There will be other cases in this code where I will only want to remove some of the items. Tha...

storing variable values from within an array to outside an array
Hi, I have this array that is acting as a button click function, and i need to write the value of variable d (the last line in the array) to a variable outside the array, but i cant figure out how to do it. i've tried setting it as global etc, but to no avail.... any help would be appreciated. bdf=['cp=round(get(gca,''CurrentPoint''));' ... 'L=get(gco,''UserData'');'... 'set(findobj(''type'',''text''),''string'','... '[''Selected Label : ' 'num2s...

How to refer to a value of an input box within the <input...> tag?
Hi, I want to put a value in a cookie. The following code does not work. It does not store the box1.value in the cookie. How can I fix it? <input type="text" name="box1" onblur=document.cookie="user=" + box1.value> Chris In article <4c22a744.0310171323.12e16e83@posting.google.com>, yma@kicon.com enlightened us with... > Hi, > > I want to put a value in a cookie. The following code does not work. > It does not store the box1.value in the cookie. How can I fix it? > <input type="text" name="box1" onblur=do...

Array within an array
for i in (0..@cw_num) for n in (0..@cl_num) $array_floor [n,i] = [i*@cw_dist,0.0,n*@cl_dist] end end -- Posted via http://www.ruby-forum.com/. ...

Ruby within Ruby
The following is a description of my situation. You may not need to read it, in which case, just skip to the part where it says "Enough of that," but you probably will need to read this. I am trying to make an online text based "hacking" game. Like, the kind where you infiltrate other people's computers and destroy their stuff. Basically, the game will be like a mud, but P2P, since I don't have/want to maintain a server for that kind of stuff. The game engine will be stored locally on the players computer. During distribution, I will use Ruby2Exe or similar ...

slow optimization using nonlcon with GA within matlab toolbox and mixed integer problem using GA within Matlab
greeting all, i'm using nonlcon(nonlinear inequality constraint) with GA from the toolbox provided by matlab. does anyone notice the extreme slowlness in evaluating nonlinear constraint as compared to other linear constraint? my problem for nonlinear constraint is as follow c(1)=x+y^2-2*ones(N,1) where x and y are Nx1 vector. which means if N increases, the size of constraint increases. and to clarify, the min problem is sometime like min c'z where z=[x;y], which means x and y are some range of variable within z(just a matter of problem formulation) well, is there anyone with ot...

Using ruby hash on array
I would like to process some data from an array and using hash to perform a count on one aspect of the data in the array. The array holds lines of similarly formatted data like so data A data B data C data D data E data F data A data B data C data D data E data F data A data B data C data D data E data F data A is the identifier of each row full of data, data F contains a lot of data which requires a regular expression which is passed to a method for example: "#{data F[/Name:\t(.+?)\r\n/, 1]}" I am stuck writing the h...

Web resources about - Using values within arrays within a hash to gsub into an input word... - comp.lang.ruby

Values Voter Summit - Wikipedia, the free encyclopedia
The Values Voter Summit is an annual political conference for American social conservative activists and elected officials from across the United ...

Facebook Is No. 5 on Glassdoor’s Top 25 Companies for Culture & Values
Facebook placed fifth on the 2014 list of Top 25 Companies for Culture & Values from social jobs and career community Glassdoor , which was released ...

Glassdoor: Facebook rated No. 5 nationally in culture, values
Facebook has been highly-rated by its employees for culture and values , coming in at No. 5 overall nationally in a new Glassdoor report . Twitter ...

Gay Family Values (@Depfox) on Twitter
Sign in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here Gay Family Values @ Depfox ...

Common Lab Values on the App Store on iTunes
Get Common Lab Values on the App Store. See screenshots and ratings, and read customer reviews.

Tony Perkins' Remarks on Election 2012 and Values Voter Summit - YouTube
FRC's Tony Perkins to Discuss Presidential Race At National Press Club's Newsmakers Event September 23, 2011 WASHINGTON, D.C. - Family Research ...

Married Life: For parents, Ramadan is a time to think about values that are passed down
I worry that my 22-month-old baby will grow up thinking Ramadan is all about the food.

Forced marriages an 'assault on values': Scott Morrison
Immigration Minister Scott Morrison says forced marriages are an "assault on Australian values" after authorities intercepted a 14-year-old girl ...

America's values on trial with Manning
Over the past 2½ years, all of which he has spent in a military prison, much has been said about Bradley Manning, but nothing has been heard ...

Migrants must follow Australian values, says Frank Lowy
... boat person who became a shopping centre billionaire, has called on migrants to honour the ''great unwritten deal'' to follow Australian values ...

Resources last updated: 1/28/2016 4:45:30 PM