Link to home
Start Free TrialLog in
Avatar of areyouready344
areyouready344Flag for United States of America

asked on

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.
Avatar of sjklein42
sjklein42
Flag of United States of America image

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

Avatar of areyouready344

ASKER

thanks but did not work, got the output of

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

I'll try it again later
ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
excellent answer