Re: Random list of filenames



On Jul 29, 8:24 pm, Rob S <Here@home> wrote:
Thank you all. The awk script was where I was heading when I got stuck.
All 3 scripts work well, but the ruby script was ideal. A good
assortment, equal number in each list, and lightning fast, all of 2
seconds. beautiful.

I think it's about time I tried drumming ruby into my old grey matter.

Cheers all.

--

Rob

Ruby is very nice. You don't have to create any
classes if you don't want to; you can use it much
as you would use awk or Perl.

I'm afraid that my code didn't do a
perfect job of evenly distributing the files.
This version is probably correct.

#!ruby
list = Dir[ "*" ].sort_by{ rand }
chunk, remainder = list.size.divmod( 4 )
start = 0
4.times{ |i|
amount = chunk
if remainder > 0
amount += 1
remainder -= 1
end
File.open( "__list_#{i}", "w" ){ |f|
f.puts( list[ start, amount ].sort )
}
start += amount
}

.



Relevant Pages

  • Re: If you are unhappy with the direction of Ruby 1.8.7+, respond
    ... We can provide the right amount of human resources as well as ... Would there be a chance of merging the efficiencies of Ruby ... CoW will not be cross-platform compatible. ...
    (comp.lang.ruby)
  • Re: [QUIZ] Unit Conversion (#183)
    ... the source and destination beverage it converts the used containers, ... $ ruby convert.rb 1 bottle of wine into glasses of beer ... Oops, I understand the output now, the amount in the brackets is the total ...
    (comp.lang.ruby)
  • Re: Programming to an Interface
    ... In Ruby, ... def debitAccount amount ... the business logic in a transaction would be hidden at the next level down. ... I fail to see how this has anything to do with programming to an interface rather than implementation though. ...
    (comp.object)
  • Re: Better performance than native unix commands?
    ... > I have an issue with nfs and listing a large amount of files that I am ... > wondering if I can solve with ruby. ... > high end so the hardware / network performance is not the bottleneck. ...
    (comp.lang.ruby)
  • Better performance than native unix commands?
    ... I have an issue with nfs and listing a large amount of files that I am ... wondering if I can solve with ruby. ... high end so the hardware / network performance is not the bottleneck. ...
    (comp.lang.ruby)