Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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]
[Corion]: Oh, cool! Have fun Discipulus!
[Discipulus]: sure! probably the last chance to choice my holidays, 3 weeks

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2018-06-25 18:22 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.