my @words = qw(I Am Cool);
my @numbers = qw(1 2 3);
my @loop_data = (); # initialize an array to hold your loop
while (@words and @numbers) {
my %row_data; # get a fresh hash for the row data
# fill in this row
$row_data{WORD} = shift @words;
$row_data{NUMBER} = shift @numbers;
# the crucial step - push a reference to this row into the loop!
push(@loop_data, \%row_data);
}
# finally, assign the loop data to the loop param, again with a ref
$template->param(THIS_LOOP => \@loop_data);
####
sub getLoopData {
my $table = shift;
my $order = shift;
my @fields = @_;
my $sql = "SELECT " . join (', ', @fields) . " from $table ORDER BY $order";
my $sth = $dbh->prepare_cached($sql);
$sth->execute();
my @rows;
while (my $row = $sth->fetchrow_hashref) {
push (@rows, $row);
}
return \@rows;
}
####
my $template->param(foo_loop => &getLoopData('foo', 'bar', 'baz', 'moo'));
####
my @years = ();
my @years_loop = ();
my $curr_year = (localtime)[5];
$curr_year += 1900;
for ($curr_year - 100 .. $curr_year - 5) {
push (@years, $_);
}
while (@years) {
my %year_data;
$year_data{year} = shift @years;
push (@years_loop, \%year_data);
}
$template->param(years_loop => \@years_loop);