Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Accessing Array of Hashes in TT

by swandown (Novice)
on Jun 18, 2013 at 17:47 UTC ( #1039613=perlquestion: print w/ replies, xml ) Need Help??
swandown has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm building up a data structure using :

while (@fetch = $sth1->fetchrow) { $vendor = $fetch[0]; $terminal_id = $fetch[1]; $terminal_name = $fetch[3]; $refunds = $fetch[4]; $sales = $fetch[5]; $count = $fetch[6]; $vendor_sales{$vendor}{terminals}{$terminal_id}{terminal_name} + = $terminal_name; $vendor_sales{$vendor}{terminals}{$terminal_id}{sales} = $sale +s; $vendor_sales{$vendor}{terminals}{$terminal_id}{refunds} = $re +funds; $vendor_sales{$vendor}{terminals}{$terminal_id}{count} = $coun +t; }

It gives me the following structure :

'Book Shop' => { '10' => { 'count' => 222, 'refunds' => '90.00', 'sales' => '1200', 'terminal_name' => 'Front Till' }, '20' => { 'count' => 149, 'refunds' => '124.00', 'sales' => '7000.00', 'terminal_name' => 'Side Till' } }, 'Sweet Shop' => { '40' => { 'count' => 2, 'refunds' => '0.00', 'sales' => '400.00', 'terminal_name' => 'Cash Till +' }, '60' => { 'count' => 241, 'refunds' => '20.00', 'sales' => '600.00', 'terminal_name' => 'Credit Til +l' } }

How can I access the different elements in TT :

[% FOREACH vendor IN vendor_sales %] [% vendor.key %] : [% vendor.terminals.terminal_name %] [% END %]

Many Thanks

Comment on Accessing Array of Hashes in TT
Select or Download Code
Re: Accessing Array of Hashes in TT
by choroba (Abbot) on Jun 18, 2013 at 17:57 UTC
    Where is terminals in the data structure?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Accessing Array of Hashes in TT
by jeffa (Chancellor) on Jun 18, 2013 at 17:58 UTC

    Check out Hash Virtual Methods from the TT docs. Something like the following should work:

    [% FOREACH shop IN vendor_sales.keys %] <h1>[% shop %]</h1> [% FOREACH number IN vendor_sales.$shop.keys %] <h2>[% number %]</h2> <ul> [% FOREACH attr IN vendor_sales.$shop.$number.keys %] <li>[% attr %]: [% vendor_sales.$shop.$number.$attr %]</li> [% END %] </ul> [% END %] [% END %]


    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Accessing Array of Hashes in TT
by swandown (Novice) on Jun 18, 2013 at 18:27 UTC

    choroba - You hit the nail on the head - I had an extra tier of nonsense in the data structure.

    jeffa - sweet - that's exactly what I was looking for

    Many Thanks All

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039613]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-08-29 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls