develooper Front page | perl.ldap | Postings from March 2010

Re: Iterating thru array of hashes

Thread Previous | Thread Next
From:
Graham Barr
Date:
March 29, 2010 12:04
Subject:
Re: Iterating thru array of hashes
Message ID:
B6A2EE13-95D0-447A-8ADC-981F3B75F36D@pobox.com

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.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About