Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash... 3

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all!

I am retriving a set of values from a ldap server, the values are attribute name and the value it holds...i collect this values in a hash...

say something like this

foreach $attr (@aarry)
{
$hold{$attr}=$value
}

If my ldap search yield more than one result then i have write one more foreach loop over this...
say something like this

foreach $entry (@entries)
{

foreach $attr (@aarry)
{
$hold{$attr}=$value
}
}

problem here is for second set of result the attribute name(ie key) is going to be same but the values will change.

so its going to over write values.

Don't know how to solve this so i tried something like this

$k=1;
foreach $entry (@entries)
{

foreach $attr (@aarry)
{
$hold.$k{$attr}=$value;
--
}
$k++;
}

Didn't work ..i haven't used hashes a lot any ideas will be really appreciated.

Thank you

This code may not make sense uses NET-LDAP module.

Code:
     foreach $entry ($result->entries)
       {
        # print $entry->dump;
         
         
         foreach $attr ( sort $entry->attributes )
           {
         next if ($attr =~ /;binary$/ );
       [COLOR=red]  $hold{$attr}=$entry->get_value($attr);[/color]
## i am storing attribute as key and  values as thier values
         print " $attr : ", $entry->get_value ($attr), "\n";
         $k++;
           }
                                                    
       }
 
Can perl do muultidimensional arrays?

pseudocode:

Code:
#declare array

$k=1; #declare counter
foreach $entry (@entries)
{
 # if array[key1] doesn't exist initialize it
 foreach $attr (@aarry)
  {
    # array[key1][key2] = value
    $hold.$k{$attr}=$value;
         --
  }  
  $k++;
}

Not that I am adept at Perl.

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
I'm not sure I understand the problem. It doesn't look like your code exactly follows your description. However, my suggestion would be a hash of arrays. The hash keys will be your 'entries/attributes' and the array that the key references will be a list of all your 'values'.

This code might give you a direction to go in:

Code:
my @entries = qw/entry1 entry2 entry3/;
my %values1 = (
    "entry1" => "value1",
    "entry2" => "value2",
    "entry3" => "value3"    );
my %values2 = (
    "entry1" => "value11",
    "entry2" => "value12",
    "entry3" => "value13"   );
my %hold;

foreach my $entry (@entries) {
    foreach my $value ($values1{$entry}) {
        push @{$hold{$entry}}, $value;
    }

    foreach my $value2 ($values2{$entry}) {
        push @{$hold{$entry}}, $value2;
    }
}

I used the hashes %values1 and %values2 to simulate the info that would be returned by your server queries - so don't worry too much about those.

You can access the attribs and values similar to this:

Code:
# Print all the values for entry2
foreach (@{$hold{entry2}}) {
    print "$_\n";
}

# Print all values for all entries
foreach my $entry (keys %hold) {
    print "$entry\n";

    foreach my $value (@{$hold{$entry}}) {
        print "\t- $value\n";
    }
}
 
Thank you all,
I should have made it clear what i am looking for, How to make 'Hash of hashes'
Finally i figured it ...

$k=1;
foreach $entry (@entries)
{

foreach $attr (@aarry)
{
$hold{$attr}{$k}=$value;
}
$k++;
}

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top