----- Original Message ----- From: ""Tony Heal"" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <beginners@perl.org>
Sent: Friday, August 10, 2007 4:23 PM
Subject: 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
[snip code]

Below is one approach to see the differences in 2 arrays (by Randal Schwartz).

Chris



The>   How do I compute the difference of two arrays?  How do I compute the
The> intersection of two arrays?

Here's code that doesn't require that uniqueness, and would be a better
candidate for the FAQ:

   my %tally;
   $tally{$_} .= "a" for @array_a;
   $tally{$_} .= "b" for @array_b;
   my @union = keys %tally;
   my @intersection = grep $tally{$_} =~ /ab/, @union;
   my @a_not_b = grep $tally{$_} =~ /a$/, @union;
   my @b_not_a = grep $tally{$_} =~ /^b/, @union;

print "Just another Perl hacker,";


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to