Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Using HTML Template to fill a 2-dimensional table

by jms53 (Monk)
on Feb 19, 2013 at 14:51 UTC ( #1019582=perlquestion: print w/ replies, xml ) Need Help??
jms53 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm having some trouble with HTML::Template. After following the tutorial here, I have the following code:

HTML:
<TABLE BORDER="1"> <TR><TD>Transaction</TD><TD>DATE</TD><TD>Type</TD><TD>Amount</TD> +<TD>Ending Balance</TD></TR> <TMPL_LOOP NAME="TRANSACTIONS"> <TR><TD>Transaction:</TD> <TD><TMPL_VAR NAME="Date"></TD> <TD><TMPL_VAR NAME="Type"></TD> <TD><TMPL_VAR NAME="Amount"></TD> <TD><TMPL_VAR NAME="Ending_Balance"></TD></TR> </TMPL_LOOP> </TABLE>

and my Perl code:
my $template = HTML::Template->new( filename => 'atm_choose.tmpl', die +_on_bad_params => 0 ); my @LINES; while (<DATA>) { chomp; push @LINES, $_; } my $split; for (@LINES) { my @row = split ('&', $_); push @{$split}, $_; } $template->param(TRANSACTIONS => $split); print header, $template->output; __DATA__ 10/02/2013 & closing balance & 0 & 1000 11/02/2013 & debit & 100 & 900 12/02/2013 & credit & 1000 & 1900

I'm currently getting the table correctly formatted, but it is entirely empty. I *THINK* I'm giving HTML::Template the correct variable.
thank you,

edit

I was able to get the table as expected, but this doesn't seem like good practice (even useful as it doesn't allow for more information to be used...). However, I am unable to get this to work using a foreach / for loop. any pointers please?

while (<DATA>) { chomp; push @LINES, $_; } my @split; for (@LINES) { my @row = split ('&', $_); push @split, \@row; } $template->param(TRANSACTIONS => [{Date => $split[0][0], Type => $spli +t[0][1], Amount => $split[0][2], Ending_Balance => $split[0][3]}, {Date => $split[1][0], Type => $split[1][1], Amount +=> $split[1][2], Ending_Balance => $split[1][3]}, {Date => $split[2][0], Type => $split[2][1], Amount +=> $split[2][2], Ending_Balance => $split[2][3]} ]);

Thank you! </p<

J -

Comment on Using HTML Template to fill a 2-dimensional table
Select or Download Code
Re: Using HTML Template to fill a 2-dimensional table
by Anonymous Monk on Feb 19, 2013 at 15:02 UTC

    Have you read perlintro?

    From that tutorial you're reading

    $template->param( STUDENT => [ { NAME => 'Bluto Blutarsky', GPA => '0.0' }, { NAME => 'Tracey Flick' , GPA => '4.0' }, ] );

    Do you know what those square brackets and curly braces do?

    Say it with me:

    my $hashref = { 'hasherefs', 'are', 'curly', 'ones' };

    my $arrayref = [ 'arrayrefs', 'are', 'square' ];

    my %hash = ( 'lists are round' , 'but hashes want pairs');

Re: Using HTML Template to fill a 2-dimensional table
by Anonymous Monk on Feb 19, 2013 at 16:01 UTC

    ...edit...

    :)

    my @names = qw[ Date Type Amount Ending_balance ] ; my @transactions; ... while ... my @row = split '&', $_; my %hashrow; @hashrow{ @names } = @row; push @transactions, \%hashrow ... $template->param(TRANSACTIONS => \@transactions );

      my %hashrow; @hashrow{ qw[ Date Type Amount Ending_balance ] } = split '&', $_;

      my %hashrow; @hashrow{Date} = $row[0]; @hashrow{Type} = $row[1]; @hashrow{Amount} = $row[2]; @hashrow{Ending_balance} = $row[3];

      my %hashrow = ( # ((( round Date => $row[0], Type => $row[1], Amount => $row[2], Ending_balance => $row[3], );

      my %hashrow = ( Date => $row[0], Type => $row[1], Amount => $row[2], Ending_balance => $row[3], ); # ))) round

      my $hashrow = { # {{{ curly Date => $row[0], Type => $row[1], Amount => $row[2], Ending_balance => $row[3], }; # }}} curly

      push @transactions, { # {{{ curly Date => $row[0], Type => $row[1], Amount => $row[2], Ending_balance => $row[3], }; # }}} curly
Re: Using HTML Template to fill a 2-dimensional table
by tangent (Deacon) on Feb 19, 2013 at 16:09 UTC
    It might be clearer if you do it this way:
    my $split; for (@LINES) { my ($date,$type,$amt,$bal) = split ('&', $_); my $hash_ref = { Date => $date, Type => $type, Amount => $amt, Ending_Balance => $bal, }; push(@$split,$hash_ref); } $template->param(TRANSACTIONS => $split);
Re: Using HTML Template to fill a 2-dimensional table
by 7stud (Deacon) on Feb 19, 2013 at 17:37 UTC
    This:

    my @LINES; while (<DATA>) { chomp; push @LINES, $_; }

    ...is equivalent to this:

    chomp (my @lines = <DATA>);

    Who knows how that works, but it does. It's a perl idiom you need to know.

    As for this:
    my $split; for (@LINES) { my @row = split ('&', $_);

    The only variable name you could think of, $split, is the same name as the perl function split() ? Don't ever do that. If you can't think up 10 unique variable names, you cannot be a computer programmer. Also, your variable names need to be descriptive. "Hey, Joe! I've got an array ref named '$split'. Guess what's in the array? Joe: Gold coins?"

    Avoid writing $_ in your code. I suggest you read "Learning Perl 6th ed.". You need to learn modern perl.

      The only variable name you could think of, $split, is the same name as the perl function split() ? Don't ever do that. If you can't think up 10 unique variable names, you cannot be a computer programmer... Avoid writing $_ in your code. I suggest you read "Learning Perl 6th ed.". You need to learn modern perl.

      ETOOMUCHCAFFEINE

      chomp (my @lines = <DATA>);

      Who knows how that works, but it does. It's a perl idiom you need to know.

      Yes, it’s a useful idiom — but surely not so mysterious?

      1. my @lines =
        Declares a lexical array variable and initialises it (with a list of array elements).

      2. @lines = <DATA>
        The LHS (left-hand side) of the assignment is an array, which gives list context to the expression on the RHS. When the readline or “diamond” operator <...> is placed in list context, it “reads until end-of-file is reached and returns a list of lines.” Lines are defined as the text between successive input record separators. The input record separator, stored in $/, “is newline by default.” See readline and General Variables in perlvar.

      3. chomp(...);
        When chomp is given a list, it chomps (removes the input record separator from) each element in the list.

      That wasn’t really so hard, was it? ;-)

      Avoid writing $_ in your code.

      Yes, avoid writing $_ explicitly where possible. But be aware that an explicit $_ is often needed; for example:

      • my @doubles = map { $_ * 2 } @numbers;

      • $couples{$_} eq 'Betty' && print for keys %couples;  # Find Betty's husband

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019582]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-07-22 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (127 votes), past polls