Array Count Issu

N

Nick Bo

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?
 
T

Tim Hunter

Nick said:
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?

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
 
N

Nick Bo

----------------------------------------------------------- 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])

?
 
A

Adam Shelly

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
 
W

William James

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?

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

Nick Bo

Adam said:
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 + "\t"
i=i+1
end

#aesthetic purposes for me
print "\n\n"

countArray = []
i=0
wordsArray.each do
countArray.push(wordsArray.grep(wordsArray).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?
 
T

Todd Benson

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)

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

=> {"goose"=>1, "duck"=>2}

hth a little,
Todd
 
N

Nick Bo

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
^_^
 
N

Nick Bo

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...
 
H

Harry Kakueki

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...

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
 
L

Lloyd Linklater

Todd said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top