contrite_newbie has asked for the wisdom of the Perl Monks concerning the following question:

I am parsing an LDIF file with
use warnings; use strict; my $LDIF = 'c:/test/any.ldf' my $csv = 'c:/test/any.csv' open ("FILE1", $LDIF) || die "can't open $LDIF: $!"; local $/ = "\n\n"; # Set an empty newline as line separator while (<$LDIF>) { my ($dn) = /^dn: (.*)$/m; my ($samACCOUNTNAME) = /^samACCOUNTNAME: (.*)$/m; }
what I need to do is take the $dn and $samACCOUNTNAME values and pass them into a new array that I can reference for matching to a value in $csv. Any suggestions would be cool...I keep over-complicating this and I am getting frustrated... TIA

Replies are listed 'Best First'.
Re: Passing 2 elements into a new associative array
by bart (Canon) on Oct 07, 2004 at 03:40 UTC
    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]
      Thx, Bart...I didn't think of using %foo before the while statement in order to create the hash. I want samACCOUNTNAME as the $key/$value pairing where $value will equal the username value in the csv file...
Re: Passing 2 elements into a new associative array
by dragonchild (Archbishop) on Oct 07, 2004 at 03:36 UTC
    First, check the syntax for open(). I don't think you're doing what you think you're doing.

    Second, I'm not quite sure what you're trying to do. You need to be more clear as to what you're trying to match in $csv and how you want to match them.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      u r right...but the fh isn't the problem...:-)
        u r right...but the fh isn't the problem...:-)

        I have a feeling this is going to get me downvotes, but it's for your own good, ya hear!

        How hard is it to write out "You" and "are"? You avoided pressing 4 keys on your keyboard, and as a result you look silly and childish. There are some people who don't mind "u r", but I do, and I know others who feel the same. Please, at least try to humor us. It's really not that hard.

        More substantively, the FH may not be "the" problem in your mind, but it's still a problem. When you paste code, it's a large part of communicating your problem. When your code has errors other than the one you want help with, people are going to get distracted, and ignore the thing you're actually asking about. Thus, it's in your own best interest to post code that is as correct as possible. I know this from experience. Testing a 5 line snippet before posting is not that difficult, so do it!