develooper Front page | perl.beginners | Postings from April 2002

Re: Checking against an assosiative array...

Thread Previous
From:
Tagore Smith
Date:
April 1, 2002 19:29
Subject:
Re: Checking against an assosiative array...
Message ID:
008401c1d9f5$c570bf40$0300a8c0@optonline.net
Daniel Falkenberg wrote:

<snip>
>%users = (
>          'username01' => {
>                      'Comp01' => '$0.00'
>                      },
>           'username02' => {
>                          'Comp02' => '$0.00'
>                        },
>           'usename03' => {
>                       'Comp03' => '-$9.90'
>  }
>);
>
>My questing is if I have the following variable...
>
>$variable = "usename01";
>
>How can I check that against the hash above.  If $variable doesn't match
>anything the print "Username does not match";

I'm not sure if you just want to check if the username exists, or get the
values associated with it. Also, it seems like you won't know the keys of
the nested hash. If that's the case the following checks to see if the key
$user exists and then gets the values in the associated hash.

if (defined(%{$users{$user}})){
    while((my $key, my $value)=each(%{$users{$user}})){
        print "$key: $value\n";
    }
}
else {
    print "No match for $user.";
}

Tagore Smith


Thread Previous


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