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

Hello , I am new to perl and I wrote a code that gives me the array of my data which has details like this :
Name-stage-type
each data has two single Name, many stages and 2 types for every stage , i.e new and old. Old and New has either true or false value as output which I am getting from some other part of the program,
Example: tony-skill-old tony-skill-new tony-skill1-old tony-skill1-new martin-skill-old martin-skill-new
I have also coded the program to fill the data in a HTML file , but I am it's not working according to my requirement. Let say I want to fill the data like this :
<!DOCTYPE html> <html> <body> <table style="width:300px"> <tr> <td>Name</td> <td>Stage</td> <td>Old</td> <td>New</td> </tr> <tr> <td>tony</td> <td>skill</td> <td>True</td> <td>True</td> </tr> <tr> <td></td> <td>skill1</td> <td>False</td> <td>False</td> </tr> <tr> <td>Martin</td> <td>skill</td> <td>False</td> <td>True</td> </tr> </table> </body> </html> <code> You can check this code here : <code> http://www.w3schools.com/html/tryit.asp?filename=tryhtml_table
As you will see thru from above code , The Name is printed once then the Stages and then the types for each stage , in my program due to two types the loop is running twice and printing the data in two different lines for a single stage . I just need idea on how to print project name once then all the stages and their type status , every stage should be in a single line . Sorry for the bad explaination

Replies are listed 'Best First'.
Re: Filling Table in a HTML file using perl
by kcott (Archbishop) on Mar 05, 2014 at 02:11 UTC

    G'day farha89,

    You don't show the source of the data nor the data structure you've used nor the code you've written to create and populate that structure. I suggest the following and leave the coding to you:

    { name => { stage0 => [old, new], ..., stageN => [old, new], }, ..., }

    Using that structure, generating the HTML is straightforward (I've only shown the <table>...</table> part):

    #!/usr/bin/env perl use strict; use warnings; my %data = ( tony => { skill => [ 1, 1 ], skill1 => [ 0, 0 ], }, martin => { skill => [ 1, 0 ], }, ); print qq{<table border="1" cellpadding="2" cellspacing="0">\n}; print "<tr> <th>Name</th> <th>Stage</th> <th>Old</th> <th>New</th> </tr>\n"; for my $name (sort keys %data) { print "<tr>\n"; my $rowspan = keys %{$data{$name}}; print qq{ <td rowspan="$rowspan">\u$name</td>\n}; for my $stage (sort keys %{$data{$name}}) { print " <td>$stage</td>\n"; for (@{$data{$name}{$stage}}) { my $bool = $_ ? 'True' : 'False'; print " <td>$bool</td>\n"; } --$rowspan && print "</tr>\n<tr>\n"; } print "</tr>\n"; } print "</table>\n";

    Output:

    <table border="1" cellpadding="2" cellspacing="0"> <tr> <th>Name</th> <th>Stage</th> <th>Old</th> <th>New</th> </tr> <tr> <td rowspan="1">Martin</td> <td>skill</td> <td>True</td> <td>False</td> </tr> <tr> <td rowspan="2">Tony</td> <td>skill</td> <td>True</td> <td>True</td> </tr> <tr> <td>skill1</td> <td>False</td> <td>False</td> </tr> </table>

    Which renders like this:

    Name Stage Old New
    Martin skill True False
    Tony skill True True
    skill1 False False

    -- Ken

Re: Filling Table in a HTML file using perl
by tangent (Parson) on Mar 05, 2014 at 02:18 UTC
    Here's a way to do it which utilises the power of Perl's arbitrary data structures. Anything you don't understand just ask :)
    use strict; use warnings; use Data::Dumper; my @AoA = ( ['tony','skill','old'], ['tony','skill','new'], ['tony','skill1','old'], ['tony','skill1','new'], ['martin','skill','old'], ['martin','skill','new'], ['martin','skill1','old'], ); my (@names,%hash); for my $ary (@AoA) { my ($name,$stage_name,$type) = @$ary; push(@names,$name) unless $hash{$name}; push(@{ $hash{$name}{'stages'} },$stage_name) unless $hash{$name}{$stage_name}; $hash{$name}{$stage_name}{$type}++; } # to see the %hash data structure: print Dumper (\%hash); print qq|<table style="width:300px"> <tr><th>Name</th><th>Stage</th><th>Old</th><th>New</th></tr>\n|; for my $name (@names) { my $print_name = $name; my $stages = $hash{$name}{'stages'}; for my $stage_name (@$stages) { my $stage = $hash{$name}{$stage_name}; $stage->{'old'} = ($stage->{'old'} ? 'True' : 'False'); $stage->{'new'} = ($stage->{'new'} ? 'True' : 'False'); print qq|<tr> <td>$print_name</td> <td>$stage_name</td> <td>$stage->{'old'}</td> <td>$stage->{'new'}</td> </tr>\n|; $print_name = ''; } } print qq|</table>|;

    Output (border added for clarity):

    NameStageOldNew
    tony skill True True
    skill1 True True
    martin skill True True
    skill1 True False
      Hey Thanks both of you for guiding me in a right direction , I didn't share my code coz it's so messy and I don't think it's easy to understand I didn't use any object orented and hashes data structure I just used the for loop with the limit of number of data. then I split the name , stage and type from each data , but I was scanning the status of data in that for loop too . Secondly True or False is not boolean value , It's a text lets say correct or wrong . Thanks again , I will ammend my code and try to update you with my results :)
Re: Filling Table in a HTML file using perl
by Anonymous Monk on Mar 05, 2014 at 15:38 UTC
    Also it is a very good idea to look at things like Template, or better yet Template::Tutorial::Web, which let you describe the HTML that you want to produce, apart from the underlying program that collects or generates the data. As you will see if you browse through the examples of this (very large ...) package, you could write your program to build up the data to be printed as an array-of-hashes, then use a template containing a [% FOREACH %] loop. It is often the case that you will need to change the HTML presentation of data many times, and that changing the structure of a Perl program in order to do that is difficult. Instead, you can write the program to generate the data, then change the template as often as the Marketing Department requires.