in reply to Re: returning matched word from string
in thread returning matched word from string

hmmm, that looks really close to what I want but $line isn't matching anything in $teststring. acrolist.txt looks like: firstword secondword CPLD lastword
  • Comment on Re^2: returning matched word from string

Replies are listed 'Best First'.
Re^3: returning matched word from string
by ikegami (Patriarch) on Jul 23, 2008 at 01:01 UTC
    So you want to match the string if any of the words on the line match? Did you want to know if "each word matched" or if "any word matched"?
    # Any word matched while (<$words_fh>) { my $re = join '|', map quotemeta, split; if ( $teststring =~ /($re)/ ) { print("$teststring contains $1\n"); } }
    # Each word matched while (<$words_fh>) { for my $word ( split ) { if ( $teststring =~ /\Q$word/ ) { print("$teststring contains $word\n"); } } }
      That first method did the trick! Thank you very very much for your help everyone. This was my first time on PerlMonks and it was fast and easy. I think now I need to convert this array of acronyms to a hash.
Re^3: returning matched word from string
by Lawliet (Curate) on Jul 23, 2008 at 00:46 UTC

    Oh, I see. You are simply comparing the two strings. I am positive there is a more efficient way to do this, but for a makeshift fix you could put each word of $teststring into an array. Then, foreach element, test to see if it matches $line in the aforementioned array.

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>