blob: 3d715a5fbaba7f87695e5c77bf5c664ce06a8618 (
plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
############################################################################
#
# File: choose.icn
#
# Subject: Program to pick lines from a file
#
# Author: Gregg M. Townsend
#
# Date: January 14, 2002
#
############################################################################
#
# This file is in the public domain.
#
############################################################################
#
# usage: choose [-N] [file...]
#
# This program randomly selects N lines from the input stream and
# outputs them in order. If N is omitted, one line is chosen.
# If the input stream supplies fewer than N lines, all are output.
#
############################################################################
#
# Links: random
#
############################################################################
link random
global wanted # number of lines wanted
global seen # number of lines read so far
record chosen( # one tentatively chosen input line
lnum, # line number
text) # data
global llist # list of tentatively chosen lines
procedure main(args)
local fname
if wanted := abs(integer(args[1])) then
get(args)
else
wanted := 1
llist := []
seen := 0
randomize()
if *args = 0 then
dofile(&input)
else while fname := get(args) do
dofile(open(fname)) | stop("cannot open ", fname)
llist := sortf(llist, 1)
every write((!llist).text)
end
procedure dofile(f)
local line
while line := read(f) do {
seen +:= 1
if seen <= wanted then
put(llist, chosen(seen, line))
else if ?0 < wanted / real(seen) then
?llist := chosen(seen, line)
}
close(f)
return
end
|