Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing hashes.... 1

Status
Not open for further replies.

nychris

MIS
Dec 4, 2004
103
US
I have two hashes in the form of "mount_point = NFS_Server:source". The first hash, %curr, lists the currently mounted NFS exports on a system. The second hash, %mounts, lists the correct mounts.

Code:
%curr = (
   '/appA' => 'serverA:/appA',
   '/db' => 'serverB:/db',
   '/oracle' => 'serverC:/oracle',
   '/temp' => 'serverX:/temp'
);


%mounts = (
   '/appC' => 'serverA:/appC',
   '/db' => 'serverB:/db',
   '/oracle' => 'serverF:/oracle',
   '/mysql' => 'serverZ:/mysql'
);


Anything in %curr that does not exist in %mounts should be unmounted. Anything in %mounts that does not exist in %curr, needs to be mounted.

The output should look something like this:

MOUNT: serverF:/oracle on /oracle
MOUNT: serverA:/appC on /appC
MOUNT: serverZ:/mysql on /mysql

UNMOUNT: /appA
UNMOUNT: /oracle
UNMOUNT: /temp

I'm just comparing these hashes to make sure their key=value pairs match. Any ideas?

Thanks,
Chris
 
Code:
foreach my $key (keys %mount) {
   if (not exists $curr{$key}) {
      #...
   }
}

foreach my $key (keys %curr) {
   if (not exists $mount{$key}) {
      # ...
   }
}

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Kirsle, that code only checks the key (local mount point). I need it to check the entire "local mount point = NFS server:source_share" key=value combination.

I was thinking of joining each key=value pair by a delimiter, a semicolon or whatever, and then pushing each to an array and then grepping through the array to see whats missing or wrong, but I can't get that code working right.
 
one way:

Code:
my %curr = (
   '/appA' => 'serverA:/appA',
   '/db' => 'serverB:/db',
   '/oracle' => 'serverC:/oracle',
   '/temp' => 'serverX:/temp'
);

my %mounts = (
   '/appC' => 'serverA:/appC',
   '/db' => 'serverB:/db',
   '/oracle' => 'serverF:/oracle',
   '/mysql' => 'serverZ:/mysql'
);

my @mount = ();
my @unmount =();

foreach my $keys (keys %curr) {
   push @unmount,"UNMOUNT: $keys" if ("$keys$curr{$keys}" ne "$keys$mounts{$keys}");
}

foreach my $keys (keys %mounts) {
   push( @mount, "MOUNT $mounts{$keys} on $keys") if ("$keys$curr{$keys}" ne "$keys$mounts{$keys}"); 
}

print "$_\n" for @unmount;
print "$_\n" for @mount;

you cuold really use one array though for the output:

Code:
my @results = ();
my @unmount =();

foreach my $keys (keys %curr) {
   push @results,"UNMOUNT: $keys" if ("$keys$curr{$keys}" ne "$keys$mounts{$keys}");
}

foreach my $keys (keys %mounts) {
   push( @results, "MOUNT $mounts{$keys} on $keys") if ("$keys$curr{$keys}" ne "$keys$mounts{$keys}"); 
}

print "$_\n" for @results;

or just skip the array and print directly to the terminal or a file. Of course this is contrived to fit the exact data you posted. If the real data differs then it may not work like I have it.

- Kevin, perl coder unexceptional!
 
forgive my slightly sloppy code, I'm struggling with a keyboard that is starting to go bad on me and is making it hard to write code or post.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top