# perl script to parse comma delimited output of wab.exe $liner = "----------------------------------------------------------------------"; while (<>) { if ($. == 1) { # first line tells us the fields: @fields = split /\,/, $_; chop $fields[$#fields]; # remove '\n' #print "Number of fields: $#fields.\n"; } else { @record = split /,/; if ($#record < $#fields) { # if their are less commas separated fields than expected, # first guess that the line got split over more than one line $a = $#record; # remember which field was split over two lines $_ .= <>; @record = split /,/; if ($record[$a] !~ s/^"(.*)"$/$1/s) { print "ERROR line $., concat 2 lines failed. (Didn't find start & end quotes). $_"; } if ($#record != $#fields) { print "ERROR line $., concat 2 lines failed. (Expected $#fields, found $#record). $_"; } else { &store_record(); } } elsif ($#record > $#fields) { # extra comma? print "ERROR line $., comma in field? (Expected $#fields, found $#record). $_"; } else { &store_record(); } } } #print "-----------\n"; foreach (@fields) {print "<$_>\n";} print "-----------\n"; # create a sorted list of records foreach (0..$#{$db{$fields[0]}}) { $a = sprintf "%-30s %-30s %-30s %d", $db{'Last Name'}[$_], $db{'First Name'}[$_], $db{'Middle Name'}[$_], $_; $a =~ tr/A-Z/a-z/; # change to lowercase for sorting list $srt{$a} = $_; } #foreach (sort keys %srt) { print "key: <$_> value: <$srt{$_}>\n"; } # debug sort hash $el_cnt = 0; foreach (sort keys %srt) { $i = $srt{$_}; &get_record($i); &validate(0); # arg = 0 means skip get_record if ($el eq 'el') { $el_cnt++; &print_adr(); } elsif ($el ne '') { print "ERROR: unexpected contents of Office Location field: '$el'.\n"; } } print "$liner\n"; $a = keys %add_err; print "Total: $#{$db{$fields[0]}} Easter Letter: $el_cnt "; printf "Errors: $a E.L. deliverable: %d\n", $el_cnt - $a; # print out errors found during address validation print "Capitalization errors:\n"; foreach (@caps_err) {print " $_\n";} print "Zip code errors:\n"; foreach (@zip_err) {print " $_\n";} $a = keys %add_err; print "Incomplete addresses on Easter Letter list: ($a)\n"; #foreach (sort keys %add_err) {printf " %-40s: $add_err{$_}\n", $_;} foreach (sort keys %srt) { $i = $srt{$_}; &get_record($i); $who = sprintf "%13s, %s", $last_name, $first_name; if (exists $add_err{$who}) { printf " %-43s: $add_err{$who}\n", $who; } } sub print_adr { my($mn) = ''; if ($middle_name ne '') {$mn = " $middle_name";} print "$first_name$mn $last_name\n"; print "$address\n"; print "$city, $state $zip\n"; print " $country\n"; print "\n"; } sub validate { # validate address, using various tests if ($_[0]) {&get_record();} my($who) = sprintf "%13s, %s", $last_name, $first_name; # check for correct Zip codes for common cities if ($city =~ m/Loveland/i && $zip !~ /^8053[78]/) { push @zip_err, "line $line $who '$city $zip'"; } if ($city =~ m/Berthoud/i && $zip !~ /^80513/) { push @zip_err, "line $line $who '$city $zip'"; } if ($city =~ m/Boulder/i && $zip !~ /^8030[123]/) { push @zip_err, "line $line $who '$city $zip'"; } # capitailzations if ($city ne '' && $city !~ m/^[A-Z]/) { push @caps_err, "line $line $who '$city'"; } # incomplete address for Easter Letter records if ($el eq 'el') { # city must begin with a capital letter (flushes out blank city fields) if ($city !~ /^[A-Z]/) { $add_err{$who} .= "city? "; } # address must be filled in if ($address =~ /^\s*$/) { $add_err{$who} .= "street? "; } # US addresses if ($country eq '' || $country =~ m/^USA$/ || country =~ /^U\.S\.A\.$/) { if (not ($zip =~ /^\d\d\d\d\d$/ || $zip =~ m/^\d\d\d\d\d\-\d\d\d\d$/)) { $add_err{$who} .= "zip? "; } if ($state !~ /^[A-Z][A-Z]$/) { $add_err{$who} .= "state? "; } # Canadian addresses } elsif ($country =~ /^Canada$/i) { if ($zip !~ /^[A-Z]\d[A-Z]\s*\d[A-Z]\d$/) { $add_err{$who} .= "zip? "; } if ($state !~ /^[A-Z][A-Z]$/) { $add_err{$who} .= "state? "; } # all other addresses } else { } } } sub get_record { my($i) = $_[0]; $first_name = $db{"First Name"}[$i]; $middle_name = $db{"Middle Name"}[$i]; $last_name = $db{"Last Name"}[$i]; $address = $db{"Home Street"}[$i]; $city = $db{"Home City"}[$i]; $state = $db{"Home State"}[$i]; $zip = $db{"Home Postal Code"}[$i]; $country = $db{"Home Country"}[$i]; $el = $db{"Office Location"}[$i]; $line = $db{"line"}[$i]; } # get_record -------------------------------------------------------------- # --------------------------------------------------------------------------- sub store_record { chop $record[$#record]; # remove '\n' foreach (0..$#record) { push @{ $db{$fields[$_]} }, $record[$_]; push @{ $db{line} }, $.; } } # store_record ------------------------------------------------------------