Hi Robert,
Thanks!
This wasn't the clue that solved my problem, but it works now.
I had the third if-statment placed in a different routine uniq(). Never thought of it
to place
it within the while loop. which was necesary (of course !!).
This is how it looks right now :
....
...
find(\&files, $directory);
# ---
sub files {
$file = $File::Find::name || shift;
next if($file =~ "ref.txt");
if ( $file =~ /\.txt$/ ) {
open (FILE, $file);
while (<FILE>) {
$_ = lc ;
$found{$file}{1} = 0;
$found{$file}{2} = 0;
if ($_ =~ /$word/i) {
++$found{$file}{1};
}
if ($_ =~ /$second/i) {
++$found{$file}{2};
}
if ( ($found{$file}{1} >= 1) && ($found{$file}{2} >= 1) ) {
push(@matches, $file);
}
} # End while
}
} # end sub files
Thanks a lot for your help !!
Regs David
------------------------
>
> It sounds like either use strict or use warnings (-w on the shebang line) is causing
>the error. If you add:
>
> $found{$file}{1} = 0;
> $found{$file}{2} = 0;
>
> before the two if's, that should suppress the error message (hopefully).
>
> What I think was happening is that one of the strings never matched, and
>therefore either the {1} or the {2} did not get incremented at all. When the
>greater-than-or-equal ( >= ) tried to compare a variable that did not exsist, the
>'strict' or 'warning' gave an error. The fix above initializes each variable to zero,
>so hopefully this will satisfy 'strict' or 'warnings'.
>
> The gist of the last logic is to check each counter and if both are positive,
>we have a match (don't we?). So, we compare the first counter, $found{$file}{1} and
>check if it's >= to 1: ($found{$file}{1} >= 1). We do the same thing with the second
>counter: ($found{$file}{2} >= 1). Since you were looking for a match only if both
>strings were found, we want to make sure test1 and test2 above are true in our if
>statement: if ( (test1) && (test2) ) { code }. If both are true, we want to record
>the $file that matched, so we push the file name onto the @matches array:
>push(@matches, $file).
>
> Hope this helps,
>
> =-= Robert T.
>
>
> On Wed, Jun 26, 2002 at 02:07:39PM +0200, David vd Geer Inhuur tbv IPlib wrote:
> >
> > Hi Robert,
> >
> > Looks good, but doens't work :)
> >
> > The if-statements work fine within the while-loop.
> > After that you do something I realy don't get and my perl either :)
> >
> > It start complaining :
> > Use of uninitialized value in numeric ge (>=) at /user/web/apache/cgi-bin/IPlib/1
>line 88, <FILE> line 471
> >
> > Any idea, what goes wrong ??
> > Maybe you can give me a litlle more explenation ?? Hope you have the time for it.
> > B.T.W. @matches is completely empty.
> >
> > Regs David
> >
> > # -----------------------
> >
> >
> > >
> > > > So in short :
> > > > I want to match 2 words within .txt documents, if the document contains BOTH
>words I'dd like to print it.
> > >
> > > I am assuming you mean strings, whereas a word would be surrounded by space
>ala: /\s$word\s/. To rephrase what you want a little, you want to track how many
>times string one is found in a file, track how many times string two is found in the
>same file, and keep the file only if both strings are found in the file.
> > >
> > > while (<INFILE>) {
> > > if ($_ =~ /$string1/i) {
> > > ++$found{$file}{1};
> > > }
> > >
> > > if ($_ =~ /$string2/i) {
> > > ++$found{$file}{2};
> > > }
> > > }
> > >
> > > if ( ($found{$file}{1} >= 1) && ($found{$file}{2} >= 1) ) {
> > > push(@matches, $file);
> > > }
> > >
> > > Each filename must be unique, since they are used as the keys for the hash.
> > >
> > >
> > > =-= Robert T.
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]