Manipulating arrays in perl

Hey Experts,

I'm at a loss with a matching/array manipulation issue in perl.

I have 2 inputs:

1 file in format (file A):

1 file of domains in format (file B)

What i want to do is strip the tld from the domains in file B and compare it to the first column in file A. If that first column matches then take the value in the second column to use for a whois search.

I keep getting uninitialized errors from trying to split the file A into 2 arrays. I've tried many variations in coding but the latest one I have is below.

FYI, I'm pretty sure it would've been easier to do if I had split file A into a hash but I'm not good at working with hashes so I made 2 arrays instead.  Also, not doing any calculations yet as I haven't gotten the code to work hence all the print statements.

Please help. Thanks in advance.

foreach my $domain (@file_B){
#strip off tld from domains
        my $tld = (split(/\./, $domain))[-1];
        print "========= $domain =========\n";

#split the columns up into 2 arrays
        foreach my $map (@file_A){
                my @arr = split(/ /,$map);
                my @arr2 = split(/\n/,$arr[0]);
                my @arr3 = split(/\n/,$arr[1]);
#compare the tld to arr2 made from column 1 of file B
                if ($tld eq @arr2) {
                        print "Finally a match!!!\n";
                        $server = @arr3;                     #assing the whois server to a variable
                        print "$server\n";
        print "The given domain is: $domain\n";
        print "The given tld is: $tld\n";
        print "The tld ($tld) matches with $server\n";
        print "The whois command is: $whois_cmd -h $server $domain\n";
        print "\n\n";

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The splits on line 9 and 10 will not do what you think they'll do.  @arr on line 8 will be the split of a single line in the file.  @arr2 will never have more than one value.  @arr3 may have 2 values (the whois server from that line and nothing after the \n at the end of the line).

I'll try to take some time later to rework your code (probably using a hash for file A) but I won't have the time for a few hours yet.b
This is how I'd do it.  I went with a complete rewrite because your code would require very significant changes to get working.

my %lkup;
open IN, "file_A" or die "could not open file_A: $!";
while (<IN>) {
    chomp;  # remove newline
    my ($tld, $srv) = split;  # implicit split of $_ on whitespace
    $lkup{$tld} = $srv;
close IN;
open IN, "file_B" or die "could not open file_B: $!";
while (<IN>) {
    my $tld = (split /\./)[-1];
    my $srv = $lkup{$tld} or die "could not find a whois server for $tld ($_)";

    print "The given domain is: $_\n";
    print "The given tld is: $tld\n";
    print "The tld ($tld) matches with $srv\n";
    print "The whois command is: $whois_cmd -h $srv $_\n";
    print "\n\n";

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akatsuki27Author Commented:
Thanks, Wilcoxon.

The solution worked well and I learned a few things about associative arrays in the process so double win for me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.