Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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++; } } } } }

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998174]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-01-16 12:05 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (177 votes). Check out past polls.