From: John Feminella Date: 2011-05-15T20:49:58+09:00 Subject: Re: Counting how many times the same elements occurs in an array? You didn't mention what a particular xml_event object looks like, but you'll probably want something like this: xml_events.group_by(&:name).each do |name, events| puts "there were #{events.size} events of type #{name}" end ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Sun, May 15, 2011 at 06:01, Thomas Greenwood wrote: > There's probably a fairly simple way to do this. > > Basically I'm reading data from an xml file, I need to figure out how > many times identical data occurs in certain attributes, so far I've got > the data into two identical arrays and had the intention of nesting > iterators - seeing if the element was equal to the second and > incrementing every time a match was found. That obviously didn't work > out the way I initially thought. > > This seems to be the jist of what I want but it's obviously returning a > count on every iteraton whereas I only want the final tally. > > xml_events.each{|x| >  puts "#{x} occurs #{xml_events.count(x)} times" > } > > Any ideas? > > -- > Posted via http://www.ruby-forum.com/. > >