[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] bad perl - need help - dispatch table



Hello,


I have a very ugly script that simply reads in a file, parses and
outputs HTML formatted text.  See my kludge at the end of the email.

The program is invoked as ./program $option  where $option can be a
number of different things.  For this example I will say $option =
asdf.    I am almost embarrassed to post this code cuz I suck so bad
at this....but that's life.

I have a pipe ( | ) delimited file that can be a lot different in that
the headers of the fields can change based on the user input.  That is
why I need to set the table headers depending on what the user inputs
(print_asdf).  That is also why I have different field numbers in
do_asdf based on the user input.  Right now I only have an example for
one user input, "asdf".   There could be as many as 20, I think,
perhaps more.  With all this information, I have two questions:

1) Any better way to handle this varied data other than a sub for each
data type/user input?
2) How can I call the subroutines based on the user input?  Obviously
&print_$foo as I have below doesn't work.  Maybe a dispatch table
would work but I can't get that to work the way I want either.

Thanks!
Terry


$who = $ARGV[0];
$file = "/RPAR.08242004112402.00";
open(F, $file ) or die ("cant open");
print <<EOF;
<HTML>
<BODY>
<b>Report</b><br><br>
<table border=1 cellspacing=8>
EOF

&print_$;

@lines = <F>;
$records = @lines;
# For each record
for( $rcounter=0; $rcounter < $records; $rcounter++)
{
        print "<p><br>\t<tr>\n";
        @fields = split(/\|/,$lines[$rcounter]);
        $len = @fields;

        # For each field in that record
        &do_$who;

        print "\t</tr>\n</p>\n\n";
}
print <<EOF;
</table>
</body>
</html>
EOF

sub print_asdf
{
        print <<EOF;
<th>Source</th>
<th>Destination</th>
<th>Input File</th>
<th>Output File</th>
<th>Date</th>
<th>Input Records</th>
<th>Filter Name</th>
<th>Filter Remove</th>
<th>Transmit Output</th>
EOF
}

sub do_asdf
{
        for $fcounter (0..4,6,36,37,52)
        {
                ($foo) = $fields[$fcounter] =~ /\s*(\S*)\s*/;
                if ($fcounter eq 0)
                {
                        print "\t\t<td><b>$foo</b></td>\n";
                } else {
                        print "\t\t<td>$foo</td>\n";
                }
        }
}