From: Robert Klemme Date: 2010-03-25T17:30:42+09:00 Subject: Re: hash problem 2010/3/25 Adam Nelreth : > Hi > > I have small problem with adding data to hash table > > In loop I take some string and get IP address from it like this: > > ip = > /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/.match(title) > > and now under ip variable I have my IP which I want to put in hash table > and count if the same IP I will find in another step (loop) > > I create hash table > test = Hash.new > and put it in table > test[ip] = 1 > > But I don't know how to search table to find if the same ip is already > in table and increase counter > > Could You tell me how to do this or give me just a clue how to figure > this out ? The usual idiom for counting is this: counters = Hash.new 0 ... counters[ip] += 1 If you want to print sorted descending by count, you can do counters.sort_by {|ip,cnt| -cnt}.each do |ip,cnt| printf "%6d %s\n", cnt, ip end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/