-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenumerate
More file actions
executable file
·59 lines (48 loc) · 1.23 KB
/
enumerate
File metadata and controls
executable file
·59 lines (48 loc) · 1.23 KB
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
#!/usr/bin/perl
use strict;
use Getopt::Std;
my $usage = "Usage: cat file | enumerate [-r] [-s N]\n";
$usage .= " Puts an monotonically increasing index at the beginning of each line\n";
$usage .= " -r Put one index for each line, but randomize them,";
$usage .= " without changing the order of the lines.";
$usage .= " -s Start numbering at N, default is 0";
my $count = 0;
my $randomize = 0;
my %para = ();
getopts('rs:', \%para);
if (defined($para{'r'})) {
$randomize = 1;
}
if (defined($para{'s'})) {
$count = $para{'s'};
}
if (defined($para{'h'})) {
die ($usage);
}
if ($randomize) {
my @lines = ();
my @indexes = ();
while (my $line = <>) {
$lines[$#lines+1] = $line;
$indexes[$#indexes+1] = $count++;
}
&fisher_yates_shuffle (\@indexes);
for (my $i = 0; $i <= $#lines; $i++) {
print "$indexes[$i] $lines[$i]";
}
} else {
# duplicating because this will run slower if input is big and we don't want to randomize
while (my $line = <>) {
print "$count $line";
$count++;
}
}
sub fisher_yates_shuffle {
my $array = shift;
my $i;
for ($i = @$array; --$i;) {
my $j = int rand ($i+1);
next if ($i == $j);
@$array[$i,$j] = @$array[$j,$i];
}
}