Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
Can i calculate the data in the Array???
a a a b b c c c d d e
For example: ArrayNumber = [“3”, “2”, “3”, “2”, “1”]
Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
Can i calculate the data in the Array???
a a a b b c c c d d e
For example: ArrayNumber = [“3”, “2”, “3”, “2”, “1”]
d=[“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”,“d”, “d”, “e”]
p d
dd={}
d.each do |v|
dd[v]||=0
dd[v]+=1
end
p dd
p dd.values
gz zz wrote:
d=[“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”,“d”, “d”, “e”]
p ddd={}
d.each do |v|
dd[v]||=0
dd[v]+=1
endp dd
p dd.values
But,I think ruby’s Hash is not ordered,so…
Dear Cool,
maybe this is what you want:
class Array
def count
k=Hash.new(0)
self.each{ |x| k[x]+=1 }
k
end
end
my_array = [“a”, “a”, “a”, “b”, “b”, “câ€, “c”, “c”,“d”, “d”, “e”]
my_array.count
It gives you a Hash, though, and the numbers are Integers,
not Strings, but this seems more reasonable
Best regards,
Axel
On 28.06.2007 10:44, Axel E. wrote:
k=Hash.new(0)
my_array = [“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”,“d”, “d”, “e”]
my_array.countIt gives you a Hash, though, and the numbers are Integers,
not Strings, but this seems more reasonable
Yes, I’d agree. Having tuples as return value is better because then
you maintain relationship between items and item count.
Btw, thanks for leaving the inject solution to me.
irb(main):005:0> a = %w{a a a b b c c c d d e}
=> [“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “e”]
irb(main):006:0> a.inject(Hash.new(0)) {|h,x| h[x]+=1; h}
=> {“a”=>3, “b”=>2, “c”=>3, “d”=>2, “e”=>1}
irb(main):007:0> a.inject(Hash.new(0)) {|h,x| h[x]+=1; h}.sort
=> [[“a”, 3], [“b”, 2], [“c”, 3], [“d”, 2], [“e”, 1]]
Kind regards
robert
On 6/28/07, Robert K. [email protected] wrote:
Btw, thanks for leaving the inject solution to me.
Shame on you!!!
But that is not what OP asked for
(a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use
#=== here
More generally we have therefore
(a|[]).map{|e|a.select{|x|x==e}.size}
Maybe OP preferred
…).sort.map{…
and sorry OP, I just needed this working break.
Robert
On 28.06.2007 12:56, Robert D. wrote:
On 6/28/07, Robert K. [email protected] wrote:
Btw, thanks for leaving the inject solution to me.
Shame on you!!!
Um, why? Somehow I don’t get the joke, sorry.
But that is not what OP asked for
(a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use
#=== here
Sorry, Robert, but I don’t think this is what the OP was asking for:
irb(main):001:0> a = %w{a a a b b c c c d d e}
=> [“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “e”]
irb(main):002:0> a.map{|e|a.grep(e).size}
=> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]
Your solution repeats counts for the same string but as far as I can see
from his original posting he wants counts for each string just once.
More generally we have therefore
(a|[]).map{|e|a.select{|x|x==e}.size}
Maybe OP preferred
…).sort.map{…and sorry OP, I just needed this working break.
Kind regards
robert
On 28.06.2007 16:11, Robert D. wrote:
On 6/28/07, Robert K. [email protected] wrote:
On 28.06.2007 12:56, Robert D. wrote:
On 6/28/07, Robert K. [email protected] wrote:
=> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]
Careful here Robert I wrote(a|[]) which is a more computation intensive way to write a.uniq. –
its a coding joke brrr
Umpf! I actually overlooked that one. Somehow I read it as (a||[]).
Duh! Sorry for the noise. Btw, that idiom was not part of my
repertoire. So I actually learned something today - although I have to
say I’d rather stick with #uniq - it’s easier for my brain.
Kind regards
robert
On 6/28/07, Robert K. [email protected] wrote:
On 28.06.2007 12:56, Robert D. wrote:
On 6/28/07, Robert K. [email protected] wrote:
Btw, thanks for leaving the inject solution to me.
Shame on you!!!Um, why? Somehow I don’t get the joke, sorry.
Really or are you metajoking?anyway to get out of recursive joke mode,
we are sometimes competing about #inject.
irb(main):002:0> a.map{|e|a.grep(e).size}
=> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]
Careful here Robert I wrote
(a|[]) which is a more computation intensive way to write a.uniq. –
its a coding joke brrr
Read my post please you can learn sooooo much. – this is a joke!
Robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs