summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouie <louie@example.com>2022-09-30 14:44:54 -0700
committerlouie <louie@example.com>2022-09-30 14:44:54 -0700
commitfd99128c22240d5fbc53ce5deff6e54d27746c2d (patch)
tree623fbe63d6409203961b5689665a9fd76cc8739b
First commit
-rwxr-xr-xflashcards.plx87
1 files changed, 87 insertions, 0 deletions
diff --git a/flashcards.plx b/flashcards.plx
new file mode 100755
index 0000000..bfd0f05
--- /dev/null
+++ b/flashcards.plx
@@ -0,0 +1,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";
+}