Home > Archive > PERL Beginners > January 2008 > how to take action based on the existence of a value in a hash
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to take action based on the existence of a value in a hash
|
|
| Angus Glanville 2008-01-15, 4:02 am |
| Hi,
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.
thanks in advance for any hints.
-Angus
#!/usr/bin/perl
use strict;
use warnings;
print "enter path:";
chomp (my $dir = <STDIN> );
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"
}
| |
|
| References: <EB0DA8DD-11C0-469F-96D8-CA6CD5C0B657@comcast.net>
In-Reply-To: <EB0DA8DD-11C0-469F-96D8-CA6CD5C0B657@comcast.net>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Posted-By: 213.64.27.67
Approved: news@nntp.perl.org
From: noreply@gunnar.cc (Gunnar Hjalmarsson)
Bytes: 3271
Lines: 43
Xref: number1.nntp.dca.giganews.com perl.beginners:53611
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
| |
| John W. Krahn 2008-01-15, 8:03 am |
| Angus Glanville wrote:
> Hi,
Hello,
> 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.
>
> thanks in advance for any hints.
>
> -Angus
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> print "enter path:";
> chomp (my $dir = <STDIN> );
>
> my %directory_hash = ("dir1" => "/usr/bin",
> "dir2" => "/users/home",);
Why are your hash keys "dir1" and "dir2"? What meaning do they have?
You should use the directory names for the keys:
my %directory_hash = (
'/usr/bin' => 1,
'/users/home' => 1,
);
Then the look-up would be easy:
print $directory_hash{ $dir } ? "match found\n" : "no match found!\n";
> foreach my $value ( values %directory_hash) {
> unless ($value eq $dir) {
> print "no match found!\n";
> last;
> }
> print "match found\n"
> }
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
|
|
|