Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

INSERT file contents inside a table

by theravadamonk (Scribe)
on May 28, 2018 at 06:17 UTC ( [id://1215287]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,

I hv a /tmp/quarantine.ok file". It's contents are given below

May 3 15:58:17 SPAM sender@domain.com peter.w@recipient.com spam-Lub +_LynRmcvj.gz May 3 15:58:24 SPAM sender@domain1.com coev074@recipient1.com spam-g +SCAdAuMpiD0.gz May 3 15:59:27 BANNED sender@domain2.com nadia.meli@grecipient2.com +banned-BpF8aM7FM48B May 3 16:00:29 SPAM sender@domain.com sascha_ste@recipient.com spam- +6fFrrzUjgvug.gz May 3 16:01:48 INFECTED sender@domain3.com philipp_me@recipient.com +virus-SZLvi-VO4IIv May 4 16:02:28 SPAM sender@domain.com caro.grill@recipient3.com spam +-saYzvikxOfVL.gz May 4 16:02:45 SPAM sender@domain.com pellitter@recipient.com spam-8 +K649OHNlRQ2.gz May 4 16:03:14 SPAM sender@domain1.com anton.d@recipient.com spam-3r +pUGn3IcDnG.gz

I am currently displaying its contents with CGI via web browser. This is my code.

#!/usr/bin/perl use CGI ':standard'; use strict; use warnings; use CGI::Carp 'fatalsToBrowser'; # use only for testing $ENV{"PATH"} = "/usr/sbin:/usr/bin:/sbin:/bin"; print "Content-Type: text/html; charset=utf-8\n\n"; print "<body bgcolor=\"#95B8DB\" onload=\"parent.adjustMyFrameHeight() +;\">"; print "<div style=\"min-height:500px; dispaly:block;\">"; open FILE, "/tmp/quarantine.ok" or die $!; while(<FILE>) { chomp; print "$_\n\n"; print "<br />"; print "<br />"; print " \n"; } close FILE; print "</div>"; print "</body>";

Now, I want to INSERT file contents inside a table. I need these below table headers

TIME CATEGORY Sender Recipients Quarantined-ID . Under those I want to display table data.

Under time, Date and Time should displayed, Under CATEGORY, it's CATEGORY should be displayed ( whether its SPAM, BANNED or INFECTED). Under sender, email senders. Under recipients, recipients. Under Quarantined-ID, Quarantined-ID should be displayed.

How can I achieve it?

I came across this URL.

http://www.perlmonks.org/bare/?node_id=86850

But, I can't get it done.

Your ideas r welcome.

Replies are listed 'Best First'.
Re: INSERT file contents inside a table
by poj (Abbot) on May 28, 2018 at 06:58 UTC
    #!/usr/bin/perl use CGI ':standard'; use strict; use warnings; use CGI::Carp 'fatalsToBrowser'; # use only for testing my @cols = qw(TIME CATEGORY Sender Recipients Quarantined-ID); my $table = '<table border="1" cellspacing="0" cellpadding="5"><tr>'; $table .= join '',map{"<th>$_</th>"}@cols; $table .= '</tr>'; #open FILE, '<', '/tmp/quarantine.ok' or die $!; #while(<FILE>) { while (<DATA>){ chomp; next unless /\S/; # skip blank lines my @f = split ; my $date = join ' ',@f[0..2]; my $color = ($f[3] eq 'INFECTED') ? '#ffcccc' : '#ffffff'; $table .= qq!<tr style="font-family:courier" bgcolor="$color">!; $table .= join '',map{"<td>$_</td>"}$date,@f[3..6]; $table .= "</tr>"; } #close FILE; $table .= '</table>'; print header,start_html('Page Title'); print h1("Heading"); print $table; print end_html; __DATA__ May 3 15:58:17 SPAM sender@domain.com peter.w@recipient.com spam-Lub +_LynRmcvj.gz May 3 15:58:24 SPAM sender@domain1.com coev074@recipient1.com spam-g +SCAdAuMpiD0.gz May 3 15:59:27 BANNED sender@domain2.com nadia.meli@grecipient2.com +banned-BpF8aM7FM48B May 3 16:00:29 SPAM sender@domain.com sascha_ste@recipient.com spam- +6fFrrzUjgvug.gz May 3 16:01:48 INFECTED sender@domain3.com philipp_me@recipient.com +virus-SZLvi-VO4IIv May 4 16:02:28 SPAM sender@domain.com caro.grill@recipient3.com spam +-saYzvikxOfVL.gz May 4 16:02:45 SPAM sender@domain.com pellitter@recipient.com spam-8 +K649OHNlRQ2.gz May 4 16:03:14 SPAM sender@domain1.com anton.d@recipient.com spam-3r +pUGn3IcDnG.gz
    poj

      Hi, Many thanks for the code. it really works as I expected. But I have MADE a small mistake.

      Under recipients, there r many due to multiple Tos and CCs. When there r multiple recipients it Won't work.

      multiple recipients display in this way.

      May 5 10:39:42 SPAM sender@domain1.com amil@recipient.com , dab@recip +ient.com , jay@recipient.com , sa@recipient.com , wid@recipient.com +spam-Nnlc4bqiNeQD.gz

      here's full data

      __DATA__ May 3 15:58:17 SPAM sender@domain.com peter.w@recipient.com spam-Lub +_LynRmcvj.gz May 3 15:58:24 SPAM sender@domain1.com coev074@recipient1.com spam-g +SCAdAuMpiD0.gz May 3 15:59:27 BANNED sender@domain2.com nadia.meli@grecipient2.com +banned-BpF8aM7FM48B May 3 16:00:29 SPAM sender@domain.com sascha_ste@recipient.com spam- +6fFrrzUjgvug.gz May 3 16:01:48 INFECTED sender@domain3.com philipp_me@recipient.com +virus-SZLvi-VO4IIv May 4 16:02:28 SPAM sender@domain.com caro.grill@recipient3.com spam +-saYzvikxOfVL.gz May 4 16:02:45 SPAM sender@domain.com pellitter@recipient.com spam-8 +K649OHNlRQ2.gz May 4 16:03:14 SPAM sender@domain1.com anton.d@recipient.com spam-3r +pUGn3IcDnG.gz May 5 10:39:42 SPAM sender@domain1.com amil@recipient.com , dab@recip +ient.com , jay@recipient.com , sa@recipient.com , wid@recipient.com +spam-Nnlc4bqiNeQD.gz May 5 11:00:41 SPAM sender@domain.com amil@recipient.com , duminda@re +cipient.com , samindak@recipient.com , vajirak@recipient.com spam-gj +WpzbXc8o1q.gz May 6 01:07:04 SPAM sender@domain1.com amil@recipient.com , samindak@ +recipient.com spam-x-vwwUP5yfqJ.gz May 7 12:54:26 SPAM sender@domian.com amil@recipient.com spam-o6LPjf +Cnz211.gz

      I can understand this below line

      my $date = join ' ',@f[0..2];

      I think I hv to change below code

      $table .= join '',map{"<td>$_</td>"}$date,@f[3..6];

      How to display @f3..6; @f3..4 is OK. @f5 is the issue. will last (Quarantined-ID) become @f6?

      I am still a novice. I still can't decide how to change the code? waiting for your INPUTS?

        See perldata  $#days  # the last index of array @days

        while (<DATA>){ chomp; next unless /\S/; # skip blank lines my @f = split /[ ,]+/; my $n = $#f; my $date = join ' ',@f[0..2]; my $recip = join ' , ',@f[5..$n-1]; my $id = $f[$n]; # last col my $color = ($f[3] eq 'INFECTED') ? '#ffcccc' : '#ffffff'; $table .= qq!<tr style="font-family:courier" bgcolor="$color"> <td>$date</td> <td>$f[3]</td> <td>$f[4]</td> <td>$recip</td> <td>$id</td> </tr>!; $table .= "</tr>"; }
        poj

        A modification of my solution below would be to change the way the data is split up and put each row into a hash with an array of recipients

        my @cols = split(/[,\s]+/); my %row; $row{quarantined_id} = pop @cols; for my $key ( qw( month day time category sender ) ) { $row{$key} = shift @cols; } $row{recipients} = \@cols; push @{$data{rows}}, \%row; ... [% FOR row=rows %] <tr> <td>[% row.month %] [% row.day %] [% row.t +ime %]</td> <td>[% row.category %]</td> <td>[% row.sender %]</td> <td>[% FOR recipient=row.recipients %][% r +ecipient %]<br />[% END %]</td> <td>[% row.quarantined_id %]</td> </tr> [% END %]

        Full example

Re: INSERT file contents inside a table
by rnewsham (Curate) on May 28, 2018 at 08:39 UTC

    I always recommend not trying to print out html in the middle of perl code. It looks messy is a nightmare to maintain. Much better to break the html out into a template. If you want to keep it as a single script you can have the template in the cgi it still provides a level of separation. In my opinion something like this is much easier to read and maintain.

    #!/usr/bin/perl use CGI ':standard'; use strict; use warnings; use CGI::Carp 'fatalsToBrowser'; # use only for testing use Template; my $cgi = new CGI; $cgi->header( -charset => 'utf-8' ); $ENV{"PATH"} = "/usr/sbin:/usr/bin:/sbin:/bin"; my %data; while(<DATA>) { chomp; my @columns = split; push @{$data{rows}}, \@columns } close FILE; my $tt = new Template; my $template = q( <!DOCTYPE html> <html> <head> <title>PM 1215287</title> </head> <body onload="parent.adjustMyFrameHeight();"> <div id='wrapper'> <table> <thead> <tr> <th>Time</th> <th>Category</th> <th>Sender</th> <th>Recipients</th> <th>Quarantined-ID</th> </tr> </thead> <tbody> [% FOR row=rows %] <tr> <td>[% row.0 %] [% row.1 %] [% row.2 %]</t +d> <td>[% row.3 %]</td> <td>[% row.4 %]</td> <td>[% row.5 %]</td> <td>[% row.6 %]</td> </tr> [% END %] </tbody> </table> </div> <style> body { background-color : #95B8DB; } #wrapper { min-height:500px; display:block; } table, tr, th, td { border : 1px solid grey; } </style> </body> </html> ); $tt->process( \$template, \%data ) or die $tt->error; __DATA__ May 3 15:58:17 SPAM sender@domain.com peter.w@recipient.com spam-Lub +_LynRmcvj.gz May 3 15:58:24 SPAM sender@domain1.com coev074@recipient1.com spam-g +SCAdAuMpiD0.gz May 3 15:59:27 BANNED sender@domain2.com nadia.meli@grecipient2.com +banned-BpF8aM7FM48B May 3 16:00:29 SPAM sender@domain.com sascha_ste@recipient.com spam- +6fFrrzUjgvug.gz May 3 16:01:48 INFECTED sender@domain3.com philipp_me@recipient.com +virus-SZLvi-VO4IIv May 4 16:02:28 SPAM sender@domain.com caro.grill@recipient3.com spam +-saYzvikxOfVL.gz May 4 16:02:45 SPAM sender@domain.com pellitter@recipient.com spam-8 +K649OHNlRQ2.gz May 4 16:03:14 SPAM sender@domain1.com anton.d@recipient.com spam-3r +pUGn3IcDnG.gz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found