in reply to Need help , newbie cgi
this code can surely be improved (the whole print stuff, %states is not beautiful etc). i just wanted to provide a starting point for you. perl scripts like that (first process data, then print it out) "live" from their datastructure.use strict; use warnings; use diagnostics; my $file = "c:\\test.data"; my ($type, $state); my %data; my %states; open (FH, $file) or die "[$file]:[$!]"; while (<FH>) { ($type, $state) = split (/\|/, $_); if ((defined $type) and (defined $state)) { $type =~ s/^\s*//; $type =~ s/\s*$//; $state =~ s/^\s*//; $state =~ s/\s*$//; $data{$type}->{$state} ++; $states{$state} = 1; } } close (FH); print qq~<table border="1">\n~; print_header (\%states); print_data (\%data, \%states); print "</table>\n"; ############################################################ sub print_data { ############################################################ my ($data_ref, $states_ref) = @_; foreach my $type (keys %$data_ref) { my $total_for_this_type = 0; print "<tr>\n"; print "\t<td>$type</td>\n"; foreach my $state (keys %$states_ref) { $data_ref->{$type}->{$state} or $data_ref->{$type}->{$stat +e} = 0; print "\t<td>", $data_ref->{$type}->{$state}, "</td>\n"; $total_for_this_type += $data_ref->{$type}->{$state}; } print "\t<td>$total_for_this_type</td>\n"; print "</tr>\n"; } } ############################################################ sub print_header { ############################################################ my ($states_ref) = @_; print "<tr>\n"; print "\t<td>MADE</td>\n"; foreach (keys %$states_ref) { print "\t<td>$_</td>\n"; } print "\t<td>TOTAL</td>\n"; print "</tr>\n"; print "<tr>\n"; print "\t<td>====</td>\n"; foreach (keys %$states_ref) { print "\t<td>", "=" x length ($_), "</td>\n"; } print "\t<td>=====</td>\n"; print "</tr>\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Need help , newbie cgi
by britney (Acolyte) on Sep 13, 2002 at 20:42 UTC | |
by kabel (Chaplain) on Sep 14, 2002 at 10:33 UTC | |
|
Re: Re: Need help , newbie cgi
by britney (Acolyte) on Sep 19, 2002 at 20:33 UTC | |
by fglock (Vicar) on Sep 19, 2002 at 20:49 UTC | |
by britney (Acolyte) on Sep 23, 2002 at 18:11 UTC | |
by britney (Acolyte) on Sep 23, 2002 at 18:12 UTC | |
by kabel (Chaplain) on Sep 23, 2002 at 18:51 UTC | |
by britney (Acolyte) on Oct 07, 2002 at 18:29 UTC |