File: sample.rb

package info (click to toggle)
migemo 0.32-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,764 kB
  • ctags: 123
  • sloc: ruby: 971; sh: 541; makefile: 113
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