Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Getting data ready for HTML::Template

by Unforgiven (Hermit)
on Nov 18, 2009 at 21:24 UTC ( #808029=note: print w/replies, xml ) Need Help??


in reply to Getting data ready for HTML::Template

The first thing I noticed is that in one loop in the template, you've got three tokens all with the same name, so they'd get the same data. In your for loop in Perl, and in the template loop, give each of them different names (key1, key2, key3, or hopefully something more meaningful). You're also pushing to the array (in other words, making a row to display) once per hash key - you actually want one row per machine. This is entirely untested code, but give this a try. Set the template tokens to match your data's keys (key1,key2,key3), and try this loop.
for $user ( keys %HoH ) { for $machine ( sort keys %{ $HoH{$user} } ) { %row_data = (); $row_data{MACHINE} = $machine; for my $key ( sort keys %{ $HoH{$user}->{$machine} } ) { $row_data{$key} = $HoH{$user}->{$machine}->{$key}; } push @{$loop_data}, \%row_data; } }

Replies are listed 'Best First'.
Re^2: Getting data ready for HTML::Template
by gctaylor1 (Hermit) on Nov 18, 2009 at 22:01 UTC
    Thanks unforgiven. Your untested(but valuable) code gave me a piece that I was wondering about(incrementing the hash key) and allowed me to see that at least I'm doing the basics correct because it got me a bit further. I made the suggested changes plus changed my template slightly like this:
    <table border="1"> <tr> <TMPL_LOOP NAME=THIS_LOOP> <td> <TMPL_VAR NAME="MACHINE"> </td> <td> <TMPL_VAR NAME="key1"> </td> <td> <TMPL_VAR NAME="key2"> </td> <td> <TMPL_VAR NAME="key3"> </td> </tr> </TMPL_LOOP> </table>

    New output:

    Machine Key One Key Two Key Three
    machine3 value7 value8 value9
    machine3 value7 value8 value9
    machine3 value7 value8 value9

    Now it seems like I just need to figure out the correct logic in my loop that creates the data.

      The remaining problem is that you push \%row_data into your array, which is the same pointer to the same data structure (which gets written to three times until only the last row data remains) instead of a new one. The easiest way to generate a new hash every time through the loop is to change the third line of Unforgivens loop
      %row_data = ();
      to
      my %row_data = ();
        Awesome! Thanks for these solutions. It's now working to the point where I can figure out the rest because it's just formatting.

        Machine Key One Key Two Key Three
        machine1 value1 value2 value3
        machine2 value4 value5 value6
        machine3 value7 value8 value9

        Here's the final segment of code:

        Thank you for the help!

        Gah, I knew I was forgetting to mention something else. Thanks for catching that.

        It's an example of keeping your variables close to where they're used. For most cases, it's better to declare a variable, use it, then stop using it. If nothing else, when reading your code, it makes it easier to keep all those variables you're actively using in your head, since as soon as you're done with it, you can forget about it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2020-10-26 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (251 votes). Check out past polls.

    Notices?