Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

returning matched word from string

by monaghan (Novice)
on Jul 22, 2008 at 23:36 UTC ( [id://699458]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to compare a string to a file that has a list of words in it. I added the word CPLD to my string which is the third word in the txt file. What I am trying to do is return whatever word may match the text file but the following script isn't doing that:
#!/usr/bin/perl use strict; my $teststring = "Hello CPLD this is just a test that I have created n +ow.\n"; print $teststring; my $file = "./acrolist.txt"; open (FILE,$file) || die "Can't open file \"$file\".\n"; my @raw_data=<FILE>; my $rawdata; my @newrawdata = (); my $newrawdata; print @raw_data; foreach $rawdata(@raw_data) { if ($teststring =~ m/$rawdata/) { print "You have a MATCH!\n"; #print $1; $newrawdata[@newrawdata] = $rawdata; } } print @newrawdata;

Replies are listed 'Best First'.
Re: returning matched word from string
by BrowserUk (Patriarch) on Jul 22, 2008 at 23:55 UTC
Re: returning matched word from string
by ww (Archbishop) on Jul 23, 2008 at 00:08 UTC

    Can you explain a little more, please? In particular (and maybe only because it's been a long day), I don't understand:

    I added the word CPLD to my string which is the third word in the txt file.

    update for clarity: As "CPLD" is the second word in $teststring, I'm confused.

    Based on a WAG, however, please note that for a match, your $rawdata will have to contain the complete sentence in $teststring.

    So please post a reasonably small sample of your acrolist.txt.

    Your script, minus one of the redundant declarations of my $newrawdata compiles and runs for me when run against a text file:

    Hello CPLD this is just a test that I have created now. Bye, CPLD! This string does not have the magic accronym.

    Compilation and execution produces the expected output, to wit,

    pl_test$ perl -c 699458.pl 699458.pl syntax OK ~/pl_test$ perl 699458.pl Hello CPLD this is just a test that I have created now. Hello CPLD this is just a test that I have created now. Bye, CPLD! This string does now have the magic accronym. You have a MATCH! Hello CPLD this is just a test that I have created now.
Re: returning matched word from string
by Lawliet (Curate) on Jul 22, 2008 at 23:58 UTC
    open (FILE, 'acrolist.txt'); my @lines = <FILE>; my @data; foreach my $line (@lines) { if ( $line =~ /$teststring/ ) { print "You have a MATCH: $1\n"; push @data, $line; } } print @data;

    Is this what you are trying to do?

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
      A few comments:
      • You need to use capturing parentheses in your regular expression, /($teststring)/, if you want to use $1
      • use warnings;
      • Check the status of your open
      • Consider using a lexical filehandle and the 3-argument form of open: open my $fh, '<', 'acrolist.txt'
      • close the file

        You're absolutely correct. All I did was copy-paste the code from one of my scripts (explaining the first, second, and last bullet) and change a few details. The other two points are more laziness of mine.

        I'll update my code appropriately. Thanks for the comments.

        <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
      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
        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"); } } }

        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.

        <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: returning matched word from string
by leocharre (Priest) on Jul 23, 2008 at 02:12 UTC
    The following may be of interest to you.
    • String::Similarity, lets you measure the similarity between two strings.
    • If you use a shell, you can quickly search for text inside files. .
      unixy> find ~/ -type f | xargs grep 'size'
      unixy> find ~/ -type f | xargs grep 'size'
      Which can be rewritten as: grep -r 'size' ~

      --
      altblue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699458]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-07-24 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.