From: 7stud -- Date: 2007-09-26T16:49:09+09:00 Subject: Re: Detecting number ranges I'm not sure why data like this: database 1 is on server 3 database 8 is on server 3 should produce: server 3 handles databases 1 to 8 since server 3 only hands db's 1 and 8. Or, what to do if there is only one db for the server, say: database 1 is on server 3 Should the output be: server 3 handles database 1 to 1 Can there be duplicate lines like this: database 1 is on server 3 database 8 is on server 7 database 1 is on server 3 Also, is the output supposed to be in ascending order by server? In any case, here is the input I used: database 8 is on server 7 database 10 is on server 7 database 10 is on server 7 database 5 is on server 9 database 1 is on server 3 database 2 is on server 3 database 133 is on server 3 database 4 is on server 144 and here is the output: server 3 handles databases 1 to 133 server 7 handles databases 8 to 10 server 9 handles database 5 server 144 handles database 4 require "set" def output_data(arr) arr.each do |elmt| min = elmt[1].min max = elmt[1].max if min == max puts "server #{elmt[0]} handles database #{min}" else puts "server #{elmt[0]} handles databases #{min} to #{max}" end end end servers = Hash.new{|hash, key| hash[key] = Set.new} File.open("data.txt") do |file| file.each_line do |line| nums = line.scan(/\d+/) servers[nums[1].to_i].add(nums[0].to_i) end end data = servers.sort output_data(data) -- Posted via http://www.ruby-forum.com/.