Ask a Question related to PERL Beginners, Design and Development.
-
Gregg O'Donnell #1
* CSV to HTML * zero reads as empty space
I have a script that reads a CSV file into an HTML template. The template reads the CSV data accurately unless the field shows zero, in which case the HTML page displays a blank space. Any suggesstions? Thanks!
---------------------------------
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online
Gregg O'Donnell Guest
-
#39204 [NEW]: MsSq_Fetch_* return Empty (Var)Char as single space
From: bertrand dot lepoittevin at groupe-mma dot fr Operating system: Windows 2000 PHP version: 4.4.4 PHP Bug Type: MSSQL... -
#26315 [Opn->Bgs]: mssql_fectch_* functions are returning a space char on empty fields
ID: 26315 Updated by: iliaa@php.net Reported By: webmaster at cbre dot fr -Status: Open +Status: ... -
#26315 [NEW]: mssql_fectch_* functions are returning a space char on empty fields
From: webmaster at cbre dot fr Operating system: Windows 2000 Server PHP version: 4.3.4 PHP Bug Type: MSSQL related Bug... -
Problem with the empty space into Session variables
Hi, I have a web page that displays records from tables in a database. It is designed to store field's value into Session variables before it... -
Empty table, why so many space ?
"MaisonBorniol" <maisonborniol@nomade.fr> wrote in message news:29f4fc84.0306260116.6aeefcb@posting.google.com... The minimum space allocated is... -
Tim Johnson #2
RE: * CSV to HTML * zero reads as empty space
We might need a little code if we're going to help. As far as I know there are no CSV-HTML gotchas that would do this, so it's probably your algorithm. Are you doing something like this?
if($my_var){
print $my_var;
}
that would print nothing if the value was a zero, because zero evaluates to FALSE.
-----Original Message-----
From: Gregg O'Donnell [mailto:gpod363@yahoo.com]
Sent: Thursday, February 05, 2004 8:38 AM
To: [email]beginners@perl.org[/email]
Subject: * CSV to HTML * zero reads as empty space
I have a script that reads a CSV file into an HTML template. The template reads the CSV data accurately unless the field shows zero, in which case the HTML page displays a blank space. Any suggesstions? Thanks!
---------------------------------
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online
Tim Johnson Guest
-
James Edward Gray II #3
Re: * CSV to HTML * zero reads as empty space
On Feb 5, 2004, at 10:38 AM, Gregg O'Donnell wrote:
Absolutely. I suggest you post your code and let us help you.> I have a script that reads a CSV file into an HTML template. The
> template reads the CSV data accurately unless the field shows zero, in
> which case the HTML page displays a blank space. Any suggesstions?
James
James Edward Gray II Guest
-
Gregg O'Donnell #4
RE: * CSV to HTML * zero reads as empty space
Good follow-up, and here's a snippet:
sub parse_line {
my $line = shift;
chomp($line);
print "LINE: $line<br>\n" if $debug;
my %record;
my $entry;
my $i = 1; # First index
while ($line) {
if ($line =~
s {
^\"
((?:[^\"]|\"\")*)
\"
(?:,|$)
} {}x) {
$entry = $1;
} elsif ($line =~
s {
^
(.*?)
(?:,|$)
} {}x) {
$entry = $1;
} else {
die "Can't parse the line $line";
}
$entry =~ s/\"\"/\"/g;
$record{$i++} = $entry;
}
return \%record;
}
Thanks.
Tim Johnson <tjohnson@sandisk.com> wrote:
We might need a little code if we're going to help. As far as I know there are no CSV-HTML gotchas that would do this, so it's probably your algorithm. Are you doing something like this?
if($my_var){
print $my_var;
}
that would print nothing if the value was a zero, because zero evaluates to FALSE.
-----Original Message-----
From: Gregg O'Donnell [mailto:gpod363@yahoo.com]
Sent: Thursday, February 05, 2004 8:38 AM
To: [email]beginners@perl.org[/email]
Subject: * CSV to HTML * zero reads as empty space
I have a script that reads a CSV file into an HTML template. The template reads the CSV data accurately unless the field shows zero, in which case the HTML page displays a blank space. Any suggesstions? Thanks!
---------------------------------
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online
---------------------------------
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online
Gregg O'Donnell Guest
-
James Edward Gray II #5
Re: * CSV to HTML * zero reads as empty space
On Feb 5, 2004, at 11:56 AM, Gregg O'Donnell wrote:
Just FYI, there are multiple CSV parsing modules on the CPAN. I use> Good follow-up, and here's a snippet:
Text::CSV_XS personally.
" is not a special character, in a regular expression, so save your> sub parse_line {
> my $line = shift;
> chomp($line);
> print "LINE: $line<br>\n" if $debug;
> my %record;
> my $entry;
> my $i = 1; # First index
> while ($line) {
> if ($line =~
> s {
> ^\"
> ((?:[^\"]|\"\")*)
> \"
> (?:,|$)
> } {}x) {
eyes and drop the \s. ;)
This line is out of place, isn't it? It's only needed if the field was> $entry = $1;
> } elsif ($line =~
> s {
> ^
> (.*?)
> (?:,|$)
> } {}x) {
> $entry = $1;
> } else {
> die "Can't parse the line $line";
> }
> $entry =~ s/\"\"/\"/g;
quoted and should be moved inside the if.
You can eliminate the need for this line and the entry variable, if you> $record{$i++} = $entry;
store them when you find them.
Also, you're using a hash when you should be using an array.
Numerically indexed data belongs in an array.
Unfortunately, this sub doesn't really tell us about your problem. I> }
> return \%record;
> }
see nothing wrong here. Does %record contain what you think it does on
exit? You might try printing it to find out.
I suspect the original problem is in your output code somewhere.
James
James Edward Gray II Guest
-
Gregg O'Donnell #6
Re: * CSV to HTML * zero reads as empty space
Here's the whole shootin' match...
#!/usr/local/bin/perl -wT
# ----------------------------------------------------------------------
my $debug = 0;
my @database_dirs =
(
"/home/state/dof/fire/",
"/home/state/dof/fire/"
);
my @template_dirs =
(
"/home/state/dof/fire/",
"/home/state/dof/fire/sit-rep-daily.shtml"
);
# ----------------------------------------------------------------------
use File::Basename;
use strict;
my $is_cgi = ! @ARGV;
$is_cgi = 1;
my $data; # Data file, relative to @database_dirs
my $template; # Template, relative to @template_dirs
my $from; # First record number, starting at 1
my $count; # Number of records to display, 0 == all
my $match; # Regular expression
my $mcol; # Match columns, starting from 1
if ($is_cgi) {
use CGI qw(param header);
use CGI::Carp qw(fatalsToBrowser);
################################################## #########################
# Get parameters
################################################## #########################
my $d = param('data');
my $t = param('template');
$from = param('from') || 2;
$count = param('count') || 1;
$match = param('match');
$mcol = param('mcol') || 1;
defined $d or usage("Input data file was not given");
defined $t or usage("HTML template file was not given");
$d =~ m|^/*(\w[\w\.\-]*)$| or usage("Not a valid file name $d");
$d = $1;
$t =~ m|^/*(\w[\w\.\-]*)$| or usage("Not a valid file name $t");
$t = $1;
foreach my $database_dir (@database_dirs) {
if (-f "$database_dir/$d") {
$data = "$database_dir/$d";
last;
}
}
foreach my $template_dir (@template_dirs) {
if (-f "$template_dir/$t") {
$template = "$template_dir/$t";
last;
}
}
defined $data or usage("File not found $data");
defined $template or usage("File not found $template");
$ENV{'PATH_INFO'} and usage("This new script is configured differently");
print header();
} else {
my $outfile = pop @ARGV;
$outfile or usage("You have to specify an out file");
$outfile =~ /^(\w[\w\.\-]*)$/ or usage("Out file not defined.");
$outfile = $1;
my %param = map {(split('=', $_, 2))} @ARGV;
map {print "==== option $_ = $param{$_}<br>\n"} keys %param if $debug;
# We get the parameters
$data = $param{'data'};
$template = $param{'template'};
$from = $param{'from'} || 2;
$count = $param{'count'} || 1;
$match = $param{'match'};
$mcol = $param{'mcol'} || 1;
defined $data or usage("CSV file not defined.");
defined $template or usage("HTML template not defined.");
open(STDOUT,">$outfile")
or usage("Can't redirect STDOUT to file $outfile - $!");
}
$from =~ /^\d+$/ or usage("'from' has to be a number");
$count =~ /^\d+$/ or usage("'count' has to be a number");
if (defined $match) {
$match =~ s/^\s+//;
$match =~ s/\s+$//;
$mcol =~ /^\d+$/ or usage("Column has has to be a number: $mcol");
}
################################################## #########################
# Open HTML Template and Execute it
################################################## #########################
open(DB, $data)
or usage("Can't open file \"$data\": $!");
my @records;
my $i = 1;
while (<DB>) {
my $record = parse_line($_);
if (defined $match and $match) {
print "MATCH $record->{$mcol} $mcol $match<br>\n" if $debug;
next unless exists $record->{$mcol};
next unless $record->{$mcol} =~ m{$match}oi;
}
print "ITERATE i = $i, from = $from, count = $count<br>\n" if $debug;
next if $i++ < $from;
last if $count and ($i > ($from + $count));
push(@records, $record);
}
close DB;
open(TEMPLATE, $template)
or usage("Can't open file \"$template\": $!");
do_page(join('', <TEMPLATE>));
close TEMPLATE;
close(STDOUT) unless $is_cgi;
################################################## #########################
# Substitution
################################################## #########################
sub do_page {
my $page = shift;
foreach (split(m|(<repeat>.*?</repeat>)|si, $page)) {
if (m|<repeat>(.*?)</repeat>|si) {
print "**** start repeat<br>\n" if $debug;
do_repeat($1);
print "**** end repeat<br>\n" if $debug;
} else {
print "**** start text<br>\n" if $debug;
print;
print "**** end text<br>\n" if $debug;
}
}
}
sub do_repeat {
my $repeat = shift;
my $text = '';
my @parts = split(/<next>/i, $repeat);
while (@records) {
print "** start record<br>\n" if $debug;
foreach my $p (@parts) {
last unless @records; # FIXME: use or not????
my $record = shift @records;
my $part = $p; # Copy so can change
$part =~ s/\$arg([0-9]{1,3})/$record->{$1} || ''/ge;
print "* start part<br>\n" if $debug;
print $part;
print "* end part<br>\n" if $debug;
}
print "** end record<br>\n" if $debug;
}
}
################################################## #########################
# Parse one line
################################################## #########################
sub parse_line {
my $line = shift;
chomp($line);
print "LINE: $line<br>\n" if $debug;
my %record;
my $entry;
my $i = 1; # First index
while ($line) {
if ($line =~
s {
^\"
((?:[^\"]|\"\")*)
\"
(?:,|$)
} {}x) {
$entry = $1;
} elsif ($line =~
s {
^
(.*?)
(?:,|$)
} {}x) {
$entry = $1;
} else {
die "Can't parse the line $line";
}
$entry =~ s/\"\"/\"/g;
$record{$i++} = $entry;
}
return \%record;
}
Thanks again....
James Edward Gray II <james@grayproductions.net> wrote:On Feb 5, 2004, at 11:56 AM, Gregg O'Donnell wrote:
Just FYI, there are multiple CSV parsing modules on the CPAN. I use> Good follow-up, and here's a snippet:
Text::CSV_XS personally.
\n" if $debug;> sub parse_line {
> my $line = shift;
> chomp($line);
> print "LINE: $line" is not a special character, in a regular expression, so save your> my %record;
> my $entry;
> my $i = 1; # First index
> while ($line) {
> if ($line =~
> s {
> ^\"
> ((?:[^\"]|\"\")*)
> \"
> (?:,|$)
> } {}x) {
eyes and drop the \s. ;)
This line is out of place, isn't it? It's only needed if the field was> $entry = $1;
> } elsif ($line =~
> s {
> ^
> (.*?)
> (?:,|$)
> } {}x) {
> $entry = $1;
> } else {
> die "Can't parse the line $line";
> }
> $entry =~ s/\"\"/\"/g;
quoted and should be moved inside the if.
You can eliminate the need for this line and the entry variable, if you> $record{$i++} = $entry;
store them when you find them.
Also, you're using a hash when you should be using an array.
Numerically indexed data belongs in an array.
Unfortunately, this sub doesn't really tell us about your problem. I> }
> return \%record;
> }
see nothing wrong here. Does %record contain what you think it does on
exit? You might try printing it to find out.
I suspect the original problem is in your output code somewhere.
James
---------------------------------
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online
Gregg O'Donnell Guest
-
James Edward Gray II #7
Re: * CSV to HTML * zero reads as empty space
On Feb 5, 2004, at 1:34 PM, Gregg O'Donnell wrote:
Wow, man. No offense intended but we need to clean that up a little> Here's the whole shootin' match...
before I can say a lot about how it is or is not working. Did you know
we won't bill you for using extra whitespace? <laughs>
First two questions:
Okay, this looks like we've decided to be a CGI script officially. Is> my $is_cgi = ! @ARGV;
> $is_cgi = 1;
that right? If so, we can drop a LOT of baggage.
Where is this usage() subroutine???> defined $d or usage("Input data file was not given");
James
James Edward Gray II Guest
-
Gregg O'Donnell #8
Re: * CSV to HTML * please send cgi-beginner listserv address
Thanks to all who helped - please give me the beginners cgi listserv email
Thanks,
Gregg
---------------------------------
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online
Gregg O'Donnell Guest



Reply With Quote

