Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: help with comparing two arrays of phrases

by Neighbour (Friar)
on Oct 10, 2012 at 08:47 UTC ( #998174=note: print w/ replies, xml ) Need Help??


in reply to help with comparing two arrays of phrases

The reason you're getting no results is because your comparison is the wrong way around :). You are checking to see if the word matches the regex with the entire 2nd phrase instead of the other way around.
I've taken the liberty of changing a few other things (sorry, couldn't resist). Amongst other things, the changes are in

  • how you load the file contents
  • how you remove special characters (and when)
  • how you loop through the words
  • how you keep track of the match_count
  • and how you perform the match

use strict; use warnings; use v5.10; my @data = <>; my $match_count = 1; foreach my $line (@data) { chomp ($line); print "Processing line [$line]\n"; $line =~ s/[",\/-]/ /g; # Change all potential word endings to +a single space $line =~ s/[()]//g; # Remove parentheses to avoid mishaps d +uring pattern matching my ($id, $source, $comparison) = split "\t", $line; # Split col +umns into an array foreach my $word (split ' ', $source) { given (length $word) { when ($_ < 3) { next; } when ($_ < 5) { if ($comparison =~ /$word/i) { print "Match [$match_count] (probable): [$word]\n" +; $match_count++; } } default { if ($comparison =~ /$word/i) { print "Match [$match_count]: [$word]\n"; $match_count++; } } } } }


Comment on Re: help with comparing two arrays of phrases
Download Code
Re^2: help with comparing two arrays of phrases
by sdtej (Initiate) on Oct 18, 2012 at 07:40 UTC

    Sorry for the late reply. Could not get online for a few days!!

    @Neighbor: Thanks for cleaning the code! This does look way better than mine, and will use your code for subsequent purposes :)

    @BrowserUk: Yes, my code does look confusing being the novice I am. However, I will keep those points you mentioned in mind from now on. Thanks a lot for all your suggestions and corrections!! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998174]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-08-30 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls