in reply to Re: Multiple identical keys in a hash.
in thread Multiple identical keys in a hash.

I forgot to say, the value of dn will be the $level1 key and all of the other name=value pairs will be $level2 key = value.

Your humble servant,
-Chuck
  • Comment on RE: Re: Multiple identical keys in a hash.

Replies are listed 'Best First'.
RE: RE: Re: Multiple identical keys in a hash.
by zodiac (Beadle) on Apr 25, 2000 at 15:26 UTC
    So do you want a entry like:
    dn=machinename1
    name=value
    
    to yield:
    $hash{"machinename1"}{"name"}="value";
    
    or
    $hash{"machinename1"}{"name=value"}=1;
    
    in case 1 you should save the different values for name in an array. something like this should work
    foreach(@files){
        my $dn = "";
        open FILE, "$_" or die "error opening $_: ".$!;
        foreach(<FILE>){
    	chomp;
            $dn = $1 if(/dn=(.*)/);
            push @{$hash{$dn}{$1}}, $2 if(/(^=*)=(.*)/ && $dn ne "");
        }
        close FILE;
    }
    
    if it is case2, I don't know why you would need to add the same line more than once. if you need the number of occurances try :
     
    foreach(<FILE>){
        chomp;
        $dn = $1 if(/dn=(.*)/);
        $hash{$dn}{$_}++ if(/=)/ && $dn ne ""); 
    }
    
    for the inner loop. or use an array for the "name=value" entries:
    foreach(<FILE>){
        chomp;
        $dn = $1 if(/dn=(.*)/);
        push @{$hash{$dn}}, $_ if(/=/ && $dn ne "");
    }
    
    hope that helps.