All you need is one more step... Only you didn't say which value you want to use as the hash key, thus, which value you want to use to do the lookup, and thus, which must be unique. I'll just assume that you want to use $dn for that. Then, change your code to:
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 }
Reverse the placement of the two variables in my additional line, if my assumption is the wrong way around.

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:

push @{$lookup{$dn}}, $samACCOUNTNAME;
The first name would then be in
$lookup{$current_dn}[0]

In reply to Re: Passing 2 elements into a new associative array by bart
in thread Passing 2 elements into a new associative array by contrite_newbie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.