#Task: I'm reading in a database file line by line. Each line contains one
record, whose fields are separated by "|".
#I have a hash with several key/value-pairs.
#The key is the position of the field on the line, the value is the
expression
#that should be present in that position. I want to retrieve all lines in
the file which contain ALL
#values together, and it is possible that this is true for more than one
line.
#This is the code I have so far:
use strict;
my (@hits, $key);
my %hash = ('0' => 'miller', '1' => '32', '2' => 'copenhagen');
open (FILE, "<test.txt") or die ("can't open: $!");
my $found = 0;
while (<FILE>) {
my @line = split /\|/, <FILE>;
foreach $key (keys %hash) {
if ($line[$key] eq "$hash{$key}") {
$found++;
if ($found = scalar keys %hash) {
push (@hits, @line); last;
}
else {
next;
}
}#end if
}#end foreach
}#end while
foreach my $hit (@hits) {print "HIT: $hit\n";}
# the problem is that this works if test.txt reads:
#somestuff|15|copenhagen
#miller|32|copenhagen
#kellner|15|vienna
#miller|32|copenhagen
# With this file, I correctly get two hits back.
#However, it doesn't work for:
#miller|32|copenhagen
#somestuff|15|copenhagen
#kellner|15|vienna
#miller|32|copenhagen
# Neither does it work for:
#somestuff|15|copenhagen
#kellner|15|vienna
#miller|32|copenhagen
#miller|32|copenhagen
#For these two files, I only get one hit back. Any clues?
# Birgit Kellner
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]