comparing elements of arrays



There has got to be a better way (or more elegant way) to do this.

I have 2 DNS files from bind9. I have removed everything but the relevant info I want. That info is as follows:



aaaa A 123.213.123.123

aaaaa-staging A 123.123.123.122

bbbb CNAME software.mycompany.com

cccc A 12.12.12.12



One file (db.cust.com.new) is an updated version of the other (db.cust.com).

I want to compare the 2 and create a new file (or update db.cust.com )

I need to remove and log any entry that is in db.cust.com and not in db.cust.com.new

I need to log and add any entry in db.cust.com.new that is not in db.cust.com.

I need to keep any entry that is in both

I need to log any differences



Most of the logging I have been saving for later.

The script below is what I have so far and it does this:

Reads both files into separate arrays after replacing any white space with a single ',' in each element

Rotates thru db.cust.com.new and splits each element into a new array then determine if the 1st element of that arrays
exists in the 2nd array

If it does it compares the 2nd element then the 3rd

It print anything that matches all three elements to a file and print to screen all differences.



The last else will print new records to the file, but does so for the total number of elements in array #2 i.e. 200 time
for each new element



Also this script is only one way. If an element exists in array ! and not in array 2 it records that

If an element exists in array 2 and not in array 1 it does not even know it and never will.



So my real question is.... is there a better, more elegant way to do this or should I keep going?



Thanks



Tony







#!/usr/bin/perl

# AUTHOR Tony Heal - theal@xxxxxxxxxxxx

use strict;

use warnings;



my (@newRecord, @oldRecord, @newCompare, @oldCompare);

sub trimVar($)

{

my $string = shift;

chomp $string;

$string =~ s/\s+/,/g;

return $string;

}



open (NEWDNS, "db.cust.com.new") or die "can not open db.cust.com.new $!";

while (<NEWDNS>)

{

my $newLine = trimVar ($_);

push (@newRecord, $newLine);

}

close NEWDNS;



open (OLDDNS, "db.cust.com") or die "can not open db.cust.com $!";

while (<OLDDNS>)

{

my $oldLine = trimVar ($_);

push (@oldRecord, $oldLine);

}

close OLDDNS;



open (NEWFILE, ">new-cust-dns") or die "can not open new-cust-dns. $!";

foreach (@newRecord)

{

@newCompare = split (/,/,$_);

foreach (@oldRecord)

{

@oldCompare = split (/,/,$_);

if ( $newCompare[0] eq $oldCompare[0] )

{

if ( $newCompare[1] eq $oldCompare[1] )

{

if ( $newCompare[2] eq $oldCompare[2] )

{

print NEWFILE "$newCompare[0],$newCompare[1],$newCompare[2]\n";

}

else

{

print " $newCompare[0] has a mis-matched address. company has
\'$newCompare[2] and DNS has $oldCompare[2]\'\n";

}

}

else

{

print " $newCompare[0] has a mis-matched record type. company has
\'$newCompare[1] and DNS has $oldCompare[1]\'\n";

if ( $newCompare[2] ne $oldCompare[2] )

{

print " $newCompare[0] has a mis-matched address. company has
\'$newCompare[2] and DNS has $oldCompare[2]\'\n";

}

}



}

else

{

print NEWFILE "$newCompare[0]\t\t$newCompare[1]\t$newCompare[2] \; THIS IS A NEW RECORD\n";

}

}

}

close NEWFILE;



Relevant Pages

  • Re: New student stuck with .CSVs and Arrays
    ... public stock as string ... array so I can work with it in the program. ... Dim AL As New ArrayList ... Dim Entry As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: A[x][y][z]
    ... This would be a simple one-liner in the elegant Snobol language.Sadly, ... "Let's make our language more like Pascal." ... and array elements. ... whether 'a' is an array of pointers to arrays ...
    (comp.lang.c)
  • Re: A[x][y][z]
    ... > "Let's make our language more like Pascal." ... > and array elements. ... > based on a tiny set of elegant principles. ... > I think the problem is that novices can't ...
    (comp.lang.c)
  • Re: Searching a sorted array of strings
    ... and I did look at that entry, actually, after both searches failed. ... So my example stands, it is what I said it is, searching a sorted ... Are you asking us how you _should_ search for a string in an array? ...
    (comp.lang.perl.misc)
  • Re: Permission Denied when using File.Copy or fso.CopyFile
    ... the CIM_DataFile WMI Class. ... filename to an array for later retry. ... It's not the most elegant solution in the world but it's a nice hack ...
    (microsoft.public.scripting.vbscript)