Hi,

I am trying to retrieve a list (of users) from an array of hashes.

I have a structure like this:
$VAR59 = {
           'lc_surname' => 'anderson',
           'first' => 'Angela',
           'file' => 'angela_anderson.clk',
           'name' => 'Angela Anderson',
           'id' => 69,
           'surname' => 'Anderson'
         };
$VAR60 = {
           'lc_surname' => 'savilahti',
           'first' => 'Pirjo',
           'file' => 'pirjo_savilahti.clk',
           'name' => 'Pirjo Savilahti',
           'id' => 70,
           'surname' => 'Savilahti'
         };


What I want to to find all users, say with the lc_surname =~ anderson 
and ideally I'd like all the details for that user.

I was thinking this is either a job for exists,defined or grep but my 
efforts are not being rewarded.

Below is a snip of what I tried. The exists (I also tried defined) 
fails and I am not sure why. Does exists only work on hash keys?

Should I be using grep for this? I think it would be quicker but I am 
not am not sure how to make it work with this data structure.

Sorry if I am being dim, perhaps I have been bashing at this too 
long.
TIA.
Dp.



use strict;
use warnings;
use diagnostics;
...
...
..

sub check_surname {
###############################
# Open the users file and
# check that the user exists
#
# check_surname($name_to_check);
# return @list_to_names_found;
#
################################

 my $n = shift;
 my $ary_users = retrieve($users);
 die "Unabe to open $users\n" unless defined $ary_users;

 my $lc_surname = lc($n);
 my @found_users;
 for (my $i = 0;$i < @{$ary_users}; ++$i) {
        if (defined($ary_users->[$i]->{$lc_surname}) ) {
                print STDERR "Found \"$lc_surname\" \"$ary_users-
>[$i]->{lc_surname}\"\n";
                push(@found_users,$ary_users->[$i]->{$lc_surname});
        }
 }
 return(@found_users);
}
 return(@found_users);
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to