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

Checking against an assosiative array...

Thread Next
From:
Daniel Falkenberg
Date:
April 1, 2002 17:02
Subject:
Checking against an assosiative array...
Message ID:
3ACA70B144BD6D45B994CAC2CA4B9F98017D03@opal.vintek.local
G'day All,

Just wondering how I would check the following variable against the hash
below?

  sub read_data {
    open USER_DATA, "$USER_DATA" or warn "cannot open '$USER_DATA' for
reading: $!\n";
    flock(USER_DATA, 2) || warn "Can't lock $USER_DATA exclusively: $!";
    while( $USER_DATA=<USER_DATA> ) {
      @USER_DATA = split (/,/, $USER_DATA);
      s/^"|"$//g foreach @USER_DATA;
      $username = $USER_DATA[0];
      $company  = $USER_DATA[2];
      $owing    = $USER_DATA[3];

      $users{$username}{$company} = $owing;

    }
    close USER_DATA;
    return %users;
  }

  read_data();

The sub read_data will create a hash similar to the following...

%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";

Kind Regards,

Dan


==============================
VINTEK CONSULTING PTY LTD
(ACN 088 825 209)
email:  dan@vintek.net
web:    <http://www.vintek.net>
Tel:    (08) 8523 5035
Fax:    (08) 8523 2104
Snail:  P.O. Box 312
        Gawler   SA   5118
==============================  

Thread Next


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