in reply to Pattern Match is not working
Well, you half listened to the replies you got to your first node, but you could have done better. You've not supplied any data. You've not shown us what you expect as output. You've not supplied a stand alone script we can run to check your results and that we can correct to verify the errors we might see. You've not even shown us what you do get as output.
However, given you've made a partial effort, I'll provide a partial answer. First off: always use strictures, especially when they tell you things you don't understand! Your C is showing through! Perl does not use 'break' and 'continue', it uses 'last', 'next' and 'redo'. The following rework of your code at least runs, although it may not yet do what you want:
#!/usr/bin/perl use strict; use warnings; my $names = <<NAMES; N 2 N 3 N 4 N 5 OR NAMES my $data = <<DATA; Notification 2 Notification 1 Order DATA open my $in1, '<', \$names or die "Can't open names: $!\n"; open my $in2, '<', \$data or die "Can't open data: $!\n"; my @leapStrings = <$in1>; chomp @leapStrings; close $in1; my @vldbStrings = <$in2>; chomp @vldbStrings; close $in2; for my $leapString (@leapStrings) { print $leapString; $leapString =~ s/^\s+|\s+$//g; my $found; for my $vldbString (@vldbStrings) { $vldbString =~ s/^\s+|\s+$//g; $vldbString =~ s/Notification\s+(\d+)$/N $1/g; $vldbString =~ s/Order/OR/g; if ($leapString =~ m/$vldbString/i) { $found = 1; last; } } if ($found) { print "\t FOUND IN VLDB =====\n"; } else { print "\t NOT FOUND IN VLDB =====\n"; } }
Prints:
N 2 FOUND IN VLDB ===== N 3 NOT FOUND IN VLDB ===== N 4 NOT FOUND IN VLDB ===== N 5 NOT FOUND IN VLDB ===== OR FOUND IN VLDB =====
I had to invent data of course because you gave none. I also changed your opens to the three parameter version which is safer and clearer (although I did a sneaky "use a string as a file trick" which may be cause for confusion). I used lexical file handles which are also safer. These are both techniques you should always use!
There are a bunch of things I'd improve in this script which would make it work better with large files, but as you don't indicate how much data you are dealing with I'll leave that for a later date.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pattern Match is not working
by Lancy (Initiate) on Sep 07, 2011 at 12:38 UTC | |
|