Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how to re-use "Formats"

by benlaw (Scribe)
on Dec 17, 2008 at 11:03 UTC ( [id://730893]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I insert a "Format" into function and reuse this function with different @array, the first return result is pretty good, but the second is worse ~~ no format_top and all format loss..~what happen for it , how to re-use the "Format"??? Thanks
#! perl -w sub WRITE_REPORT{ my ($cat,$name) = @_; my $filename = "$name\_Joblog\.html"; open(OUTPUTLOG, ">$filename") or die "Can't open up myfile: $!\n"; my $jobset = "CATCAT"; format OUTPUTLOG= <tr> <td> @<<<<<< </td> <td> @* </td> <td> @* </td> <td> @* </td> <td> @* < +/td> $a, $b, $c, $d, $e </tr> . format OUTPUTLOG_TOP = @<<<<<<<<<< $jobset <table border="2"> <tr> <th>Job</th> <th>Status</th> <th>Start</th> <th>End</th> <th>Remark</th> </tr> . ########## Control the lines , 3 line must be used!! $old = select OUTPUTLOG; $= = 200; select $old; foreach (@{$cat}){ my @temp = split(/\,/, $_); $a = "$temp[0]"; $b = "$temp[1]"; $c = "$temp[2]"; $d = "$temp[3]"; $e = "$temp[4]"; write OUTPUTLOG; } close(OUTPUTLOG); } my (@x, @y) = (); while(<DATA>){ if(/^AA/){ push (@x, $_) }elsif(/^BB/){ push(@y, $_); } } WRITE_REPORT(\@x, "AA"); WRITE_REPORT(\@y, "BB"); close(DATA) __DATA__ AA,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, AA,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, AA,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, AA,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, AA,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, AA,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, AA,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, AA,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, AA,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, AA,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, BB,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33,

Replies are listed 'Best First'.
Re: how to re-use "Formats"
by cdarke (Prior) on Dec 17, 2008 at 12:19 UTC
    You are setting $= to 200 but writing less than 200 lines, so you need to reset the line count. Place the following at the start of the WRITE_REPORT subroutine:
    my $old = select OUTPUTLOG; $- = 0; select $old;
    (and use strict; might be a good idea as well)
Re: how to re-use "Formats"
by GrandFather (Saint) on Dec 17, 2008 at 20:42 UTC

    The simple answer is, don't use format, use HTML::Template or one of the other templating solutions available from CPAN. Consider:

    use strict; use warnings; use HTML::Template; my $template = <<'TEMPLATE'; <table border="2"> <tr> <th>Job</th> <th>Status</th> <th>Start</th> <th>End</th> <th>Remark</th> </tr> <TMPL_LOOP NAME=rows> <tr><TMPL_LOOP NAME=row><td><TMPL_VAR NAME=a></td></TMPL_LOOP></tr> </TMPL_LOOP> TEMPLATE my @x; my @y; while(<DATA>){ if(/^AA/){ push @x, {row => [map {{a => $_}} split ',']}; }elsif(/^BB/){ push @y, {row => [map {{a => $_}} split ',']}; } } writeReport (\@x, "AA") if @x; writeReport (\@y, "BB") if @y; sub writeReport { my ($cat, $name) = @_; my $t = HTML::Template->new (scalarref => \$template); $t->param (rows => $cat); print $t->output(); } __DATA__ AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33, BB,data5,NORMAL,13/08/2008 16:00:31,20/08/2008 16:00:33, BB,data4,NORMAL,14/08/2008 16:00:31,32/08/2008 16:00:33, BB,data3,NORMAL,15/08/2008 16:00:31,42/08/2008 16:00:33, AA,data2,NORMAL,16/08/2008 16:00:31,52/08/2008 16:00:33, BB,data1,NORMAL,12/08/2008 16:00:31,12/08/2008 16:00:33,

    Prints:

    <table border="2"> <tr> <th>Job</th> <th>Status</th> <th>Start</th> <th>End</th> <th>Remark</th> </tr> <tr><td>AA</td><td>data2</td><td>NORMAL</td><td>16/08/2008 16:00:31</t +d><td>52/08/2008 16:00:33</td><td> </td></tr> <tr><td>AA</td><td>data2</td><td>NORMAL</td><td>16/08/2008 16:00:31</t +d><td>52/08/2008 16:00:33</td><td> </td></tr> <tr><td>AA</td><td>data2</td><td>NORMAL</td><td>16/08/2008 16:00:31</t +d><td>52/08/2008 16:00:33</td><td> </td></tr> <table border="2"> <tr> <th>Job</th> <th>Status</th> <th>Start</th> <th>End</th> <th>Remark</th> </tr> <tr><td>BB</td><td>data1</td><td>NORMAL</td><td>12/08/2008 16:00:31</t +d><td>12/08/2008 16:00:33</td><td> </td></tr> <tr><td>BB</td><td>data5</td><td>NORMAL</td><td>13/08/2008 16:00:31</t +d><td>20/08/2008 16:00:33</td><td> </td></tr> <tr><td>BB</td><td>data4</td><td>NORMAL</td><td>14/08/2008 16:00:31</t +d><td>32/08/2008 16:00:33</td><td> </td></tr> <tr><td>BB</td><td>data3</td><td>NORMAL</td><td>15/08/2008 16:00:31</t +d><td>42/08/2008 16:00:33</td><td> </td></tr> <tr><td>BB</td><td>data1</td><td>NORMAL</td><td>12/08/2008 16:00:31</t +d><td>12/08/2008 16:00:33</td><td> </td></tr>

    Perl's payment curve coincides with its learning curve.
Re: how to re-use "Formats"
by poolpi (Hermit) on Dec 17, 2008 at 16:31 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://730893]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2026-02-07 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    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.