###########################
#  s.pl
#     07.10.17
#     07.12.05

use strict;
use HTML::Template;

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

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

$t{htmfile} = $t{root} . '1.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 @{ $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);
}

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

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

__END__;