Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to find 1-3 matches per line, no matter in which order the words are.

Line from flatfile:

FALCHE;12;ff;20020101;Pori;Yyteri;lietteet;testi;kylma

I have tried following code to search matches.

if (($hdata =~/$crit1/ig) && ($hdata =~/$crit2/ig) && ($hdata =~/$crit3/ig)) { print "$hdata<br>"; }

Problem is the order. If I search "pori and yyteri" I get fifferent resuts than with "yyteri and pori".

What should I do?

PH

Edited: ~Thu Aug 1 14:02:19 2002 (GMT) by footpad: Added <code> and other HTML formatting tags.

Replies are listed 'Best First'.
Re: Finding multiple words from a line of flatfile
by Bilbo (Pilgrim) on Aug 01, 2002 at 12:53 UTC
    The /g at the end of each regexp makes it remember where in the line it has reached. Each match starts from where the previous one ended. Get rid of them and it should do what you intended.
    #!/usr/bin/perl -w use strict; my $hdata = 'FALCHE;12;ff;20020101;Pori;Yyteri;lietteet;testi;kylma'; my $crit1 = "pori"; my $crit2 = "yyteri"; if (($hdata =~/$crit1/i) && ($hdata =~/$crit2/i)) {print "$hdata\n"}
Re: Finding multiple words from a line of flatfile
by Abigail-II (Bishop) on Aug 01, 2002 at 12:50 UTC
    Get rid of the /g.

    Abigail

Re: Finding multiple words from a line of flatfile
by Basilides (Friar) on Aug 01, 2002 at 15:47 UTC
    By the way, you say you want to find 1-3 matches per line, but the && signs in your regex mean that actually you are only succeeding if all three searches succeed.

    If you really want 1, 2 or 3 matches swap these for || signs:

    if (($hdata =~/$crit1/i) || ($hdata =~/$crit2/i) || ($hdata =~/$crit3/i)) { print "$hdata<br>"; }
      I really want to get 1 or more matches.I have a little trick for this. if($crit1 eq "") { $crit1="\;" # there is 7 ; in every line so it matches :) } Same with $crit2 and so on... Thank you all folks!