Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Newbie question, advice appreciated

by injunjoel (Priest)
on Sep 27, 2005 at 19:59 UTC ( #495532=note: print w/replies, xml ) Need Help??

in reply to Newbie question, advice appreciated

Greetings all,
One other suggestion is to use HTML::Template, if you have access to CPAN that is. Im not sure how you want to handle the lines but here is a quick mockup that may or may not work for your purposes.
#!/usr/bin/perl -w use strict; use HTML::Template; my $output_template = qq* <table border="0" width="100%" cellspacing="1" bgcolor="#000000"> <tr bgcolor="#cccccc"> <td>Function Name:</td><td>Description</td> </tr> <TMPL_LOOP NAME="functions"> <tr bgcolor="#ffffff"> <td valign="top"><TMPL_VAR NAME="name"></td><td valign="top">< +TMPL_VAR NAME="desc"></td> </tr> </TMPL_LOOP> </table> *; my @output = do{ local @_; #you will need to replace the DATA handle with #one to your file of interest. my $string = do{ local undef $/; <DATA> }; while($string =~ /\s?STANDARD\s+(.*?)\nfunction\s(\S+)\n?/mg){ push @_, {name=>$2, desc=>$1}; } @_; }; my $tmplt = HTML::Template->new(scalarref=>\$output_template,die_on_ba +d_params=>0); $tmplt->param({functions=>\@output}); print $tmplt->output(); exit; __DATA__ blah blah blahblah blah blah blah blah blahblah blah blah # STANDARD This is some text that describes function seventhdoesnotgetenoughsleep # STANDARD This is some describing text function seventhgetsomesleep blah blah blahblah blah blah # STANDARD This is some description function seventhgethomesafe blah blah blahblah blah blah # STANDARD This is some text ...nuff said function seventhwhyiraq blah blah blahblah blah blah

the output is
Function Name:Description
seventhdoesnotgetenoughsleepThis is some text that describes
seventhgetsomesleepThis is some describing text
seventhgethomesafeThis is some description
seventhwhyiraqThis is some text ...nuff said

Now in your code you will need to change the <DATA> part to read in your file and as dragonchild illustrated, TEST IF OPEN WORKED! Sorry for the caps but its a necessary step. Also you will need to open another handle (and check if it opened) to your output file for writing. Is that what you were thinking?
I hope that helps

This approach assumes that the file size is not enormous since it reads the entire file into $string. If the files are rather large you probably want to go with a line by line approach.
I updated the @output do block code to match across newlines.
Removed obsolete code from do block my @data = split /\#/, $string; .

"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://495532]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2019-07-20 17:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found