File: sample.rb

package info (click to toggle)
ruby-bsearch 1.5-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: ruby: 115; makefile: 23; sh: 23
file content (26 lines) | stat: -rw-r--r-- 379 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
# sample - Pick random N lines from a file.
#

num = 10
if ARGV[0] != nil && ARGV[0] =~ /^-(\d+)/ then
  num = $1.to_i
  ARGV.shift;
end

selected = []
lineno = 1
while line = gets
  rand = rand lineno
  if rand < num then
    selected.push line
    if selected.length > num then
      selected.delete_at rand
    end
  end
  lineno += 1
end

selected.each do |x|
  puts x
end