in reply to Filling Table in a HTML file using perl

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

Replies are listed 'Best First'.
Re^2: Filling Table in a HTML file using perl
by farha89 (Initiate) on Mar 05, 2014 at 12:01 UTC
    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 :)