in reply to Searching a database

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

Replies are listed 'Best First'.
Re: Re: Searching a database
by Anonymous Monk on Sep 12, 2003 at 09:01 UTC
    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

        I've been doing Perl for about 5 years, and I'm *NO* expert, but I have to say that to me that is at least somewhat obfuscated code. I have no problem with the while, but the foreach:
        foreach my $num (split /::/ => $value) {
        mystifies me - what is the split splitting? When split isn't given an arg to split, it splits $_, but what is $_ in this case, since the while loop gets $key and $value?

        IMHO, more straightforward code, albeit sometimes longer code, is better:
        my %matches = map {$_ => 1} 17, 83, 114, 205, 407; while (my ($key, $value) = each %hash) { my @value_tokens = split /::/, $value; foreach my $value_token (@value_tokens) { if ($matches{$value_token}) { print "$key: $value\n"; $found = 1; last; ### break out of foreach } } ### end foreach if $found: next; } } ### end while
        I'm not entirely sure I understand the original intent, so this solution may need some work.

        HTH.