You have various problems with your code, one of which was already mentioned
by Jos. I will mention it again, as well as point out the other problems.
On Sat, Sep 22, 2001 at 06:22:01PM +0200, Birgit Kellner wrote:
> use strict;
> my (@hits, $key);
> my %hash = ('0' => 'miller', '1' => '32', '2' => 'copenhagen');
If you're indexing your hash based on number then you should be using an
array. Also, don't name hashes %hash, or arrays @array, scalars $scalar,
etc.; use descriptive names.
> open (FILE, "<test.txt") or die ("can't open: $!");
Good, you checked your open.
> my $found = 0;
This variable is used for each line, but it's declared and initialized here.
As far as I understand your code, you should be declaring it within your
while loop.
> while (<FILE>) {
> my @line = split /\|/, <FILE>;
As Jos pointed out, you're reading from the file twice, discarding one of
the lines. Split on $_.
> foreach $key (keys %hash) {
> if ($line[$key] eq "$hash{$key}") {
> $found++;
> if ($found = scalar keys %hash) {
You just assigned 3 to $found; perhaps you meant ==?
> push (@hits, @line); last;
@hits just grew by three elements, not one. You probably wanted to
enreference @line, or perhaps push $_.
> }
> else {
> next;
This next is unnecessary and potentially confusing. At loop's end, it
naturally goes to the next iteration.
> }
> }#end if
> }#end foreach
> }#end while
> foreach my $hit (@hits) {print "HIT: $hit\n";}
Given your description of what you want, and how I'm reading your code's
intent, it'd be easier to restructure your code thusly:
@matching_line = qw(miller 32 copenhagen);
LINE: while (<FILE>) {
my @line = split("\|");
foreach my $i (0 .. $#matching_line) {
next LINE unless $line[$i] eq $matching_line[$i];
}
# If we've gotten here it means @line matches @matching_line.
push(@hits, \@line);
}
foreach my $hit (@hits) {
print "HIT: ", join("|", @$hit), "\n";
}
The code is untested.
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]