Interesting problem. I really wish you would have supplied your template as well as your flat text file, as you didn't, i'll have to make up my own. ;) I chose to use Tie::File, even though my first instincts were to use File::ReadBackwards. I chose Tie::File because i'd rather work with an array slice, that's all. There are two lines that print two important datastructures to STDERR -- understanding what you datastructure should look like is the key in getting the hang of loop with HTML::Template. You don't have to use map as i have done, i just wanted to show you that Perl can be quite consise. (Make sure that the entire DATA section is included in the script if you copy this.)
use strict; use warnings; use Tie::File; use Data::Dumper; use HTML::Template; # part 1: get the last ten lines and store in a 2-D array my @record; tie @record, 'Tie::File', 'record.txt' or die $!; my @last10 = map [split "\t"], @record[map -$_,1..10]; #print STDERR Dumper \@last10; # part 2: create a list of hashes (LoH) for HTML::Template my %hash; my @headers = qw(date amount action total); my @loop = map { @hash{@headers} = @$_[1..@$_]; # exclude timestamp $_ = {%hash} } sort {$a->[0] <=> $b->[0]} @last10; # sort by timestamp #print STDERR Dumper \@loop; # part 3: shove data through template my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(member => \@loop); print $tmpl->output; __DATA__ <table> <tr> <th>Date</th> <th>Amount</th> <th>Action</th> <th>Total</th> </tr> <tmpl_loop member> <tr> <td><tmpl_var date></td> <td><tmpl_var amount></td> <td><tmpl_var action></td> <td><tmpl_var total></td> </tr> </tmpl_loop> </table>
record.txt is a tab delimited file similar this one that i used for testing:
1054703890	a	b	c	d
1054703892	e	f	g	h
1054703894	1	2	3	4
1054703896	5	6	7	8
1054703891	A	B	C	D
1054703893	E	F	G	H
1054703895	my	how	time	flies

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)

In reply to (jeffa) Re: Making a hash of making a hash of an array by jeffa
in thread Making a hash of making a hash of an array by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.