How about something like this:

my %machines = (); # List of machines with their IPs
my %ipaddrss = (); # List of IPs with their machines

# We assume the db is a flat file and that the file
# is already opened.  We also assume that the fields
# are separated by whitespace (as per your example).
while (my $line = <FH>) {
   chomp($line);

   # Split the record by whitespace
   my ($host, $ip) = split(/\s+/, $line);

   # Save the ip in the machine's record
   if (exists $machines{$host}) {
       push(@{ $machines{$host} }, $ip);
   } else {
       $machines{$host} = [ $ip ];
   }

   # Save the host in the ip's record
   if (exists $ipaddrss{$ip}) {
       push(@{ $ipaddrss{$ip} }, $host);
   } else {
       $ipaddrss{$ip} = [ $host ];
   }
}


At the end you'll have 2 hashes, one containing a list of machines and the IPs 
that belong to each one, and the other containing a list of IPs and the 
machines they belong to.  I am assuming that you want to find the duplicate 
records and store them to use them for something.  If you just want to count 
how many times they occur then its different.

To access the stored duplicates, just do something like this:

# Output all the IPs belonging to $host
my $host = 'm001ws';
foreach my $ip ( @{ $machines{$host} }) {
   print $ip;
}

# Output all the hosts belonging to $ip
my $ip = '192.168.20.2';
foreach my $host ( @{ $ipaddrss{$ip} }) {
   print $host;
}

I hope this helps!

     -dZ.

----- Original Message -----
From: King, Dan
Sent: 5/8/2006 2:23:13 PM
To: [email protected]
Subject: Finding partly duplicated records

> I am at my wits end on this question.  I feel like the answer is 
> probably simple but it has me beat.
> 
> 
> I have a list of machines and IP addresses.  The combination, 
> $machine and $IPAddress is never duplicated in the list.  However, 
> $IPAddress may be duplicated and $machine may be duplicated.  
> 
> For example:
> 
>     m001ws     192.168.20.2
>     m002ws     192.168.20.4
>     m003ws     192.168.20.6
>     m003ws     192.168.20.10
>     m004ws     192.168.20.9
>     m005ws     192.168.20.20
>     m006ws     192.168.20.12
>     m009ws     192.168.20.12
> 
> I want to find m003ws with its two IP addresses 
>     m003ws     192.168.20.6
>     m003ws     192.168.20.10
> 
> and 192.168.20.12 with its two machines.
>     m006ws     192.168.20.12
>     m009ws     192.168.20.12
> 
> I also have a longer record consisting of about 9 fields where I want 
> to know if there is a duplication within one of the fields.  For example
> 
> any time a userid appeared in more than one record.
> 
> I was thinking that these are basically the same problem but I can't 
> seem to find any examples on the web.  I would appreciate any help.  
> By the way, in the real problems I am looking at several thousand 
> 'records' and I need to run these reports once a day.
> 
> Dan
> 
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to