in reply to Re: Print hash keys and lookup the keys for values in another file -- oneliner and more explained
in thread Print hash keys and lookup the keys for values in another filr
#/usr/bin/perl -w use warnings; use strict; use Data::Dumper; my (@county,%result); my $inFile01 ="FILE01.dat"; #CLAYCOUNTY;Wood;statecode=FL #CLAYCOUNTY;Wood;statecode=FL #SUWANNEECOUNTY;Wood;statecode=FL #SUWANNEECOUNTY;Wood;statecode=TX #SUWANNEECOUNTY;Wood;statecode=TX #SUWANNEECOUNTY;Wood;statecode=TX #NASSAUCOUNTY;Wood;statecode=UT open(DATA01,'<',$inFile01)or die("Can't open input file\"$inFile01\":$ +!\n"); while (<DATA01>) { # Skipping if the line is empty or a comment next if ( $_ =~ /^\s*$/ ); next if ( $_ =~ /^#\s*/ ); my ($county,$srch,$stcode) = split(";",$_); chomp($srch,$county); if ($srch eq "Wood") { push (@county,$county) } } close(DATA01); my $inFile02 ="FILE02.dat"; #119736;Residential;CLAYCOUNTY #448094;Residential;CLAYCOUNTY #206893;Residential;CLAYCOUNTY #333743;Residential;CLAYCOUNTY #172534;Residential;CLAYCOUNTY #785275;Residential;CLAYCOUNTY #995932;Residential;CLAYCOUNTY #223488;Residential;CLAYCOUNTY #433512;Residential;CLAYCOUNTY #640802;Residential;SUWANNEECOUNTY #403866;Residential;SUWANNEECOUNTY #828788;Residential;SUWANNEECOUNTY #751490;Residential;SUWANNEECOUNTY #972562;Residential;SUWANNEECOUNTY #367541;Residential;SUWANNEECOUNTY #481360;Residential;SUWANNEECOUNTY #920232;Residential;NASSAUCOUNTY #727659;Residential;NASSAUCOUNTY #471817;Residential;NASSAUCOUNTY #983043;Residential;NASSAUCOUNTY #578286;Residential;NASSAUCOUNTY foreach my $cnty (@county) { my @countycode; open(DATA02,'<',$inFile02)or die("Can't open input file\"$inFi +le02\":$!\n"); while (<DATA02>) { # Skipping if the line is empty or a comment next if ( $_ =~ /^\s*$/ ); next if ( $_ =~ /^#\s*/ ); my ($code,$attr,$countyy) = split (";",$_); chomp ($code,$attr,$countyy); if ($countyy eq $cnty) { push @countycode, $code; } $result{$cnty} = [@countycode] } } close(DATA02); print Dumper \%result; foreach my $key (keys %result) { print "$key" . "\n"; my $op = join "|", @{$result{$var}}; print "$op" . "\n"; }
I am able to get thw desired values. One last thing with printing the output to an another file now which is required as below: for all the values in @{$result{$var}} I need to print as follows to a output file (No particular order) - for 119736 Need two lines in file as below (similarly for all).
L|A|119736|119736||||||||||||||||||||||| M|A|119736||||Wood|Wood|CONSTANT_STRING
complete file looks like
Thanks.L|A|119736|119736||||||||||||||||||||||| M|A|119736||||Wood|Wood|CONSTANT_STRING L|A|448094|119736||||||||||||||||||||||| M|A|448094||||Wood|Wood|CONSTANT_STRING L|A|206893|206893||||||||||||||||||||||| M|A|206893||||Wood|Wood|CONSTANT_STRING L|A|333743|333743||||||||||||||||||||||| M|A|333743||||Wood|Wood|CONSTANT_STRING L|A|172534|172534||||||||||||||||||||||| M|A|172534||||Wood|Wood|CONSTANT_STRING ..... .... ....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Print hash keys and lookup the keys for values in another file -- oneliner and more explained
by poj (Abbot) on Feb 10, 2017 at 17:18 UTC | |
by Magnolia25 (Sexton) on Feb 12, 2017 at 15:09 UTC |