comparing elements of arrays
- From: theal@xxxxxxxxxxxx (Tony Heal)
- Date: Fri, 10 Aug 2007 16:23:13 -0400
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;
- Follow-Ups:
- Re: comparing elements of arrays
- From: Chris Charley
- Re: comparing elements of arrays
- Prev by Date: Perl LWP post to a web form
- Next by Date: reading from scalar File::Temp handle
- Previous by thread: Perl LWP post to a web form
- Next by thread: Re: comparing elements of arrays
- Index(es):
Relevant Pages
|