#!/usr/bin/perl

$comma=",";
open (SOURCE, "NEWCAT01.") || die "Cannot open: $!";
open (DESTINATION1, ">>NEWCAT01-ECON.csv") || die "Cannot open: $!";
open (DESTINATION2, ">>NEWCAT01-DEMO.csv") || die "Cannot open: $!";
sub prep {
   $string=$_[0];        #Best practice: my ($string, $dest, $term)=@_;
   $dest=$_[1];          #for unpacking the argument array.
   $term=$_[2];
   chomp $string;    
   $string=~ s/^\s+//;   #Remove leading white space
   $string=~ s/\s+$//;   #Remove trailing white space
   if ($string eq ""){   #If the string is empty insert a ? into the field
     $string="?";
   }
   print $dest $string;  #Output the string
   if ($term==0){        #If the value does not terminate the line output a comma.
   print $dest $comma;
   }
   else{                 #If the value does terminate the line output an end of line.
   print $dest "\n";
   }
}
$count=1;
while(){
     if ($count%6==1){              #If the loop is the first in the series of
     $dest="DESTINATION1";          #six, then write the data to the file storing
     read SOURCE,$series,2;         #economic information.
    # &prep($series,$dest,0);
     read SOURCE,$id,4;             #Write necessary fields to file using the prep subroutine
    # &prep($id,$dest,0);
     read SOURCE,$location,5;
     &prep($location,$dest,0);
     read SOURCE,$othername,10;
    # &prep($othername,$dest,0);
     read SOURCE,$fathername,10;
    # &prep($fathername,$dest,0);
     read SOURCE,$familyname,10;
    # &prep($familyname,$dest,0);
     read SOURCE,$source,3;
    # &prep($source,$dest,0);
     read SOURCE,$page,3;
    # &prep($page,$dest,0);
     read SOURCE,$householdtype,1;
     &prep($householdtype,$dest,0);
     read SOURCE,$dwellingtype,1;
     &prep($dwellingtype,$dest,0);
     read SOURCE,$ownsanimals,1;
     &prep($ownsanimals,$dest,0);
     read SOURCE,$emim,1;
     &prep($emim,$dest,0);
     read SOURCE,$trade,1;
     &prep($trade,$dest,0);
     read SOURCE,$occupation,2;
     &prep($occupation,$dest,1);
     read SOURCE,$privateinvest,5;
    # &prep($privateinvest,$dest,0);
     read SOURCE,$publicinvest,5;
    # &prep($publicinvest,$dest,0);
     read SOURCE,$totalassets,6;
    # &prep($totalassets,$dest,0);
     read SOURCE,$deductions,5;
    # &prep($deductions,$dest,0);
     read SOURCE,$tax,5;
    # &prep($tax,$dest,1);
     }
     else{                                #If the loop is not the first of a series of 6
           $dest="DESTINATION2";          #then write data to the file storing demographic
           read SOURCE,$series,2;         #data.
           &prep($series,$dest,0);
           read SOURCE,$id,4;
           &prep($id,$dest,0);
           read SOURCE,$card,1;
           &prep($card,$dest,0);
           read SOURCE,$totalcards,1;
           &prep($totalcards,$dest,1);

     }
$count=$count+1;
}
close (DESTINATION2);
close (DESTINATION1);
close (SOURCE);