From: Harry Kakueki Date: 2007-09-28T07:13:39+09:00 Subject: Re: Detecting number ranges On 9/28/07, Jay Levitt wrote: > On Wed, 26 Sep 2007 17:57:16 +0900, Harry Kakueki wrote: > > > fp_arr = ["db 11 is on sv 3","db 9 is on sv 7","db 13 is on sv 3","db > > 12 is on sv 3","db 8 is on sv 7","db 18 is on sv 2"] > > > > nums,info = [],{} > > fp_arr.each {|x| nums << x.scan(/\d+/)} > > sv = nums.map{|x| x[1]}.uniq > > sv.each {|t| info[t] = nums.select{|x| x[1] == t}.sort.map {|a| a[0]}} > > info.keys.sort.each {|j| print "server #{j} handles database > > #{info[j][0]} through #{info[j][-1]}\n"} > > > > #server 2 handles database 18 through 18 > > #server 3 handles database 11 through 13 > > #server 7 handles database 8 through 9 > > Hmm, that fails if I add "db 20 is on sv 2", which should retain "server 2 > handles db 18 through 18" but also add "srver 2 handles db 20 through 20". > I can't quite follow what it's doing but I'm going to have to study that > more; map is a powerful tool in general. > In that case, it outputs "server 2 handles database 18 to 20". I also thought that is what you wanted. But after seeing your further explanation, I see that it is not. Here is the same code with comments I probably should have included the first time. fp_arr = ["db 11 is on sv 3","db 9 is on sv 7","db 13 is on sv 3","db 12 is on sv 3","db 8 is on sv 7","db 18 is on sv 2","db 20 is on sv 2"] nums,info = [],{} fp_arr.each {|x| nums << x.scan(/\d+/)} #p nums #[["11", "3"], ["9", "7"], ["13", "3"], ["12", "3"]....] sv = nums.map{|x| x[1]}.uniq #["3", "7", "2"] server list sv.each {|t| info[t] = nums.select{|x| x[1] == t}.sort.map {|a| a[0]}} #p info #{"7"=>["8", "9"], "2"=>["18", "20"], "3"=>["11", "12", "13"]} info.keys.sort.each do |j| print "server #{j} handles database #{info[j][0]} to #{info[j][-1]}\n" end #server 2 handles database 18 to 20 #server 3 handles database 11 to 13 #server 7 handles database 8 to 9 Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/