PerlUserNL has asked for the wisdom of the Perl Monks concerning the following question:

I got the following script:

use strict; use warnings; use Win32::OLE; my $ou; my @filter; my $obj; $ou=Win32::OLE->GetObject("LDAP://ou=NID_Users,dc=A34,dc=NID"); @filter=("user"); $ou->{filter}=\@filter; print "Hier Volgt een lijst van gebruikers:\n"; foreach $obj (in $ou){ print "$obj->{name}\n"; print " First name: " ; print "$obj->{givenName}\n" ; print "###############\n\n\n" ; }

I want to find the users with the same givenName.

How Can I do this?

Replies are listed 'Best First'.
Re: Compare usernames
by Ratazong (Monsignor) on Jan 13, 2011 at 12:09 UTC

    If you think "duplicate" (or "unique"), think about using a hash!

    foreach $obj (in $ou){ ... print "$obj->{givenName}\n" ; ... }
    In that loop, just create a hash-entry for each user, using $obj->{givenName} as the key...

    ... unless, however, an entry for that key already exists. In this case you have found a duplicate :-)

    HTH, Rata
Re: Compare usernames
by Corion (Patriarch) on Jan 13, 2011 at 12:07 UTC

    Maybe by fetching all users and comparing their givenName properties?

    Where do you have problems?

      now the problem is: I'm a beginner in the scripting world. I always got problem with scripts, but if i see a script that looks like the script i have to made, i now what to do. Now I have to made a script that compare the user first name, output the users with the same name. And copy the usernames (of the users with the same name) tot the General field of the user with the same name. The problem is: I don't now how to start en how i can build up this script.

        See perlfaq4 for "duplicate". That shows the basic approach of removing duplicates from any kind of list.

        Adapting this algorithm to your specific needs is left as an exercise.

Re: Compare usernames
by Anonymous Monk on Jan 13, 2011 at 13:46 UTC
    my %found; for my $user (in $ou) { push @{ $found{ $obj->{givenName} } }, $user; } for my $given (keys %found) { my $users = $found{$given}; next if @$users < 2; # skip unique names print "found ".@$users." users with given name $given:\n"; print "\t", join(', ', map { $_->{name} } @$users), "\n"; }