Re: Iterating thru array of hashes

Graham Barr <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.ldap
Message-ID <[email protected]>
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.