Trying to understand your files. You have one file of head values (whatever those are) one file filled with phone numbers, and one file filled with addresses. Is that correct? Each file can have multiple head, addresses, or phone numbers, and each file somehow corresponds to each other.
Could you give an example of the data in the files, and how they relate to each other? I'll update my answer as soon as I get a better understanding on what your data actually looks like.
Meanwhile, it's time to learn about references. References allow you to create more complex data structures. And, once you understand references, you can move onto Object Oriented Perl which will really allow you to tackle programming tasks that you didn't know were possible.
Perl references allow you to have hashes of hashes, arrays of arrays, arrays of hashes, or hashes of arrays, and of course those arrays or hashes in that array or hash can itself have arrays or hashes. Maybe an example will help.
Let's say you have a hash of people assigned by employee number. I'm assuming that your first file is employee_id|name
, and the second file is address|city_state
, and the third is home_phone|work_phone
:
First, just read in the files into arrays:
use strict;
use warnings;
use autodie;
use feature qw(say);
open my $heading_fh, "<", $file1;
open my $address_fh, "<", $file2;
open my $phone_fh, "<", $file3;
my @headings = <$heading_fh>;
chomp @headings;
close $heading_fh;
my @addresses = <$address_fh>;
chomp @addresses;
close $address_fh;
my @phones = <$phone_fh>;
chomp @phones;
close $phone_fh;
That'll make it easier to manipulate the various data streams. Now, we can go through each row:
my %employees;
for my $employee_number (0..$#headings) {
my ( $employee_id, $employee_name ) = split /\s*\|\s*/, $employees[$employee_number];
my ( $address, $city ) = split /\s*\|\s*/, $phones[$employee_number];
my ( $work_phone, $home_phone ) = split /\s*\|\s*/, $addresses[$employee_number];
my $employees{$employee_id}->{NAME} = $employee_name;
my $employees{$employee_id}->{ADDRESS} = $address;
my $employess{$employee_id}->{CITY} = $city;
my $employees{$employee_id}->{WORK} = $work_phone;
my $employees{$employee_id}->{HOME} = $home_phone;
}
Now, you have a single hash called %employees
that is keyed by the $employee_id
, and each entry in the hash is a reference to another hash. You have a hash of hashes.
The end result is a single data structure (your %employees
) that are keyed by $employee_id
, but each field is individually accessible. What is the name of employee number A103?, It's $employees{A103}->{NAME}
.
Code is far from complete. For example, you probably want to verify that all of your initial arrays are all the same size and die if they're not:
if ( ( not $#employees == $#phones ) or ( not $#employees == $#addresses ) ) {
die qq(The files don't have the same number of entries);
}
I hope the idea of using references and making use of more complex data structures makes things easier to handle. However, if you need more help. Post an example of what your data looks like. Also explain what the various fields are and how they relate to each other.
There are many postings on Stackoverflow are look like this to me:
My data looks like this:
ajdjadd|oieuqweoqwe|qwoeqwe|(asdad|asdads)|adsadsnrrd|hqweqwe
And, I need to make it look like this:
@#*()#&&###|@#*@#&)(*&!@!|@#@#&(*&@#