in reply to Passing 2 elements into a new associative array
Reverse the placement of the two variables in my additional line, if my assumption is the wrong way around.my %lookup; while (<$LDIF>) { my ($dn) = /^dn: (.*)$/m; my ($samACCOUNTNAME) = /^samACCOUNTNAME: (.*)$/m; $lookup{$dn} = $samACCOUNTNAME if defined $dn and defined $samACCOUNTNAME; # only if both are fou +nd }
In case you don't know what to do next: you can look up the account name by dn simply by using $lookup{$current_dn}. exists $lookup{$current_dn} may help to see if there is a value for it in this table, though with all values garanteed to be defined — due to the way I added them, you could just as well use defined instead. Or, if all valid account names are supposed to be neither "" nor "0", just test its truth value, as in
if($lookup{$current_dn}) { ... }
You want to associate more than one value with one key, you'll have to use arrays for the hash values. There is no middle ground, you either use the strings for value, or use arrays for all. Simply change my added line to:
The first name would then be inpush @{$lookup{$dn}}, $samACCOUNTNAME;
$lookup{$current_dn}[0]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing 2 elements into a new associative array
by contrite_newbie (Novice) on Oct 07, 2004 at 04:21 UTC |