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 = "
  <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 = HTML::Template->new(scalarref => \$txt);

my @data = (
            {cId => 'c01_id', cTitle => 'c01_title',
             scId => 'sc01_id', scTitle => 'sc01_title',
            },
            {cId => 'c01_id', cTitle => 'c01_title',
             scId => 'sc02_id', scTitle => 'sc02_title',
            },
            {cId => 'c02_id', cTitle => 'c02_title', 
             scId => '', scTitle => '',
            },
            {cId => 'c03_id', cTitle => 'c03_title',
             scId => 'sc03_id', scTitle => 'sc03_title',
            },
           );

my @cLoop=(); my @scLoop=(); my $prevCid='';
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}=$rec->{cId}; $cRow{cTitle}=$rec->{cTitle};
  }

  my %scRow; $scRow{scId}=$rec->{scId}; $scRow{scTitle}=$rec->{scTitle};
  push(@scLoop, \%scRow);

  $cRow{scLoop} = [EMAIL PROTECTED] if ($rec->{cId} ne $prevCid);
  push(@cLoop, \%cRow) if ($rec->{cId} ne $prevCid);
  $prevCid = $rec->{cId};
}

$tmpl->param(cLoop => [EMAIL PROTECTED]);
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

==================================================
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



-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_idG21&alloc_id040&op=click
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to