Home: Perl Programming Help: Intermediate:
reports and grouping


Jul 23, 2013, 8:27 AM

Views: 3132
reports and grouping

Hi All,

I'm trying to create a basic report using template toolkit, that has both column and row groups. I have the data, but can't seem to wrap my mind around how to format the output. So for example

my %hash; 
$hash{'2013-01-01'}->{'apples'}->{'qty'} = 3;
$hash{'2013-01-01'}->{'pears'}->{'qty'} = 2;
$hash{'2013-01-01'}->{'peaches'}->{'qty'} = 1;
$hash{'2013-01-03'}->{'peaches'}->{'qty'} = 1;
$hash{'2013-01-03'}->{'apples'}->{'qty'} = 5;

Now in that case, I want the date to be the column header and fruit to be the row. I can think of how to group by either column or row, but not both. Example output.

         2013-01-01 | 2013-01-03 
apples | 3 | 5
peaches | 1 | 1

I'm open to other suggestions besides toolkit, but I am limited to Perl 5.8.8


I created 2 hashes to pass to template toolkit, one where the main key is date and the other where it's the fruit, using my example code. I would still appreciate feedback as I'm sure my current approach isn't the best.


(This post was edited by lightspd on Jul 23, 2013, 10:33 AM)

Veteran / Moderator

Jul 23, 2013, 11:51 AM

Views: 3123
Re: [lightspd] reports and grouping

you could "transpose" your hash (just as one can transpose a matrix). Before I go further, from what you've shown you dont really need an HoHoH, a HoH would be sufficient (the 'qty' level is useless, you could put the number of peaches in hash{$date}{'peaches'}. For the transposed hash, I will use a HoH. The following is just a "proof of concept" quick session under the Perl debugger.

  DB<1> $hash{'2013-01-01'}->{'apples'}->{'qty'} = 3; 

DB<2> $hash{'2013-01-01'}->{'pears'}->{'qty'} = 2;

DB<3> $hash{'2013-01-01'}->{'peaches'}->{'qty'} = 1;

DB<4> $hash{'2013-01-03'}->{'peaches'}->{'qty'} = 1;

DB<5> $hash{'2013-01-03'}->{'apples'}->{'qty'} = 5;

DB<6> x %hash
0 '2013-01-03'
1 HASH(0x8035a050)
'apples' => HASH(0x80359fd8)
'qty' => 5
'peaches' => HASH(0x803564e8)
'qty' => 1
2 '2013-01-01'
3 HASH(0x803606f8)
'apples' => HASH(0x8035a170)
'qty' => 3
'peaches' => HASH(0x803606e0)
'qty' => 1
'pears' => HASH(0x8035a320)
'qty' => 2
DB<7> for $key (keys %hash) {for $fruit (keys %{$hash{$key}}) {$hash2{$fruit}{$key} = $hash{$key}{$fruit}{'qty'}}}

DB<8> x %hash2
0 'peaches'
1 HASH(0x80405810)
'2013-01-01' => 1
'2013-01-03' => 1
2 'apples'
3 HASH(0x803555d0)
'2013-01-01' => 3
'2013-01-03' => 5
4 'pears'
5 HASH(0x803f6be0)
'2013-01-01' => 2

The only important part occurs on line DB<7>. I suppose that you get the point, it should now be very easy to scan your %hash2 hash in order to print your data the way you want.

Veteran / Moderator

Jul 25, 2013, 3:10 PM

Views: 3102
Re: [lightspd] reports and grouping

Hi Lightspd,

I am surprised not to get any feedback from you after two days.

I think I gave you the solution, a pretty nice one, IMHO.

But maybe you don't understand it and just discarded it, then you should ask for explanations, I could explain further.

Or is it that you've got your answer and don't care anymore?

Is thanking people for help offered outdated? Especially if it is offered for free, clearly, it isn't worth much, isn't it?


Jul 25, 2013, 3:29 PM

Views: 3100
Re: [Laurent_R] reports and grouping

Hi Laurent_R,

My sincerest apologies. I honestly meant to reply to you yesterday and got completely caught up in another issue.

I found your solution very helpful in solving my problem.

I appreciate your help and send my thanks.

(This post was edited by lightspd on Jul 25, 2013, 3:32 PM)

Chris Charley

Jul 27, 2013, 4:43 PM

Views: 3080
Re: [lightspd] reports and grouping

Attached is file t9.pl which makes your matrix.

The output is:

        2013-01-01 2013-01-03 
apples 3 5
peaches 1 1
pears 2 0

Attachments: t9.pl (0.58 KB)