3 # Converts a Unicorn users.data file to a tab-separated file.
4 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
17 # print STDERR "Loaded this line: " . $_;
19 # Is this the start of a new record?
20 if ( /^... DOCUMENT BOUNDARY ...$/ ) {
23 $section = ''; # just in case this didn't get reset in the previous record
24 # print STDERR "Processing record $serial.\n";
28 # If this isn't the start of the new record, it's a new line in the present record.
31 # Is this line the beginning of a block of data (typically an address or a note)?
32 if ( /^\.(.*?)_BEGIN.$/ ) {
33 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
38 # Is this line the beginning of a block of data (typically an address or a note)?
39 if ( /^\.(.*?)_END.$/ ) {
40 if ("$1." ne $section) {
41 print STDERR "Error in record $serial, line $line (input line $.): got an end-of-$1 but I thought I was in a $section block!\n";
43 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
48 # Looks like we've got some actual data! Let's store it.
49 # FIXME: For large batches of data, we may run out of memory and should store this on disk.
50 if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
52 # Build the name of this field (taking note of whether we're in a named section of data)
59 # Store the field as a key of an array. If it already exists, oh well, now it still exists.
60 $unique_fields{$field} = 1;
62 # Now we can actually store this line of data!
63 $users[$serial]{$field} = $3;
65 # print STDERR "Data extracted: \$users[$serial]{'$field'} = '$3'\n";
70 # This is the continuation of the previous line.
73 $users[$serial]{$field} .= ' ' . $_;
74 # print STDERR "Appended data to previous field. \$users[$serial]{'$field'} is now '" . $users[$serial]{$field} . "'.\n";
79 print STDERR "Loaded " . scalar(@users) . " users.\n";
84 @sorted_fields = sort keys %unique_fields;
85 foreach $i (@sorted_fields) {
92 for (my $u = 0; $u < @users; $u++) {
94 foreach $f (@sorted_fields) {
95 if (defined $users[$u]{$f}) {
103 print STDERR "Wrote " . scalar(@users) . " users.\n";
104 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS