[htmltmpl] problem in populating nested loops
Brought to you by:
samtregar
From: Kapoor, N. <nis...@xc...> - 2004-07-23 15:09:25
|
Hello List, I have this small piece of code here that I am trying to use to create a = nested loop, and I am totally stuck! Would greatly appreciate some help. Thanks, Nishi --------------- start - code -------------------- #!/usr/bin/perl -w # Requires use strict; use HTML::Template; my $txt =3D " <tmpl_loop cLoop> Cat: <tmpl_var cId>, <tmpl_var cTitle> <tmpl_loop scLoop> SubCat: <tmpl_var scId>, <tmpl_var scTitle> </tmpl_loop scLoop> </tmpl_loop cLoop> "; my $tmpl =3D HTML::Template->new(scalarref =3D> \$txt); my @data =3D ( {cId =3D> 'c01_id', cTitle =3D> 'c01_title', scId =3D> 'sc01_id', scTitle =3D> 'sc01_title', }, {cId =3D> 'c01_id', cTitle =3D> 'c01_title', scId =3D> 'sc02_id', scTitle =3D> 'sc02_title', }, {cId =3D> 'c02_id', cTitle =3D> 'c02_title',=20 scId =3D> '', scTitle =3D> '', }, {cId =3D> 'c03_id', cTitle =3D> 'c03_title', scId =3D> 'sc03_id', scTitle =3D> 'sc03_title', }, ); my @cLoop=3D(); my @scLoop=3D(); my $prevCid=3D''; foreach my $rec (sort {$a->{cId} cmp $b->{cId}} @data) { next if (!$rec->{scId}); #skip recs that don't have scId my %cRow; if ($rec->{cId} ne $prevCid) { $cRow{cId}=3D$rec->{cId}; $cRow{cTitle}=3D$rec->{cTitle}; } my %scRow; $scRow{scId}=3D$rec->{scId}; = $scRow{scTitle}=3D$rec->{scTitle}; push(@scLoop, \%scRow); $cRow{scLoop} =3D \@scLoop if ($rec->{cId} ne $prevCid); push(@cLoop, \%cRow) if ($rec->{cId} ne $prevCid); $prevCid =3D $rec->{cId}; } $tmpl->param(cLoop =3D> \@cLoop); print $tmpl->output; exit; --------------- end - code -------------------- The output I am getting is: Cat: c01_id, c01_title SubCat: sc01_id, sc01_title SubCat: sc02_id, sc02_title SubCat: sc03_id, sc03_title Cat: c03_id, c03_title SubCat: sc01_id, sc01_title SubCat: sc02_id, sc02_title SubCat: sc03_id, sc03_title =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= But, this is what I am hoping to get: Cat: c01_id, c01_title SubCat: sc01_id, sc01_title SubCat: sc02_id, sc02_title Cat: c03_id, c03_title SubCat: sc03_id, sc03_title |