Angus Glanville wrote:
I have a small hash of directory values. When a user inputs a directory value I want to check to see if this directory value is in my hash. If it is not the requested function will be denied. My problem is that when I iterate over the hash I get answers for every key in the hash. so in my example below if I enter "/usr/bin I will get a "match found" and "no match found". What I want is to only get a "no match found error if the value is never found in the hash. I tried using "exists" but this seems to check for the existence of a key not a value, and the user will input the value.

If you cannot - or don't want to - adapt your data structure, the fact that your list happens to be hash values is not relevant. You actually just want to check for the existence of a value in a list or array, which is a FAQ:

    perldoc -q contained

my %directory_hash = ("dir1" => "/usr/bin",
              "dir2" => "/users/home",);
foreach my $value ( values %directory_hash) {
    unless ($value eq $dir) {
        print "no match found!\n";
        last;
        }
        print "match found\n"
    }

You probably want:

    my $found;
    foreach my $value ( values %directory_hash) {
        if ($value eq $dir) {
            print "match found\n";
            $found = 1;
            last;
        }
    }
    print "no match found!\n" unless $found;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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


Reply via email to