#!/usr/bin/perl -w # make_exhibit.plx # this script reads a pair of data files, extracts information # relating to a group of tradeshow exhibitors, and writes # out a browseable Web-based directory of those exhibitors use strict; # configuration section: my $exhibit_file = './exhibit.txt'; # script-wide variable: my %listing; # key: company name ($co_name). # value: HTML-ized listing for this company. # read and parse the main exhibitor file my @listing_lines = (); # holds current listing's lines for passing # to the &parse_exhibitor subroutine open EXHIBIT, $exhibit_file or die "Can't open $exhibit_file for reading: $!\n"; while () { if (/^\s*$/) { # this line is blank (or has nothing but space chars) if (@listing_lines) { &parse_exhibitor(@listing_lines); @listing_lines = (); } } else { # this line actually has data push @listing_lines, $_; } } # process last batch of lines, if the file didn't have a trailing # blank line to trigger it already. if (@listing_lines) { &parse_exhibitor(@listing_lines); } close EXHIBIT or die "Can't close $exhibit_file after reading: $!\n"; # output parsed data for debugging print "LISTINGS:\n\n"; foreach my $co_name (sort keys %listing) { print $listing{$co_name}, "\n"; } # script proper ends. subroutine follows. sub parse_exhibitor { # extract the relevant information about a particular # exhibitor and store it in the appropriate hash. # # invoked with an array of lines read from $exhibit_file. # has no return value, but instead modifies the following # script-wide variable: # # %listing my @lines = @_; my($co_name, $booth, $address, $address2, $phone, $fax, $email, $url, $description); my $line_count = 0; foreach my $line (@lines) { chomp $line; ++$line_count; if ($line_count == 1) { $co_name = $line; } elsif ($line_count == 2) { $booth = $line; } elsif ($line_count == 3) { $address = $line; } elsif ($line_count == 4) { $address2 = $line; } elsif ($line_count == 5) { $phone = $line; } elsif ($line_count == 6){ $fax = $line; } elsif ($line =~ /^\S+@\S+$/) { $email = $line; } elsif ($line =~ /^http:\S+$/) { $url = $line; } else { $description .= "$line\n"; # append so that multi-line # descriptions work right } } # done cycling through @lines. # create the %listing entry. $listing{$co_name} = <<"EOF"; co_name: $co_name booth: $booth address: $address address2: $address2 phone: $phone fax: $fax email: $email url: $url description: $description EOF }