blob: bfd0f05a4e7527daa52540a228b3e110d9410f79 (
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#!/usr/bin/env perl
use strict;
use warnings;
my $input;
my $separator = ",";
my %cards;
my $line;
my @fields;
my $field_count; #keep track of how many fields should be in each row
my $term_field; #keep track of which column is the term
my $def_field; #keep track of which column is the definition
sub gethelp() {
print "Usage: $0 [OPTION]... [FILE]\n";
print "Run through flashcards from CSV file\n\n";
print " -h, --help Print this help message\n";
print " -s, --separator SEP Set CSV separator to SEP\n";
}
# Check for argument
if($#ARGV < 0) {
die "Error: missing argument";
}
# Read flags
while($#ARGV >= 0) {
if($ARGV[0] =~ /-h|--help/) {
gethelp();
exit;
}
if($ARGV[0] =~ /-s|--separator/) {
$separator = $ARGV[1] or die "Error: flag missing argument";
shift;
shift;
next;
}
else {
$input = $ARGV[0] or die "Error: missing filename argument";
last;
}
}
# Attempt to open
open(my $fh, "<", "$input") or die "Error: could not open \"$input\"";
# Read first line to determine which column is a function, and which is a definition
$line = <$fh>;
chomp $line;
@fields = split($separator, $line);
$field_count = $#fields;
for(my $i = 0; $i <= $field_count; ++$i) {
if($fields[$i] eq "term") {
$term_field = $i;
}
elsif($fields[$i] eq "definition") {
$def_field = $i;
}
}
#print "$field_count\n";
#print "$term_field\n";
#print "$def_field\n";
# Check that separator produces valid table
if($field_count < 1) {
close($fh);
die "Error: invalid table (no separator)";
}
# Get terms and definitions and close the filehandle
while($line = <$fh>) {
chomp $line;
@fields = split($separator, $line);
$cards{$fields[$term_field]} = $fields[$def_field];
}
close($fh);
# Go through each card (automatically shuffled!)
foreach my $k (keys %cards) {
print "$k\n";
readline(STDIN);
print "$cards{$k}\n";
readline(STDIN);
print "\n";
}
|