###########################
#  multi.pl

use strict;
use HTML::Template;

my(%t,@fld,$n,$template,@loop);
print "Please input filename=";
chop($t{root}=<STDIN>);
$t{tmpl} = 'index.html';
$t{inputf} = $t{root} . '.txt';

open(IN,"names.txt") or die "Can't open the file names.txt.\n";
while(<IN>){
    if ( /^NAME\s/ ) {
        @fld = split;
        $t{list}{$fld[1]} = $fld[2];
    }
}
close(IN);

$template = HTML::Template->new(filename => $t{tmpl});
@loop = ();

$t{htmfile} = $t{root} . '.htm';

$t{flag} = 1;
open(IN,"$t{inputf}") or die "Can't open the file $t{inputf}";
while(<IN>){
    next if $. == 1;
    next if length($_) < 2;
    if ( $t{flag} == 1 ) {
        $t{flag} = 2;
        push(@{ $t{N1s} },$_);
        $t{N11} = $_;
    } elsif ($t{flag} == 2) {
        $t{clist}{$t{N11}} = $_;
        $t{flag} = 3;
    } elsif ($t{flag} == 3) {
        $t{elist}{$t{N11}} = $_;
        $t{flag} = 1;
    } 
}
close(IN);

@{ $t{NN} } = sort {lc($a) cmp lc($b)} @{ $t{N1s} };
#@{ $t{NN} } = @{ $t{N1s} };

for $n ( 0 .. $#{ $t{NN} } ) {
    $t{N1} = $t{NN}[$n];
    $t{c1} = $t{clist}{$t{N1}};
    $t{e1} = $t{elist}{$t{N1}};
    my %row = (
            N1 => $t{N1},
            C1 => $t{c1},
            E1 => $t{e1}
    );
    push(@loop, \%row);
}

$t{etitle} = uc($t{root});
$template->param(std_loop => \@loop);
$template->param(ename => $t{etitle});
$template->param(cname => $t{list}{$t{etitle}});
open(OUT,">$t{htmfile}");
print OUT $template->output;
close(OUT);

print "The output file is $t{htmfile}\n";

__END__;