Re: Iterating thru array of hashes

[email protected] (Prashanth Sundaram)
Newsgroups perl.ldap
Message-ID <C7D78C1B.16BE9%[email protected]>
Thanks Graham.

1. I am using objectClass as an array in the hash. I am getting 'Object
Class violation' in the ldapadd op. Should I be using a 3-D array hash?

$user{'loginShell'} = $loginShell;
$user{'homeDirectory'} = "/home/$sAMAccountName";
$user{'gecos'} = $cn;
$user{'objectClass'} =
['top','shadowAccount','posixAccount','person','account','inetorgperson','or
ganizationalPerson'];
push @add_user,\%user;

2. You are right, I get a flat DN when trying to iterate thru the array of
hashes. How do I make the below code to print the different values that I
pushed in the above code snippet?
 
code:
my(@add_user)=@_;
my %value = @_;
foreach $value ( @add_user )
{ 
 $entry = Net::LDAP::Entry->new;
 $dn = "uid=".$value->{'uid'}.",ou=People,".$ldap_base;
 print "DN:$dn \n";
}

The above code currently prints the last appended value to the output and
due to object class violation LDAP-ADD is throwing error.

Thanks,
Prashanth


On 3/29/10 3:04 PM, "Graham Barr" <[email protected]> wrote:

> 
> On Mar 29, 2010, at 1:56 PM, Prashanth Sundaram wrote:
> 
>> Hello,
>> 
>> I need some help in understanding how to iterate thru array of hashes. I am
>> basically pushing a hash into array and then passing the array to another
>> function to iterate thru the values. I could do it parallely but I wanted to
>> check the size of the array and make sure it is within the defined limits.
>> 
>> Q: After passing the array, how can I iterate thru the entries to perform an
>> LDAP-ADD. I am getting error adding. Any help or optimization for this?
>> 
>> Does someone have a better way of doing this? I am performing a one-way sync
>> from AD to LDAP with custom attributes and also making sure the additiona nd
>> deletion list is within a specified limit.
>> 
>> code:
>>         $user{'cn'} = $cn;
>>         $user{'givenName'}=$givenName;
>>         $user{'sn'} = $sn;
>>         $user{'description'} = $description;
>>         $user{'mail'} = $mail;
>>         $user{'uid'} = $sAMAccountName;
>>         $user{'objectClass'} = @objectClass;
>>         $user{'userPassword'} = &get_password;
>>         $user{'gidNumber'} = $gidNumber;
>>         $user{'loginShell'} = $loginShell;
>>         $user{'HomeDirectory'} = "/home/$sAMAccountName";
>>         $user{'gecos'} = $cn;
>>         $user{'uidNumber'} = $uidNumber;
>>         push @add_user,%user;
> 
> you cannot push hashes, perl will flatten it and push its contents.
> 
> You need to push a reference to the hash
> 
>   push @add_user, \%user;
> 
>> 
>> Routine-2
>> foreach $item ( @add_user )
>>    {
>>      $entry = Net::LDAP::Entry->new;
>>      $dn = "uid=".$item{'uid'}.",ou=People,".$ldap_base;
> 
> as $item is a reference to a hash, you have to access it as one
> 
>   $item->{'uid'}
> 
>>      print "DN:$dn \n";  # Test to see if DN prints properly
>> 
>>      my $mesg_ld = $ldap_ld->add($dn,attr => [ %item ] );
> 
> Graham.
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.