views:

65

answers:

1

I have a Perl hash of "people" like this:

my $data = {
    124535 => {
        NAME     => "abe",
        AGE      => 100,
        SEX      => "m",
        HOMEPAGE => qw (http://abe.knaan.old)
    },
    54478 => {
        NAME     => "joe",
        AGE      => 18,
        SEX      => "m",
        HOMEPAGE => qw (http://slappy.joe.com)
    },
    54478 => {
        NAME     => "jane",
        AGE      => 20,
        SEX      => "f",
        HOMEPAGE => qw (http://i.am.jane/jane.html)
    },
};

I would like to print an HTML page with a table of all people, one row per person, with all its data including the hash key (i.e. 5 columns), including a hyperlinks to it's homepage.

I can write a long piece of ugly code that prints all the HTML headers etc., but is there a nicer, cleaner way to that? Perhaps using some modules for this, I guess, quite popular task?

I found HTML::QuickTable but I'm not sure how to convert my structure to an appropriate one.

+3  A: 

This should do it:

my @names = qw(NAME AGE SEX HOMEPAGE);
my @data  = [@names, 'KEY'];

for my $k (keys %$data) {
    my @t = @{$data->{$k}}{@names};
    $t[-1] = qq{<a href="$t[-1]">$t[-1]</a>};        
    push @data, [@t, $k]
}        

use HTML::QuickTable;
my $qt = HTML::QuickTable->new(... labels => 1); 
print $qt->render(\@data); 
eugene y
+1 that's a good start, but the hompepages are still text, not links.
David B
@David: see the updated answer
eugene y
almost perfect! but we're still missing the hash key (we need to have 5 columns)
David B
David: oops. updated
eugene y
@eugene y: thank you very much!
David B