my $infilename = "MaineReps.txt";
my $outfilename = "MaineRepsTable.txt";
open my $INFILE, '<', $infilename or die "Can't open input file: $!";
open my $OUTFILE, '>', $outfilename or die "Can't open output file: $!";
print $OUTFILE qq
/{| class=\"wikitable\"
! Congress
! Name
! District
! Party
! Residence
! Occupation
/;
my %replist = {};
while (my $line = <$INFILE>) {
chomp($line);
my $wordchar = qr{[A-Za-z0-9.\'\&\(\)\/\,]};
my $matchtoken = qr{$wordchar+(\s$wordchar+)*};
print $OUTFILE "|-\n";
print $OUTFILE "| " ;
$lasttoken = $1;
$line =~ /^\*(\d\d\d?\w\w\, \d{4})\s+.*/;
if ($1 ne $lasttoken) {
$congress = $1;
print $OUTFILE $congress;
}
print $OUTFILE "\n" ;
$line =~ /^\*($congress\s+|\t)($matchtoken)\s\s+.*/;
my $rep = $2;
$rep =~ /^([A-Za-z0-9\.\,\'\s]+)(\([^\)]+\).*)?/;
if ($replist{$1}) {
print $OUTFILE "| " . $rep . "\n";
} else {
$replist{$1} = true;
print $OUTFILE "| [[" . $1 . "]] " . $2 . "\n";
}
$line =~ /.*\t(\d+)\s.*/; # $rep
my $district = $1;
print $OUTFILE "| " . $district . "\n";
$line =~ /.*$district\s*\t($matchtoken)\s\s+.*/;
my $party = $1;
print $OUTFILE "| " . $party . "\n";
$line =~ /.*$party\s*\t($matchtoken)\s\s+.*/;
my $city = $1;
print $OUTFILE "| " . $city . "\n";
$line =~ /.*$city\s*\t($matchtoken)(\s*)$/;
my $occupation = $1;
print $OUTFILE "| " . $occupation . "\n";
}
print $OUTFILE "|}";