#!/usr/local/bin/perl

sub byName { ((substr $a, 8, 12) cmp (substr $b, 8, 12)); }

# initialize loop counter
$i = 0;

# read input file into array of rows
open(F, "out");
while(<F>) { $filerecs[$i++]=$_; }
close(F);

# sort the array of rows using name sort into sorted_recs array
@sorted_recs = sort by_lob @filerecs;

#foreach $line (@sorted_recs) { print $line ; }

exit(0);

sub by_dctr
{
 %dctrlist=('CON','CH','ACH','CH','DOSC','CH','ELM','CH','ORG','CH','PCS','CH','SAC','CH','BUT','CH','OCSP','CH','BOI','MN','LUB','MN','PHX','MN','RNO','MN','SHN','MN','SHP','MN','SIO','MN','UTA','MN','WLK','MN');
ROW:
	foreach $row (@filerecs)
	{
		($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
		print STDERR "\$ctr: $ctr \$dc: $dc \$lob: $lob\n";
    # SAC|CH|NBBC|60|1.55|||1|25|3.97|2||1|208|1154|386|1540|94|528|
    # ctr dc lob  vals
		if(($dc	eq 'CH')&&($ctr ne 'BUT')){ push(@table, $row); next;}
		if(($dc	eq 'CH')&&($ctr eq 'BUT')){ push(@table, $row); next; }
		if( $dc	eq 'MN')                  { push(@table, $row); next; }
		if( $ctr eq 'TOTAL')              { push(@table, $row); next; }
	}
 return @table;
}

sub by_lob
{
 %loblist= ('SAC','NBBC','SHN','NBBC','CON','OCS','UTA','OCS','OCSP','OCS','DOSC','Other','PCS','Other','ACH','Other','ELM','PB','ORG','PB','BUT','PB','BOI','PB','LUB','PB','PHX','PB','RNO','PB','SHP','PB','STPS','PB','SIO','PB','WLK','PB');

  foreach $key (%loblist)
  {
    foreach $row (@filerecs)
    {
      ($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
      if($lob eq $loblist{$key})
      { 
        if($loblist{$key} eq 'NBBC')  { push(@table, $row); next; }
        if($loblist{$key} eq 'OCS')   { push(@table, $row); next; }
        if($loblist{$key} eq 'PB')    { push(@table, $row); next; }
        if($loblist{$key} eq 'Other') { push(@table, $row); next; }
        if($loblist{$key} eq 'TOTAL') { push(@table, $row); last; }
      } 
      print "$key: @table";
    }
  }
 print @table; 
}

#    #if($lob eq 'NBBC') { push(@table, $row); next; }
#  }
# print "1 \@table: @table";
#  foreach $row (@filerecs)
#  {
#    ($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
#    if($lob eq 'OCS') { push(@table, $row); next; }
#  }
# print "2 \@table: @table";
#  foreach $row (@filerecs)
#  {
#    ($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
#    if($lob eq 'PB') { push(@table, $row); next; }
#  }
# print "3 \@table: @table";
#  foreach $row (@filerecs)
#  {
#    ($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
#    if($lob eq 'Other') { push(@table, $row); next; }
#  }
# print "4 \@table: @table";
#  foreach $row (@filerecs)
#  {
#    ($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
#    if($ctr eq 'TOTAL') { push(@table, $row); last; }
#  }
# print @table; 
# }

