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

I am using a module's "fetch" function, which returns a hash of data from a (LDAP) database. It's used as follows:
my $data = $self->{'ldap'}->fetch( source => $self->{'config'}->{'ldap.base'}, filter => "cn=$self->{'arg'}->{'host'}" );
This is useful because it organizes the data based on DNs:
$data = { 'cn=hostA,ou=Hosts,dc=company,dc=com' = { cn => 'hostA', ipAddress => '192.168.0.1', }, 'cn=hostB,ou=Hosts,dc=company,dc=com' = { cn => 'hostB', ipAddress => '192.168.0.2', } }
...and so on. The problem is, I am using this method to (occasionally) return only one DN. At the moment, I'm using the following code to transform $data so that it does not contain the DN key:
map { $data = $data->{$_}; } keys %{$data};
But I can only imagine that there must be a better way. Is there? The catch is to assume that the key (DN) is not known in advance.

Thanks.

-s.

Replies are listed 'Best First'.
Re: Transforming hash (removing useless key)
by friedo (Prior) on Jan 28, 2005 at 19:38 UTC
    I abhor the use of map in void context, so I would do something like this:

    my ( $thing ) = values %$data;
Re: Transforming hash (removing useless key)
by bgreenlee (Friar) on Jan 28, 2005 at 20:18 UTC
    You could also do:
    my $result = (each %$data)[1];

    -b

Re: Transforming hash (removing useless key)
by olivierp (Hermit) on Jan 28, 2005 at 21:12 UTC
    As you mention you will be only returning a single key, how about:
    $data = delete $data->{ (keys(%$data))[0] };
    HTH
    --
    Olivier