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

Hi, I'm doing some work with ldap on a MS system, trying to extract members of groups. The following code seems to do that...
use strict; use warnings; use Win32::OLE 'in'; use Data::Dumper::Simple; my $ADsPath = "LDAP://CN=users,DC=woodland,DC=wednet,DC=edu"; my $adsobj = Win32::OLE->GetObject("$ADsPath") or die "Unable to retri +eve the object for ADsPath\n"; warn Dumper($adsobj); foreach my $adsobj_child (in $adsobj) { if (($adsobj_child->{Class} eq "group") && ($adsobj_child->{samaccou +ntname} eq "Domain Admins")) { warn Dumper($adsobj_child->{member}); my @array = $adsobj_child->{member}; foreach (@array) { print "$_\n"; } } }
Now, warn Dumper($adsobj_child->{member}); shows me the members of the group as an array, but the foreach over either @array or directly over $adsobj_child->{member} just gives me ARRAY(0x1a94908). How do I get at that (I don't want to just assign the output of dump to a variable, I want to know what I'm not understanding!) Many thanks!

Replies are listed 'Best First'.
Re: Extracting elements of an array reference?
by ikegami (Patriarch) on Jul 26, 2006 at 15:26 UTC

    my @array = $adsobj_child->{member};
    just copies the reference.

    my @array = @{$adsobj_child->{member}};
    copies the array.

    But why copy?
    foreach (@{$adsobj_child->{member}})

Re: Extracting elements of an array reference?
by davorg (Chancellor) on Jul 26, 2006 at 15:34 UTC

    Either use the array reference:

    my $array_ref = $adsobj_child->{member}; foreach (@{$array_ref}) { print "$_\n"; }

    Or dereference it immediately:

    my @array = @{$adsobj_child->{member}}; foreach (@array) { print "$_\n"; }

    See perlreftut, perllol and perldsc for more examples.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks!