in reply to hash comparison & nested selections
As for the part that's messing you up, your pattern to check for an email address in $emailString is probably only capturing portions of some addresses -- your regex, when given a perfectly acceptable address like "my.name@host.domain.net", will only capture "name@host.domain", which would be wrong. Is that the sort of problem you're asking about? (You didn't really indicate what sort of problem you're having.)
What sort of stuff is in users.txt? Is it a flat table file with some sort of regular delimiter between the fields on each line, and is the email address always in the same field position? If so, then you'd want to use split instead of a regex to grab the email address; e.g. suppose the fields are tab-delimited, and the the email address is the second field:
You're still likely have some trouble with the grep, if your username keys include things like "rob", "robert", "latrobe", etc. When you hand the shortest one of these to grep (in perl or on the command line), it will return all three users. In your original code, the value of $emailString would contain three entries from the users.txt, separated by newline characters within the one long scalar string. If "rob" comes later in the file than "latrobe", you never get to see rob's email address. As for the code I suggested above, $emailString will contain only one entry from users.txt, but if "latrobe" came first in the file, that will be the entry you get for "rob". (update: you can fix this easily enough, of course, just by using suitable anchors around the username in the grep regex, e.g. /\b\Q$_\E\b/ or whatever)open( USRS, "users.txt" ) or die "no users.txt: $!"; my @emailStrings = <USRS>; # let's just read this once close USRS; foreach ( keys %postiniList ) { if ( exists $ldapList{$_} ) { print "$_ exists in ldap\n"; # don't need a "next;" here } else { print "$_ does not exist in ldap\n"; my ( $match ) = grep /\Q$_\E/, @emailStrings; my $emailString = ( split /\t/, $match )[1]; print "deleteuser $emailString\n" } }
Let me make another guess, that the usernames (keys for %postiniList and %ldapList) happen to be another delimited field in users.txt, in which case you'd want to read users.txt once, and for each line therein, use split to get both the username and email fields -- eg. suppose that they are actually the first and third fields, respectively:
open( USRS, "users.txt" ) or die "no users.txt: $!"; while (<USRS>) { my ($keystring, $emailstring) = (split /\t/, $_)[0,2]; if ( exists $postiniList{$keystring} ) { my $report = "$keystring exists in ldap\n"; unless ( exists $ldapList{$keystring} ) { $report =~ s/ exists / does not exist /; $report .= "deleteuser $emailstring\n" if ( $emailstring ); } print $report; } } close USRS;
|
|---|