Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Excel::Template and a hash of arrays of hashes

by wfsp (Abbot)
on Dec 29, 2006 at 17:11 UTC ( [id://592252]=note: print w/replies, xml ) Need Help??


in reply to Excel::Template and a hash of arrays of hashes

I can't help directly but if, as the docs say, you're looking for a data structure similar to that used by HTML::Template this may help get you started.

#!/usr/bin/perl use strict; use warnings; use HTML::Template; use Data::Dumper; $Data::Dumper::Indent = 1; my %h = get_hash(); my @outer; for my $rep (keys %h){ my @inner; for my $detail (@{$h{$rep}}){ push @inner, $detail; } push @outer, { rep => $rep, inner => [@inner], } } my $param = {outer => [@outer]}; print Dumper $param; exit; # the following not directly relevant my $t = HTML::Template->new(filename => 'kwaping.html') or die "can't parse template: $!"; $t->param($param); print $t->output; sub get_hash{ return ( 'One' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'Two' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, ] ); }
Data::Dumper output:
---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl $VAR1 = { 'outer' => [ { 'inner' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'rep' => 'Two' }, { 'inner' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'rep' => 'One' } ] }; > Terminated with exit code 0.

Replies are listed 'Best First'.
Re^2: Excel::Template and a hash of arrays of hashes
by kwaping (Priest) on Dec 29, 2006 at 17:18 UTC
    Thank you wfsp, that did the job perfectly! I owe you a beverage of your choice sometime.

    The code:
    my @outer; foreach my $rep (sort keys %hash) { my @inner; foreach my $detail (@{ $hash{$rep} }) { push @inner, $detail; } push @outer, { rep => $rep, inner => \@inner, } } $template->param( { outer => \@outer } );
    The XML:
    <workbook> <loop name="outer"> <worksheet name="$rep"> <row> <cell text="Comp_Recid" /> <cell text="ApplicationType" /> <cell text="State" /> <cell text="Count (*)" /> </row> <loop name="inner"> <row> <cell text="$companyid" /> <cell text="$apptype" /> <cell text="$state" /> <cell text="$count" /> </row> </loop> </worksheet> </loop> </workbook>

    ---
    It's all fine and dandy until someone has to look at the code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://592252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found