Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can you search a database for something that exists in it's value but the value is a joined string?

Let's say I have $help{$me) = "with::this::please";

And I want to to search the entire database for either "with" or "this" or "please"and pull back the key/value pair (which will ALWAYS have only one match, so we don't have to worry about pulling back more than one pair).

This confuses me beyond belief, can someone help?

Replies are listed 'Best First'.
Re: Searching a database
by Abigail-II (Bishop) on Sep 12, 2003 at 08:56 UTC
    You say database, but your example suggest that you mean hash. Let's assume you do indeed mean hash. So, you have a hash, and are looking for the key/value pair, where the value is one of "with", "this", "please". I'd do something like:
    my @matches = qw /with this please/; OUTER: while (my ($key, $value) = each %hash) { foreach my $match (@matches) { if ($match eq $value) { print "$key: $value"; last OUTER; } } }

    Or you could make a regex and match against the regex.

    Abigail

      That would work except what I'm searching for is a group of numbers but there will be a few sets of numbers in one value. Is there a way to iterate over each key/value in the hash, split them on :: then test existance?

      Thanks.

        my %matches = map {$_ => 1} 17, 83, 114, 205, 407; OUTER: while (my ($key, $value) = each %hash) { foreach my $num (split /::/ => $value) { if ($matches {$value}) { print "$key: $value\n"; last OUTER; } } }

        Abigail