File: sample.rb

package info (click to toggle)
migemo 0.40-10
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,304 kB
  • ctags: 122
  • sloc: sh: 4,758; ruby: 892; makefile: 120
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