[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array Count Issu

Nick Bo

9/18/2008 9:17:00 PM

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?
--
Posted via http://www.ruby-....

10 Answers

Tim Hunter

9/18/2008 9:25:00 PM

0

Nick Bo wrote:
> 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


--
RMagick: http://rmagick.ruby...

Nick Bo

9/18/2008 10:04:00 PM

0


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

?

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

Adam Shelly

9/18/2008 11:45:00 PM

0

On Thu, Sep 18, 2008 at 3:04 PM, Nick Bo <bornemann1@nku.edu> 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

William James

9/19/2008 12:23:00 AM

0

On Sep 18, 4:17 pm, Nick Bo <bornema...@nku.edu> wrote:
> 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

Nick Bo

9/19/2008 12:36:00 AM

0

Adam Shelly wrote:
> On Thu, Sep 18, 2008 at 3:04 PM, Nick Bo <bornemann1@nku.edu> 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?
--
Posted via http://www.ruby-....

Todd Benson

9/19/2008 12:43:00 AM

0

On Thu, Sep 18, 2008 at 4:17 PM, Nick Bo <bornemann1@nku.edu> wrote:
> 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

Nick Bo

9/19/2008 12:51:00 AM

0

> 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
^_^
--
Posted via http://www.ruby-....

Nick Bo

9/19/2008 2:07:00 AM

0

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...
--
Posted via http://www.ruby-....

Harry Kakueki

9/19/2008 2:17:00 PM

0

On Fri, Sep 19, 2008 at 11:07 AM, Nick Bo <bornemann1@nku.edu> wrote:
> 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...
> --
> Posted via http://www.ruby-....
>
>

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

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Lloyd Linklater

9/19/2008 3:42:00 PM

0

Todd Benson wrote:
> On Thu, Sep 18, 2008 at 4:17 PM, Nick Bo <bornemann1@nku.edu> 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!
--
Posted via http://www.ruby-....