Adam S. wrote:
On Thu, Sep 18, 2008 at 3:04 PM, Nick Bo [email protected] wrote:
I dont want to know how many characters are in a word i want to find the
word itself so how would i set it up so I could see how many instances
of the word is in the string? would it be something like
irb> st = ‘cat cat fox’
=> “cat cat fox”
irb> sa = st.split
=> [“cat”, “cat”, “fox”]
since the string is now in the array, you can search the array
instead of the string.
irb> sa.grep(sa[0]).size
=> 2
#if you have to search through a string, here’s a hack:
irb> ct=0;st.gsub(sa[0]){ct+=1};ct
=> 2
-Adam
This was the simplest for me to understand since I am very new to using
Ruby THANK YOU SOOO MUCH. Though it still needs tweaking it gives me
alot of 1’s for some reason? Ill show you EVERYTHING i got now.
./wordcount <words
#This is the words doc.
bar bar bar bar bar bar
baz baz baz baz baz baz baz baz baz baz baz
eggs eggs eggs
lovely
spam spam spam spam
#this is the wordsArray printing
bar bar bar bar bar bar baz baz baz
baz baz baz baz baz baz baz baz eggs
eggs eggs lovely spam spam spam spam
#this is the countArray printing.
666666111111111111111111111133314444
:::HERES MY CODE:::
string = “”
i=0
while words = gets
string << words
end
#print words doc, and then print the array and each variable with a tab.
print string
wordsArray = string.split
wordsArray.each do
print wordsArray[i] + “\t”
i=i+1
end
#aesthetic purposes for me
print “\n\n”
countArray = []
i=0
wordsArray.each do
countArray.push(wordsArray.grep(wordsArray[i]).size)
i=i+1
end
print countArray
obivously when i process as well i will not need repetition such as:
bar = 6
bar = 6
etc…
But i am hoping hash with a mixture of repetitive values and keys will
be able to show just one of each type of word that is counted through
the program. Now why is it give me a bunch of 1’s though?