CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
reports and grouping

 



lightspd
Novice

Jul 23, 2013, 8:27 AM

Post #1 of 5 (1247 views)
reports and grouping Can't Post

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

Code
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;
etc.


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.

Code
         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

EDIT:

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.

Cheers.


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


Laurent_R
Veteran / Moderator

Jul 23, 2013, 11:51 AM

Post #2 of 5 (1238 views)
Re: [lightspd] reports and grouping [In reply to] Can't Post

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.


Code
  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
DB<9>


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.


Laurent_R
Veteran / Moderator

Jul 25, 2013, 3:10 PM

Post #3 of 5 (1217 views)
Re: [lightspd] reports and grouping [In reply to] Can't Post

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?


lightspd
Novice

Jul 25, 2013, 3:29 PM

Post #4 of 5 (1215 views)
Re: [Laurent_R] reports and grouping [In reply to] Can't Post

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
User

Jul 27, 2013, 4:43 PM

Post #5 of 5 (1195 views)
Re: [lightspd] reports and grouping [In reply to] Can't Post

Attached is file t9.pl which makes your matrix.

The output is:


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

Attachments: t9.pl (0.58 KB)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives