bryced1234 has asked for the wisdom of the Perl Monks concerning the following question:
#Read a file that has the dates and IDs I need...each line of the file + looks like...11062000C12345678 open(DATA1, "<", "dates.txt") or die $!; while (<DATA1>) { my $dateindex = substr $_, 0, 8; my $candID = substr $_, 8, 9; #Now that I have the first date/ID I will now start to aggregate the m +oney by Zip Code...this requires opening a second file which is forma +tted like this...ZIP/MONEY/DATE/ID open(DATA2, "<", "indv2000.txt") or die $!; my %hash; while (<DATA2>) { my $zip = substr $_, 82, 5; my $money = substr $_, 130, 7; my $date = substr $_, 122, 8; my $cand1 = substr $_, 0, 9; #IF the date and candidate ID is equal to the one fed by the first whi +le loop...then aggregate the money by zip code if ($date == $dateindex && $cand1 eq $candID){ $hash{$zip} += $money; } } #NOW that I have the total money for zip code for the day and candidat +e ID in question....I will print the hash separated by commas with an + indication of the date/candidate ID used....I will use the dos comma +nd > to dump the output into a text file while ( ($k,$v) = each %hash ) { print "ZIP, MONEY, DATE, CANDID \n"; print "$k,$v, $dateindex, $candID, HASH \n"; } #repeat until the dates file is done... }
|
|---|