Re: Detecting number ranges



On 9/26/07, Jay Levitt <jay+news@xxxxxx> wrote:
I had to write a script this evening to take an unsorted input file of the
form:

database 1 is on server 3
database 8 is on server 7
...

and output it in the form:

server 3 handles database 1 through 7
server 7 handles database 8 through 11

I feel like there's a much nicer way to express this in Ruby, but can't
think of what it might be...

Does this feel nicer?

$ cat db_ranges.rb && ruby db_ranges.rb
# db_ranges.rb
# 26 September 2007
#

require 'enumerator'

file =<<END
database 8 is on server 7
database 10 is on server 7
database 9 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
END


dbs = Hash.new {|h,k| h[k] = []}

file.each do |line|
m = line.match(/^database (.*) is on server (.*)/)
db_n = m[1].to_i
server_n = m[2].to_i
dbs[server_n] << db_n
end

dbs.each do |server, db|
result = [[db[0]]]
db.sort.each_cons(2) {|x, y| if y == x + 1 then result.last << y;
else result << [y]; end}
result.each {|x| puts "Server #{server} handles databases #{x.first}
to #{x.last}"}
end

Server 144 handles databases 4 to 4
Server 7 handles databases 8 to 10
Server 3 handles databases 1 to 2
Server 3 handles databases 133 to 133
Server 9 handles databases 5 to 5

Kind regards,

Jesus.

.



Relevant Pages

  • Create SharePoint Portal failed.
    ... One mentioned ensuring that SQL Server uses a case ... 13:55:40 Service database server is 'USDC-JOHRIV'. ... Update dbo.propertylist set DisplayName = N'Last name' ...
    (microsoft.public.sharepoint.portalserver)
  • Re: ADO Connection Timeout
    ... to the central server, but you are willing to live with periods where it ... i.e. a local database or even a text file. ... to function until the connection can be restored to the server. ...
    (microsoft.public.data.ado)
  • Web Developers - Happy Hearts And HDTV! - Lockergnome
    ... Certificate on your MSIIS Web server. ... getting data from a database is only half the problem. ... Zend recently started a series about building rock solid code in PHP. ... which provides bulk database conversion. ...
    (freebsd-questions)
  • Re: TNS could not resolve the connect identifier
    ... This database resides on Machine A. ... The Web server is running on Machine B. ... Using tnsping is not as good as using a real connection such as via ... client (note that this is terminology that appears in the 10g R2 ...
    (comp.databases.oracle.server)
  • Config for OLTP system
    ... extrenal disks fo the 60GByte database server. ... IBM Informix Dynamic Server Configuration Parameters ... # BUFFSIZE - OnLine no longer supports this configuration parameter. ...
    (comp.databases.informix)