Array Count Issu

irb
irb(main):001:0> string =“cat cat fox”
=> “cat cat fox”
irb(main):002:0> stringArray = string.split(" ")
=> [“cat”, “cat”, “fox”]
irb(main):003:0> string.count(stringArray[0])
=> 6

obviously what i want it to do is to count how many instances of cat
there are in a string by using an array. Reason for this is i want to
be able to push the value of the count into a new array and make a hash
using (word, count) and then be able to print the word and count and
then do a method which will print the word and some kind of delimeter
that will express how many times cat showed up so…

cat| ##

or something like that but first thing is first why wont it let me count
how many instances of cat were in the string using an array? and why
does it tell me 6?

Nick Bo wrote:

be able to push the value of the count into a new array and make a hash
using (word, count) and then be able to print the word and count and
then do a method which will print the word and some kind of delimeter
that will express how many times cat showed up so…

cat| ##

or something like that but first thing is first why wont it let me count
how many instances of cat were in the string using an array? and why
does it tell me 6?

String#count counts characters, not strings. You’re asking how many c’s,
a’s, and t’s there are in “cat cat fox” and the answer is 6. 2 c’s, 2
a’s, and 2 t’s.

----------------------------------------------------------- String#count
str.count([other_str]+) => fixnum

  Each other_str parameter defines a set of characters to count. The
  intersection of these sets defines the characters to count in str.
  Any other_str that starts with a caret (^) is negated. The
  sequence c1--c2 means all characters between c1 and c2.

     a = "hello world"
     a.count "lo"            #=> 5
     a.count "lo", "o"       #=> 2
     a.count "hello", "^l"   #=> 4
     a.count "ej-m"          #=> 4

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

----------------------------------------------------------- String#count
str.count([other_str]+) => fixnum

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

str.count([cat])

?

On Sep 18, 4:17 pm, Nick Bo [email protected] wrote:

be able to push the value of the count into a new array and make a hash
using (word, count) and then be able to print the word and count and
then do a method which will print the word and some kind of delimeter
that will express how many times cat showed up so…

cat| ##

or something like that but first thing is first why wont it let me count
how many instances of cat were in the string using an array? and why
does it tell me 6?

count = 0
==>0
“cat cat fox”.scan( “cat” ){ count += 1 }
==>“cat cat fox”
count
==>2
“cat cat fox”.scan( “cat” ).size
==>2

On Thu, Sep 18, 2008 at 4:17 PM, Nick Bo [email protected] wrote:

be able to push the value of the count into a new array and make a hash
using (word, count)

a = “duck”,“duck”,“goose”
a.inject(Hash.new(0)) {|h, e| h[e] += 1; h}

=> {“goose”=>1, “duck”=>2}

hth a little,
Todd

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?

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?

OMG
<<n00b those werent 1s they were 11’s ROFLMAO i figured it out thanks
guys but now i got to think of a way to hash it so it only shows one
instance of each word with the count along with it ^^ I love this
site’s community hopefully ill get better and get to contribute as well
^
^

ok well now i got a bunch of repetitive arrays and repetitive counts how
do i just do 1 array for an instant of the word and then an array for
the amount of times it was found in the string. Cause i dont want it to
print out like

bar = 6
bar = 6
(… 4 more times)

and so forth i just want it to do
bar = 6
baz = 11
eggs = 3
etc…

From: Nick Bo [mailto:[email protected]]

ok well now i got a bunch of repetitive arrays and repetitive

counts how

do i just do 1 array for an instant of the word and then an array for

the amount of times it was found in the string. Cause i dont

want it to

print out like

bar = 6

bar = 6

(… 4 more times)

and so forth i just want it to do

bar = 6

baz = 11

eggs = 3

etc…

just keep your code simple.
use the structure that works best in your case.

eg

c:\family\ruby>cat test.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

c:\family\ruby>cat test.rb
h=Hash.new(0)
while line=gets
line.split.each do |word|
h[word] += 1
end
end

h.each do |word, count|
puts “%s: %d” % [word,count]
end

c:\family\ruby>cat test.doc | ruby test.rb
baz: 11
eggs: 3
bar: 6
spam: 4
lovely: 1

Todd B. wrote:

On Thu, Sep 18, 2008 at 4:17 PM, Nick Bo [email protected] wrote:

be able to push the value of the count into a new array and make a hash
using (word, count)

a = “duck”,“duck”,“goose”
a.inject(Hash.new(0)) {|h, e| h[e] += 1; h}

=> {“goose”=>1, “duck”=>2}

hth a little,
Todd

Todd, that is just wonderful! I have been playing with variations from
this for an hour. GREAT stuff!

On Fri, Sep 19, 2008 at 11:07 AM, Nick Bo [email protected] wrote:

bar = 6
baz = 11
eggs = 3
etc…

Posted via http://www.ruby-forum.com/.

str =“cat cat hat fox hat hat foo bar bar hat test bar hat fox”
arr = str.split(/ /)
arr.uniq.each do |x|
print “#{x} = #{arr.select{|y| y==x}.length} \n”
end

Harry