#!/usr/local/perl -w #printhh.perl - perl script for printing household data # open(INPUT,$ARGV[0]); #file specified on command line open(RUNINFO,">run_info"); #file for writing run information open(ERROR,">error_records"); #file for writing $status==0 records # $lpid = -1; #initialize "last" $pid value for reading 1st record $lhhn = 0; #initialize "last" $hhn value for reading 1st record $records = 0; #initialize counter for records read $persons1 = 0; #initialize counter for persons with $status == 1 $persons0 = 0; #initialize counter for persons with $status == 0 $personsh = 0; #initialize counter for persons in households printed $households = 0; #initialize counter for households printed @hh = (); #initialize array for persons in household # while () { # compute status code to identify invalid records ($status == 0) # because $pid doesn't always count up to $hhn as it should $pid = substr($_,16,2); #person's id number $hhn = substr($_,50,2); #number of persons in household if ($pid == 1) { $status = 1; } elsif ($pid > 1 && $pid == $lpid + 1 && $pid <= $hhn && $hhn == $lhhn ) { $status = 1; } else { $status = 0; } $lpid = $pid; $lhhn = $hhn; # print $status," ",$pid," ",$hhn,"\n"; #uncomment for checking # # accumulate person records for each household ($status == 0) # in array @hh, then print information from this array $records++; if ($status == 1) { $persons1++; $hh[$pid-1] = $_; if ($pid == $hhn) { $personsh = $personsh + scalar(@hh); $households++; foreach $person (@hh) { &print_person($person); } print "\n"; @hh = (); } } else { $persons0++; print ERROR substr($_,1,15), " ",substr($_,16); } # no check here for $pid not reaching $hhn; household will not # be printed in this case } print RUNINFO 'Records read: ',$records,"\n"; print RUNINFO 'Persons with $status == 1: ',$persons1,"\n"; print RUNINFO 'Persons with $status == 0: ',$persons0,"\n"; print RUNINFO 'Households written: ',$households,"\n"; print RUNINFO 'Persons in households written: ',$personsh,"\n"; $discrepancy = $persons1 - $personsh; print RUNINFO 'Discrepancy: ',$discrepancy,"\n"; # sub print_person { $rhh = substr($person,18,1); $rhh =~ tr/12345678/HSCDPGON/; $sex = substr($person,19,1); $sex =~ tr/12/MF/; $yrb = substr($person,20,3); #year of birth $mnb = substr($person,23,2); #month of birth $yrb = 1000 + $yrb; $adj = $mnb eq "07" || $mnb eq "08" || $mnb eq "09" ; $adj = $adj || $mnb eq "10" || $mnb eq "11" || $mnb eq "12"; $age = (1990 - $yrb) - $adj; if (length($age) eq 1) {$age = "0".$age} $mar = substr($person,42,1); $mar =~ tr/01234/-SMWD/; $kids = substr($person,43,4); if ($sex eq 'M') { $kids = '----'; } if ($age <= 10) { $kids = '----'; } # print $rhh," ",$sex," ",$age," ",$mar," ",$kids,"\n"; } # # $prv = substr($_, 0,2); #province # $prf = substr($_, 2,2); #prefecture # $cnt = substr($_, 4,2); #county # $twn = substr($_, 6,3); #township # $vil = substr($_, 9,2); #village # $ced = substr($_,11,2); #census enumeration district # $hid = substr($_,13,3); #household id number # $pid = substr($_,16,2); #person id number # $rhh = substr($_,18,1); #relation to head of household # $sex = substr($_,19,1); #sex # $yrb = substr($_,20,3); #year of birth # $mnb = substr($_,23,2); #month of birth # $eth = substr($_,25,2); #ethnicity # $hhr = substr($_,27,1); #household registration # $tyr = substr($_,28,1); #type of registration # $mig = substr($_,29,2); #migration # $res = substr($_,31,1); #type of residence at origin # $rea = substr($_,32,1); #reason for migration # $eda = substr($_,33,1); #educational attainment # $eds = substr($_,34,1); #educational enrollment status # $ind = substr($_,35,3); #industry # $occ = substr($_,38,3); #occupation # $emp = substr($_,41,1); #employment status # $mar = substr($_,42,1); #marital status # $cbm = substr($_,43,1); #children born, male # $cbf = substr($_,44,1); #children born, female # $csm = substr($_,45,1); #children surviving, male # $csf = substr($_,46,1); #children surviving, female # $hht = substr($_,49,1); #household type # $hhn = substr($_,50,2); #number of persons in household