On Sep 27, 12:00 pm, Jay Levitt <jay+n... / jay.fm> wrote:
> On Wed, 26 Sep 2007 14:57:04 -0700, William James wrote:
> > I'm not in the mood for hash.
>
> > ary = DATA.readlines
> > ary.map{|s| s[/\d+$/] }.uniq.each{|server|
> >   db = ary.grep(/ #{server}$/).map{|s|
> >     s[/\d+/].to_i}.sort
> >   puts "Server #{server} databases #{db[0]} to #{db[-1]}"
> > }
>
> Definitely wins the "shortest" prize!  But, um, no output:
>
> #=> Server  databases  to

I just copied, pasted, and ran the code.  Output:
Server 7 databases 8 to 10
Server 9 databases 5 to 5
Server 3 databases 1 to 133
Server 144 databases 4 to 4

Change
ary = DATA.readlines
to
ary = DATA.readlines ; p ary
and see what you get.
Your lack of output could be caused by an added space at the end
of each data line.

>
> Looks like .each is called only once, with |server| == nil.
>
> Weirder, when I just run:
>
> ary = open("fake_data").readlines
> ary.map{|s| s[/\d+$/] }.inspect
> return
>
> I get:
>
> #=> wjames.rb:4: unexpected return (LocalJumpError)
>
> I would have thought that ary.map{|s| s[/d+$] } should return an array
> itself.  Although I'm not clear what that line does - doesn't it look for a
> 1+ digit number n, and return the nth character of the line?

irb(main):001:0> "is it foo bar again?"[ /.oo \w+/ ]
=> "foo bar"

s[ /\d+$/ ] returns a sequence of digits at the end of the string (or
just before
"\n" in the string).

Extra spaces won't bother this version:

ary = DATA.readlines.map{|s| s.scan(/\d+/).map{|s| s.to_i}}
p ary
ary.map{|a| a[1] }.uniq.each{|server|
  db = ary.select{|a| a[1]==server}.map{|a| a[0]}.sort
  puts "Server #{server} databases #{db[0]} to #{db[-1]}"
}

__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