Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching a hash 1

Status
Not open for further replies.

ailse

Programmer
Jan 20, 2004
79
0
0
GB
I have implemented a search of a hash using a keyword as follows:

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?
 
Print it outside while loop
Code:
my $flag = 0;
while ( my ($id, $description) = each(%hash) ) {
    if ($description =~ /$query_string/i) {
    $flag = 1 ;   
    print "$id" . "\t" . "$description";
    print "\n";
    }
}
if ( $flag == 0 ) {
print "No matching results!\n";
}

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
That seems to be a lot of work ...

can you force your hash to a particular case, eg lower

Code:
$qs=lc($query_string);
if (exists($hash($qs)) {
  print "Gotcha"
}

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Agreed. 19,000 regexp matches for each search just isn't practical. However, it's the values that the OP is searching rather than the keys, so that solution won't work, Paul.
 
gyaargh .... I need a holiday

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
heh, jump across the pool for the weekend. Tuesday next week, we celebrate the day we politely asked your countrymen (or was it your neighbors?) to go home. :)

uhh...can't post without content, so here's another way. I've always liked the list operators.
Code:
[COLOR=#0000FF]my[/color] $flag = [COLOR=#FF0000]0[/color];
[COLOR=#0000FF]for[/color] [COLOR=#0000FF]my[/color] $id ([COLOR=#FF0000]grep[/color] $hash{$_} =~ /$query_string/i, [COLOR=#FF0000]keys[/color] %hash) {
	$flag = [COLOR=#FF0000]1[/color];
	[COLOR=#FF0000]print[/color] [COLOR=#808080]"$id\t$hash{$id}\n"[/color];
}
[COLOR=#FF0000]print[/color] [COLOR=#808080]"No matching results!\n"[/color] [COLOR=#0000FF]unless[/color] $flag;

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
thx for the invite, at least the door's in now, so that should help with your headache/colour dilemma :p

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
a twist on icrf's code:

Code:
my @results = grep {/$query_string/i} values %hash;     
print "No matching results!\n" unless @results;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top