Domanda

Nel mio codice, voglio visualizzare tutti i dati da un CSV in forma di tabella, ma visualizza solo l'ultima riga. Che ne dici delle linee 1 e 2? Ecco i dati:

1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh

Ecco il codice:

#! /usr/bin/perl
print "Content-type:text/html\r\n\r\n";
use CGI qw(:standard);
use strict;
use warnings;

my $line;
my $file;
my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19);

$file='MyFile.txt';
open(F,$file)||die("Could not open $file");
while ($line=<F>)
{

($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19)= split ',',$line;

}

close(F);

print "<HTML>";
print "<head>";
print "<body bgcolor='#4682B4'>";
print "<title>FUSION SHIFT REPORT</title>";
print "<div align='left'>";
print "<TABLE CELLPADDING='1' CELLSPACING='1' BORDER='1' bordercolor=black width='100%'>";
print "<TR>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>RECORD No.</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>TESTER No.</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>DATE</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>TIME</td>";
print "<td width='11%'bgcolor='#00ff00'><font size='2'>DEVICE NAME</td>";
print "<td bgcolor='#00ff00'><font size='2'>TEST PROGRAM</td>";
print "<td bgcolor='#00ff00'><font size='2'>DEVICE FAMILY</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>SMSLOT</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>DIE LOT</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>LOADBOARD</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>TESTER </td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>SERIAL NUMBER</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>TESTER CONFIG</td>";
print "<td width='11%'bgcolor='#00ff00'><font size='2'>SMSLOT</td>";
print "<td bgcolor='#00ff00'><font size='2'>PACKAGE</td>";
print "<td bgcolor='#00ff00'><font size='2'>SOCKET</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 1</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 2</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 3</td>";
print "</tr>";
print "<TR>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f1</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f2</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f3</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f4</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f5</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f6</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f7</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f8</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f9</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f10</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f11</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f12</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f13</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f14</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f15</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f16</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f17</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f18</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f19</TD>";
print "</tr>";

print "</TABLE>";

print "</body>";
print "<html>";
È stato utile?

Soluzione

HTML :: Template renderebbe la tua vita molto più semplice. Ecco il mio go con un modello ridotto.

#!/usr/local/bin/perl

use strict;
use warnings;

use HTML::Template;

my @table;
while (my $line = <DATA>){
    chomp $line;
    my @row = map{{cell => $_}} split(/,/, $line);
    push @table, {row => \@row};
}

my $tmpl = HTML::Template->new(scalarref => \get_tmpl());
$tmpl->param(table => \@table);
print $tmpl->output;

sub get_tmpl{
    return <<TMPL
<html>
<TMPL_LOOP table>
<tr>
<TMPL_LOOP row>
<td><TMPL_VAR cell></td></TMPL_LOOP>
</tr></TMPL_LOOP>
</html>
TMPL
}

__DATA__
1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh

Altri suggerimenti

Devi generare le righe della tabella dentro il ciclo while, poiché è lì che stai leggendo le righe.

Quindi cambia il codice in modo che

  • restituisce le intestazioni di tabella
  • legge il file riga per riga emettendo righe di tabella
  • genera piè di pagina di output

Ecco come potrebbe apparire il tuo loop se un po 'semplificato ...

while ($line=<F>)
{  
    print "<tr>";
    my @cells= split ',',$line;
    foreach my $cell (@cells)
    {
       print "<td>$cell</td>";
    }
    print "</tr>";
}
$f1,$f2,$f3,$f4

Ogni volta che vedi un codice come quello, le campane di allarme dovrebbero suonare. Usa un array.

Mi permetta di dimostrare con un esempio più piccolo.

my $f;
while ($line = <F>) {
    $f = $line;
}
print $f;

Il codice sopra leggerà ogni riga del file F, assegnando ogni riga alla variabile $f. Ogni volta che assegna una nuova riga, la riga precedente viene sovrascritta. Quando raggiunge la fine del file, stampa il valore di print una volta.

my $f;
while ($line = <F>) {
    $f = $line;
    print $f;
}

Il codice sopra ha <=> all'interno del ciclo, quindi <=> verrà eseguito per ogni riga dell'input. Ti consigliamo di apportare una modifica simile al tuo codice per ottenere l'output che ti aspetti.

La ragione per cui stavi vedendo solo l'ultima riga era che hai eseguito la stampa dopo aver letto l'intero file e scartato ogni riga quando è stata letta la successiva.

Ad essere onesti, ci sono varie cose che non vanno in quel codice. Ne citerò un paio e affronterò quelli nello snippet di codice qui sotto.

  1. Non analizzare i dati CSV da soli (la divisione). Usa Text :: CSV o - se hai bisogno di prestazioni migliori - Testo :: CSV_XS .
  2. Il tuo HTML non è valido. Non chiudi tutti i tag e, in particolare, il titolo dovrebbe essere in testa, ma non in corpo.
  3. Dovresti usare un modulo di template per inserire dati tabulari in una pagina web. Esempi: HTML :: Template o Template Toolkit .
  4. Per lo meno, utilizzare le funzioni per sottrarre il codice ridondante.
  5. Meglio ancora, usa il fatto che anche la formattazione dei dati è data. Ecco perché dovrebbe essere trattato come tale.
  6. Se ti accorgi che stai dichiarando molte variabili con numeri associati ($f1, $f2, ...), vuoi davvero un array: @fields = split /,/, $line;

use strict;
use warnings;
use CGI qw();
use Text::CSV;

my $cgi = CGI->new();
print $cgi->header();

my $file ='MyFile.txt';

# should really use a templating module from CPAN,
# but let's take it step by step.
# Any of the following would fit nicely:
# - Template.pm (Template Toolkit)
# - HTML::Template, etc.
my $startHtml = <<'HERE';
<html>
<head> <title>FUSION SHIFT REPORT</title> </head>
<body bgcolor="#4682B4">
<div align="left">
<table cellpadding="1" cellspacing="1" border="1" bordercolor="black" width="100%">
HERE
my $endHtml = <<'HERE';

</table>

</body>
<html>
HERE

my @columns = (
  { name => 'RECORD No.', width => 12 },
  { name => 'TESTER No.', width => 12 },
  { name => 'DATE', width => 12 },
  { name => 'TIME', width => 13 },
  { name => 'DEVICE NAME', width => 11 },
  { name => 'TEST PROGRAM' },
  { name => 'DEVICE FAMILY' },
  { name => 'SMSLOT', width => 13 },
  { name => 'DIE LOT', width => 13 },
  { name => 'LOADBOARD', width => 12 },
  { name => 'TESTER', width => 12 },
  { name => 'SERIAL NUMBER', width => 12 },
  { name => 'TESTER CONFIG', width => 13 },
  { name => 'SMSLOT', width => 11 },
  { name => 'PACKAGE' },
  { name => 'SOCKET' },
  { name => 'ROOT CAUSE 1', width => 13 },
  { name => 'ROOT CAUSE 2', width => 13 },
  { name => 'ROOT CAUSE 3', width => 13 },
);


my $csv = Text::CSV->new();
open my $fh, '<', $file
  or die "Could not open file '$file': $!"; # should generate a HTML error here

# print header
print $startHtml;
print_table_header(\@columns);

while (defined(my $line = <$fh>)) {
  $csv->parse($line);
  # watch out: This may be "tainted" data!
  my @fields = $csv->fields();
  @fields = @fields[0..$#columns] if @fields > @columns;
  print_table_line(\@fields);
}

close $fh;

print $endHtml;




sub print_table_header {
  my $columns = shift;
  print "<tr>\n";
  foreach my $column (@$columns) {
    my $widthStr = (defined($column->{width}) ? ' width="'.$column->{width}.'"' : '');
    my $colName = $column->{name};
    print qq{<td$widthStr bgcolor="#00FF00"><font size="2">$colName</font></td>\n};
  }
  print "</tr>\n";
}

sub print_table_line {
  my $fields = shift;
  print "<tr>\n";
  foreach my $field (@$fields) {
    print qq{<td bgcolor=\"#ADD8E6\"><font size="2">$field</font></td>\n};
  }
  print "</tr>\n";
}

Chiudi < font > tag. Solo perché il browser gestirà la loro mancanza non significa che non siano preziosi da includere.

Questa non è la soluzione più solida, in realtà ha degli errori piuttosto spiacevoli se hai delle virgole all'interno dei valori, ma ha fatto il lavoro per me:

(i dati CSV sono in una variabile chiamata $ content)

$content =~ s#\n#</td></tr><tr><td>#g;
$content =~ s#,#</td><td>#g;
$content = "<table><tr><td>$content</td></tr></table>";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top