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

Hi Monks I am new to Perl below is my script for comparing two text files and printing the difference. pattern matching is working for one occurrence. Could you please guide me where i am going wrong. Thanks for your kind help.

#!/usr/bin/perl #use strict; use warnings; open(FILE,'>C:\\Aolvldbcomp\\Difference.txt'); my ($names, $data) =('LEAP_O.txt','VLDB_O.txt'); open (FILE1,$names) || die; open (FILE2,$data) || die; my @leapStrings = <FILE1>; chomp (@leapStrings); close(FILE1); my @vldbStrings = <FILE2>; chomp (@vldbStrings); close(FILE2); for my $leapString (@leapStrings){ print FILE "$leapString"; $leapString =~ s/^\s+//; $leapString =~ s/\s+$//; my $found = 0; for my $vldbString (@vldbStrings){ $vldbString =~ s/^\s+//; $vldbString =~ s/\s+$//; $vldbString =~ s/Notification\s+(\d+)\s*$/N $1/g; $vldbString =~ s/Order/OR/g; if (($leapString) =~ m/$vldbString/i) { $found = 1; break; } } if ($found) { print FILE "\t FOUND IN VLDB =====\n"; } else { print FILE "\t NOT FOUND IN VLDB =====\n"; } } close(FILE);

Replies are listed 'Best First'.
Re: Pattern Match is not working
by GrandFather (Saint) on Sep 07, 2011 at 11:14 UTC

    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.

    True laziness is hard work

      Hi i am sorry.i am dealing with data of a-z series,some series contain more than 500 lines. below given my sample input from o series. my requirement is need to print the difference at the same time for some criteria need to ignore. Example

      COMMITED PLACES ORDER, Map. 213, Order 1 ------> VLDB_O.txt Commited Places Order, Map. 213, OR 1 ------> LEAP_O.txt COMMITMENT OF COMPETENT AUTHORITY, Map. 213, Notification 1 + ------> VLDB_O.txt Commitment of Competent Authority, Map. 213, N 1 -->LEAP_O.txt + Thanks for your kind help. VLDB_O.txt JOINTS AND COMMITMENT JOINTS AND cOMMITMENT (PRESCRIBED FORMAT) REGULATIONS, Map. 21 +1, Regulation 1 PRACTICAL ORDER COMMITED PLACES ORDER, Map. 213, Order 1 COMMITMENT OF COMPETENT AUTHORITY, Map. 213, Notification 1 COMMITED OF COMPETENT AUTHORITY, S 417/1998 COMMITED PLACES ORDER 1997, S 276/2000 JOINTS AND OPTICIANS JOINTS AND OPTICIANS (REGISTRATION AND PRACTISING CERTIFICATES) R +EGULATIONS, Map. 213A, Regulation 1 JOINTS AND OPTICIANS (INVESTIGATION OF COMPLAINTS) REGULATIONS, + Map. 213A, Regulation 2 JOINTS AND OPTICIANS (PRACTICE, CONDUCT, ETHICS AND PUBLICITY) RE +GULATIONS, Map. 213A, Regulation 3 JOINTS AND OPTICIANS (EXEMPTION FROM SECTION 25(1)) ORDER, Map +. 213A, Order 1 JOINTS AND OPTICIANS (COMMENCEMENT) NOTIFICATION 2008, S 89/2 +008 LEAP_O.txt Joints and Commitment Practical Order Commitment of Competent Authority, Cap. 213, N 1 Commited Places Order, Cap. 213, OR 1 Prohibited Places Order 1997, S 276/2000 committed Places Order 2001, S 538/2010 Joints and Opticians Joints and Opticians (Exemption from Section 25(1)) Order, Map +. 213A, OR 1 Joints and Opticians (Exemption from Section 25(1)) Order 2008, + S 91/2008 Joints and Opticians (Investigation of Complaints) Regulations, + Map. 213A, RG 2 Joints and Opticians (Practice, Conduct, Ethics and Publicity) Re +gulations, Map. 213A, RG 3 Joints and Opticians (Registration and Practising Certificates) R +egulations, Map. 213A, RG 1 Joints and Opticians (Registration and Practising Certificates) R +egulations 2008, S 90/2008 Joints and Opticians (Registration and Practising Certificates) R +egulations 2008, S 90/2008 Joints and Opticians (Commencement) Notification 2008, S 89/20 +08 Joints and Opticians (Commencement) Notification 2008, S 89/20 +08
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Pattern Match is not working
by ww (Archbishop) on Sep 07, 2011 at 11:04 UTC
    Thank you for taking the trouble to use code tags, this time.

    Now, perhaps, you'll continue your evolution toward one who posts questions which make it easy for us to help.

    • "pattern matching is working for one occurrence."
      Which one? Is there an error message? If so, post it, verbatim. You need to specify your problem (and that may be something in your data, so you should post a small sample of that, too.
    • "Could you please guide me where i am going wrong."
      Aside from the fact that that sentence should end with a question mark, it misses the mark as an SOPW question because you don't tell us what is "going wrong" which leaves us to guess. At best, that's an inefficient way for you to learn.

    As noted in a previous reply (in another thread), please plainly mark any edits/updates you do.

Re: Pattern Match is not working
by Perlbotics (Archbishop) on Sep 07, 2011 at 11:01 UTC

    Did you really run this program? I get (5.14.1):

    Unquoted string "break" may clash with future reserved word at zut lin +e 33. Useless use of a constant (break) in void context at zut line 33. zut syntax OK
    break is part of given/when. The Perl equivalent to C's break would be last, but you probably meant next (C's continue)?

    if (($leapString) =~ m/$vldbString/i) { $found = 1; break; # <-- here, the equivalent to 'break' would be 'last'; # but you probably meant 'next;' ? }

Re: Pattern Match is not working
by RichardK (Parson) on Sep 07, 2011 at 10:58 UTC

    Well, that's a bit difficult as you didn't tell us what's going wrong :(

    But there's one thing that leaps out at me, you need to use last to break out of a loop

    try giving us an example of what's happening & what you expect

      Hi My expectation is In leap some line ends with OR followed by a number same line in vldb +ends with Order followed by a number. similar to this i have more. <code> $vldbString =~ s/Order/OR/g;
      should apply in whole file wherever Order is there. what i am getting is only in the first occurrence.(first line) ignoring in next occurrence sample output

      1)system Places Order, map. 213, OR 1 FOUND IN VLDB ==

      2)option and Opticians (option from Section 25(1)) Order, map. 213A, OR 1 NOT FOUND IN VLDB ==

      My expectation is second one also should found in vldb. Thanks.