Thanks a lot for the advice, Michael; I now got the code to work and am 
adding it further below, just for the record.

--On Montag, 24. September 2001 09:07 -0800 Michael Fowler 
<[EMAIL PROTECTED]> wrote:

> my %hash = ('0' => 'miller', '1' => '32', '2' => 'copenhagen');
>
> If you're indexing your hash based on number then you should be using an
> array.

I didn't mean to index. There will be numbers used as hash keys which may 
or may not be consecutive.
So the hash could also look like this:
my %searchhash = ('2' => 'kowalski', '5' => 56', '10' => 'terranova');
The numbers indicate which position in the line the expression given as 
hash value should be on. I only want search results when ALL hash values 
match elements of the array (in this case: all three), not if the number of 
matches < 3.

The following code now does just that:

use strict;
my (@hits, $key);
my %searchhash = ('0' => 'miller', '1' => '32', '2' => 'copenhagen');
open (FILE, "<test.txt") or die ("can't open: $!");
     while (<FILE>) {
        chomp;
        my $found = 0;
        my @line = split /\|/, $_;
        foreach my $i (0 .. $#line) {
            if ($line[$i] eq $searchhash{$i}) { $found++; }
        #when an array element matches the hash value, increment found
        #otherwise, move on
            if ($found == scalar keys %searchhash) {
        #when there are as many matches as hash elements, grab the line, otherwise 
just move on
                push (@hits, \@line);}
        }
    }
close (FILE);

    foreach my $hit (@hits) {
        print "HIT: ", join("|", @$hit), "\n";
    }


Birgit Kellner


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to