In order to populate a table in HTML::Template I'm having to go a bit beyond my current knowledge base and have run into some problems.
I have set up and confirmed the template using the example having entered the values into the script. I cannot, however, extract the values from a file to populate the hash. In the code that follows I am opening a flat file and reversing the line order of the file to extract the last 10 records
I'd be grateful If someone could help me to get this right.open (FILE, "$record") or die "can't open $record: $!"; my @record = reverse <FILE>; close FILE; foreach my $line (@record) { ($timestamp, $current_time, $funds, $action, $current_funds) = split " +\t",$line; while ($count < 10){ $member{$timestamp} = [$current_time, $funds, $action, $current_fu +nds]; $count += 1; } } %member = ( $timestamp => [$current_time, $funds, $action, $current_fu +nds]); my $template = HTML::Template->new(filename => 'temp_late.html'); my @loop; # the loop data will be put in here # fill in the loop, sorted by timestamp foreach my $timestamp (sort keys %member) { # get the other data from the data hash my ($current_time, $funds, $action, $current_funds) = @{$member{$t +imestamp}}; # make a new row for this data - the keys are <TMPL_VAR> names # and the values are the values to fill in the template. my %row = ( date => $current_time, amount => $funds, action => $action, total => $current_funds, ); # put this row into the loop by reference push(@loop, \%row); } # call param to fill in the loop with the loop data by reference +. $template->param(member => \@loop); # send the obligatory Content-Type print "Content-Type: text/html\n\n"; # print the template print $template->output;
In reply to Making a hash of making a hash of an array by jonnyfolk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |