• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

Convert records into html table using Perl

I have the following data record in a scsi_test file that I would like to convert into a html table using perl

Here's the data record....

__Data__
scsi_test_hotswap
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld

__Data__
scsi_test_memory_leakage
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
3. dldldld - dldldldldldldldld
4. dldldld - dldldldldlldldldld

__Data__
scsi_test_scatter_gatter


Starting Code...
--------------------
#!/usr/bin/perl

use warnings;
use strict;

# Html table tag variables

my $open_table ="<table border=\"1\">";
my $close_table ="</table>";
my $open_row ="<tr>";
my $close_row ="</tr>";
my $open_column_header ="<th>";
my $close_column_header ="</th>";
my $open_column_data ="<td>";
my $close_column_data ="</td>";
my $open_color_blue ="<font color=\"#0000ff\">";
my $close_color_blue ="</font>";
my $blank_column ="<tr></tr>";


my @first_record_row; # this should include the first element and element starting with 1. from the whole_record array
                        excluding the __Data__ input record separator.
my @additional_record_rows; # this should include the remaining elements in the whole_record array.

# Input Record Separator
$/='__Data__';


# Read In file

open FH,'<',"scsi_test" or die $!;


while(<FH>)
{
        s/^\s+//;
        s/^\s+$//;

   
    print "@whole_record","\n";

    foreach (@whole_record)
    {
       print $whole_record[1],"\n";
      # push @first_record_row = grep(m/^1\..*/,@whole_record);
      # print @first_record_row;

    }


Output should look like this....

row_1 -----> scsi_test_hotswap                  Passed          1. dldldld - dldldldldldldldld
row_2 ----->                                                                      2. dldldld - dldldldldldldldld
row_3 ----->                                                                      3. dldldld - dldldldldldldldld
row_4 ----->                                                                      4. dldldld - dldldldldldldldld
row_5 -----> blank line row
row_6 -----> scsi_test_memory_leakage     Passed          1. dldldld - dldldldldldldldld          
row_7 ----->                                                                       2. dldldld - dldldldldldldldld
row_8 ----->                                                                       3. dldldld - dldldldldldldldld

Side note, the text upto the hypen should be color blue blod as in, dldldld -, and excluding the line number.
0
areyouready344
Asked:
areyouready344
  • 2
  • 2
1 Solution
 
sjklein42Commented:
This should give you a start.

To make it easier to test I changed it so it takes input from stdin, so run it like this:

    perl makeTab.pl scsi.test >scsiTest.html

#!/usr/bin/perl


# Html table tag variables

my $open_table ="<table border=\"1\">";
my $close_table ="</table>";
my $open_row ="<tr>";
my $close_row ="</tr>";
my $open_column_header ="<th>";
my $close_column_header ="</th>";
my $open_column_data ="<td>";
my $close_column_data ="</td>";
my $open_color_blue ="<font color=\"#0000ff\">";
my $close_color_blue ="</font>";
my $blank_column ="<tr></tr>";


print $open_table;

while(<>)
{
	s/[\r\n]//g;
	if ( /__Data__/ )
	{
		$testname = <>;
		if ( $testname eq '' ) { last; }
		$testname =~ s/[\r\n]//g;
		
		$stopline = <>;
		if ( $stopline eq '' ) { last; }
		$stopline =~ s/[\r\n]//g;
		if ( $stopline ne 'stop' ) { die "expected stop\n"; }

		$pass = <>;
		if ( $pass eq '' ) { last; }
		$pass =~ s/[\r\n]//g;

		while ( <> )
		{
			if ( ! ( /^[0-9]/ ) ) { last; }

			$detailline = $_;

			print $open_row;
			print $open_column_data;
			print $testname;
			print $close_column_data . $open_column_data;
			print $pass;
			print $close_column_data . $open_column_data;
			print $detailline;
			print $close_column_data . $close_row;


		}
	}
}

print $close_table;

Open in new window

0
 
areyouready344Author Commented:
thanks but did not work, got the output of

<table border="1"><table border="1">

I'll try it again later
0
 
sjklein42Commented:
A little cleaner.

#!/usr/bin/perl


# Html table tag variables

my $open_table ="<table border=\"1\">";
my $close_table ="</table>";
my $open_row ="<tr>";
my $close_row ="</tr>";
my $open_column_header ="<th>";
my $close_column_header ="</th>";
my $open_column_data ="<td>";
my $close_column_data ="</td>";
my $open_color_blue ="<font color=\"#0000ff\">";
my $close_color_blue ="</font>";
my $blank_column ="<tr></tr>";


print $open_table;

while(<>)
{
	s/[\r\n]//g;
	if ( /__Data__/ )
	{
		$testname = <>;
		if ( $testname eq '' ) { last; }
		$testname =~ s/[\r\n]//g;
		
		$stopline = <>;
		if ( $stopline eq '' ) { last; }
		$stopline =~ s/[\r\n]//g;
		if ( $stopline ne 'stop' ) { die "**** expected stop line not founc\n"; }

		$pass = <>;
		if ( $pass eq '' ) { last; }
		$pass =~ s/[\r\n]//g;

		while ( <> )
		{
			if ( ! ( /^[0-9]/ ) ) { $quit = 1; last; }

			$detailline = $_;
			$detailline =~ s/[\r\n]//g;

			print $open_row;
			print $open_column_data;
			print $testname;
			print $close_column_data . $open_column_data;
			print $pass;
			print $close_column_data . $open_column_data;
			print $detailline;
			print $close_column_data . $close_row;
			print "\n";
		}
		if ( $quit && ( $_ eq '' ) ) { last; }
	}
}

print $close_table;

Open in new window


The output:

c:\temp>perl makeTab.pl scsi_test
<table border="1"><tr><td>scsi_test_hotswap</td><td>Passed</td><td>1. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_hotswap</td><td>Passed</td><td>2. dldldld - dldldldldlldldldld</td></tr>
<tr><td>scsi_test_hotswap</td><td>Passed</td><td>1. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_hotswap</td><td>Passed</td><td>2. dldldld - dldldldldlldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>1. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>2. dldldld - dldldldldlldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>3. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>4. dldldld - dldldldldlldldldld</td></tr>
</table>
c:\temp>

Open in new window

0
 
areyouready344Author Commented:
excellent answer
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now