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?
wilcoxonConnect With a Mentor Commented:
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

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
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.