Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
We don't bite newbies here... much
 
PerlMonks  

Creating a table from text delimited by newlines

by jerrygarciuh (Curate)
on Nov 26, 2001 at 03:30 UTC ( [id://127485]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

This little snippet is close to what I want. It prints dark rows in the table if the line of the text file just has a newline, but it takes each line of text and makes it its own row. What I want to do is take all the text up to the next newline into one row, then print the dark rows for the newline(s). How can I modify this to keep reading, I know it needs to be an until or something like that but I can't seem to figure it out.
TIA
jg
while (<FH>) { chomp($_); if (/(.)/) { print "<TR><TD>Checkbox</TD><TD>$_ </TD></TR>\n"; } else { print "<TR bgcolor=#000066><TD WIDTH=298>&nbsp;</TD><TD WIDTH= +298>&nbsp;</TD></TR>\n"; } } close FH;
_____________________________________________________
If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ

Replies are listed 'Best First'.
Re: Creating a table from text delimited by newlines
by Fastolfe (Vicar) on Nov 26, 2001 at 03:36 UTC
    Maybe something simple/stupid like:
    use CGI ':standard'; my $did_blank; while (<FH>) { chomp; if ($_) { print Tr(td("Checkbox"), td($_)); undef $did_blank; } else { print Tr({-bgcolor => '#006'}, td({-width=>298}, '&nbsp;') x 2) unless $did_blank++; } }
Re: Creating a table from text delimited by newlines
by chipmunk (Parson) on Nov 26, 2001 at 03:49 UTC
    I'm not completely certain what you want the result to be, but I think this code does something like what you're looking for:
    $line = ''; while (<FH>) { chomp; if (length) { $line .= "$_ "; } else { print <<"EOHTML"; <TR> <TD>Checkbox</TD> <TD>$line</TD> </TR> <TR bgcolor=#000066><TD WIDTH=298>&nbsp;</TD><TD WIDTH=298>&nbsp;</TD> +</TR> EOHTML $line = ''; } } if (length $line) { print <<"EOHTML"; <TR> <TD>Checkbox</TD> <TD>$line</TD> </TR> EOHTML }
    Note that I replaced the regex with a call to length. I think this is a clearer way to check whether the line contained any text (after chomping, of course).

    Your question would be clearer with sample input and output...

Re: Creating a table from text delimited by newlines
by Trimbach (Curate) on Nov 26, 2001 at 05:14 UTC
    ...or you could be clever. As I understand it you want all the text up to a blank line in a single row, with each blank row outputting a darker table row. If that's the task this'll work:
    my $file = join "", <FH>; my @rows = split /^\n/m, $file; for (@rows) { print Tr(td(["Checkboxes", split "\n"])); print Tr({-bgcolor=>"#000066"}, td({-width=>298}, ["&nbsp;", "&nbs +p;"])); print "\n"; }
    This is reason #27 why CGI.pm's HTML generation functions are just so damn cool when it comes to outputting truly dynamic HTML. If you haven't meditated on how Tr(td(\@array)) works you should consult the CGI.pm documentation and be enlightened.

    Gary Blackburn
    Trained Killer

Re: Creating a table from text delimited by newlines
by physi (Friar) on Nov 26, 2001 at 13:40 UTC
    Well I think you like to seperate Textblocks by a darkline, so this may also work:
    $/="\n\n"; #set the input Record seperator to + a double newline while (<FH>) { next if /^\n+$/; #ignore lines which contains only +newlines print "<TR><TD>Checkbox</TD> <TD>$_ </TD></TR>\n #print the textblock with an ... print <TR bgcolor=#000066> #apending darkline <TD WIDTH=298>&nbsp;</TD> <TD WIDTH=298>&nbsp;</TD> </TR>\n"; } close FH; ----------------------------------- --the good, the bad and the physi-- -----------------------------------

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://127485]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.