I have implemented a search of a hash using a keyword as follows:
my problem is to do with returning an error message if $query_string does not appear in the hash. if i modify the code as follows:
the error message "no matching results" prints for every record in the hash, when i just want it to print once. so if my hash has four records (just as an example, i actually have over 19,000):
id1 description
id2 description
id3 description
id4 description
and $query_string does not appear, the above code is returning:
No matching results!
No matching results!
No matching results!
No matching results!
when it should only return this message once - any ideas how to work around this?
Code:
while ( my ($id, $description) = each(%hash) ) {
if ($description =~ /$query_string/i) {
print "$id" . "\t" . "$description";
print "\n";
}
}
my problem is to do with returning an error message if $query_string does not appear in the hash. if i modify the code as follows:
Code:
while ( my ($id, $description) = each(%hash) ) {
if ($description =~ /$query_string/i) {
print "$id" . "\t" . "$description";
print "\n";
}
else {
print "No matching results!\n";
}
}
the error message "no matching results" prints for every record in the hash, when i just want it to print once. so if my hash has four records (just as an example, i actually have over 19,000):
id1 description
id2 description
id3 description
id4 description
and $query_string does not appear, the above code is returning:
No matching results!
No matching results!
No matching results!
No matching results!
when it should only return this message once - any ideas how to work around this?