I have created a subroutine that creates a HoA out of the tab delimted below.
header_map.txt:
account_number_header account
account_number_header Account #
account_number_header Account No.
account_number_header Account number
account_number_header Account_Id
first_Name_header name1
first_Name_header first name
first_Name_header account name1
first_Name_header first_name
first_Name_header f name
last_Name_header name2
last_Name_header last name
last_Name_header account name2
last_Name_header last_name
last_Name_header l name
address_header address1
address_header address
address_header addresses
address_header place of residency
address_header location
The sub then bounces the array off the values of given keys (shown below). Where the values match the array, the index of the matching array element is returned. What I want to do is instead of searching a predefined constant array, I want to search through an array that is read from a file, or in this case data. The working code is below for the constant array.
my @fields = ('Account No.','name1','name2','location'); #array being searched
my $hm = "header_map.txt"; #declare variable to file
my $fh = (readfile($hm)); #declare variable to sub routine call
my $address_header = 'address_header'; #my given key
my $address = hashofarray($fh,$address_header); #looking for($fh,key) in sub
my $account_number_header = 'account_number_header'; #my given key
my $account_number = hashofarray($fh,$account_number_header); #looking for($fh,key) in sub
print $address,",",$account_number,"\n"; #prints desired array indexes of given keys
sub hashofarray {
my $fh = shift;
my $key = shift;
my %hash;
while (<$fh>) { # creating HoA
chomp;
my ( $key, $value ) = split /\t/;
push (@{ $header_map{$key} }, $value);
}
foreach my $key1 (@{$header_map{$key}}) {
if (my @index = grep { $fields[$_] eq $key1 } 0..$#fields) {
return $index[0];
}
}
}
sub readfile {
my $file = shift;
open my $f, '<', $file or die $!;
return $f;
}
RESULTS
location,Account No.
This is good and what I want, however I would like to read the array @fields from DATA file instead. Here is my attempt while reading DATA.
Failed Attempt
my $hm = "O:/josh/trade_data/mock_header_map.txt"; # declare variable to file
my $fh = (readfile($hm)); # declare variable to sub routine call
while (<DATA>) { # calling the subroutine after reading DATA
my @fields = split /\t/;
my $address_header = 'address_header'; # my given key
my $address = hashofarray($fh, $address_header); # looking for($fh, key) in sub
my $account_number_header = 'account_number_header'; # my given key
# looking for($fh, key) in sub
my $account_number = hashofarray($fh, $account_number_header);
# prints desired array indexes of given keys
print $address, ",", $account_number, "\n";
}
sub hashofarray {
my $fh = shift;
my $key = shift;
my %hash;
while (<$fh>) { #creating HoA
chomp;
my ( $key, $value ) = split /\t/;
push (@{ $header_map{$key} }, $value);
}
foreach my $key1 (@{$header_map{$key}}) {
if(my @index = grep { $fields[$_] eq $key1 } 0..$#fields) {
return $index[0];
} else {
print "not found";
}
}
}
sub readfile {
my $file = shift;
open my $f, '<', $file or die $!;
return $f;
}
__DATA__
Account No name1 name2 location
1 josh smith 411 s chirris ave. sometown st 12345
1 josh smith 411 s chirris ave. sometown st 12345
1 josh smith 411 s chirris ave. sometown st 12345
1 josh smith 411 s chirris ave. sometown st 12345
My results
,
,
,
,
,
Desired Results
1 411 s chirris ave. sometown st 12345
1 411 s chirris ave. sometown st 12345
1 411 s chirris ave. sometown st 12345
1 411 s chirris ave. sometown st 12345
In the end, I would like to print the desired columns, which I would be able to do if I could read DATA into the array, instead I am getting empty strings because the sub does not recognize @fields. I know I need to do something with array refernces but I'm a little off on those..any suggestions? I hope this is clear.
hashofarray(e.g. inreadfileinstead).