* CSV to HTML * zero reads as empty space

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default * 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

  2. Similar Questions and Discussions

    1. #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...
    2. #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: ...
    3. #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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: * CSV to HTML * zero reads as empty space

    On Feb 5, 2004, at 10:38 AM, Gregg O'Donnell wrote:
    > 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?
    Absolutely. I suggest you post your code and let us help you.

    James

    James Edward Gray II Guest

  5. #4

    Default 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

  6. #5

    Default Re: * CSV to HTML * zero reads as empty space

    On Feb 5, 2004, at 11:56 AM, Gregg O'Donnell wrote:
    > Good follow-up, and here's a snippet:
    Just FYI, there are multiple CSV parsing modules on the CPAN. I use
    Text::CSV_XS personally.
    > 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) {
    " is not a special character, in a regular expression, so save your
    eyes and drop the \s. ;)
    > $entry = $1;
    > } elsif ($line =~
    > s {
    > ^
    > (.*?)
    > (?:,|$)
    > } {}x) {
    > $entry = $1;
    > } else {
    > die "Can't parse the line $line";
    > }
    > $entry =~ s/\"\"/\"/g;
    This line is out of place, isn't it? It's only needed if the field was
    quoted and should be moved inside the if.
    > $record{$i++} = $entry;
    You can eliminate the need for this line and the entry variable, if you
    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.
    > }
    > return \%record;
    > }
    Unfortunately, this sub doesn't really tell us about your problem. I
    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

  7. #6

    Default 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:
    > Good follow-up, and here's a snippet:
    Just FYI, there are multiple CSV parsing modules on the CPAN. I use
    Text::CSV_XS personally.
    > sub parse_line {
    > my $line = shift;
    > chomp($line);
    > print "LINE: $line
    \n" if $debug;
    > my %record;
    > my $entry;
    > my $i = 1; # First index
    > while ($line) {
    > if ($line =~
    > s {
    > ^\"
    > ((?:[^\"]|\"\")*)
    > \"
    > (?:,|$)
    > } {}x) {
    " is not a special character, in a regular expression, so save your
    eyes and drop the \s. ;)
    > $entry = $1;
    > } elsif ($line =~
    > s {
    > ^
    > (.*?)
    > (?:,|$)
    > } {}x) {
    > $entry = $1;
    > } else {
    > die "Can't parse the line $line";
    > }
    > $entry =~ s/\"\"/\"/g;
    This line is out of place, isn't it? It's only needed if the field was
    quoted and should be moved inside the if.
    > $record{$i++} = $entry;
    You can eliminate the need for this line and the entry variable, if you
    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.
    > }
    > return \%record;
    > }
    Unfortunately, this sub doesn't really tell us about your problem. I
    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

  8. #7

    Default Re: * CSV to HTML * zero reads as empty space

    On Feb 5, 2004, at 1:34 PM, Gregg O'Donnell wrote:
    > Here's the whole shootin' match...
    Wow, man. No offense intended but we need to clean that up a little
    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:
    > my $is_cgi = ! @ARGV;
    > $is_cgi = 1;
    Okay, this looks like we've decided to be a CGI script officially. Is
    that right? If so, we can drop a LOT of baggage.
    > defined $d or usage("Input data file was not given");
    Where is this usage() subroutine???

    James

    James Edward Gray II Guest

  9. #8

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139