Mjpaddy has asked for the wisdom of the Perl Monks concerning the following question:
How to compare two variables which has values a line and print the specific unmatched word by its position and line number
for example
$str1 = "But I think the device doesn't allow writes or something and +that's causing this issue."; $str2 = "But I think the device allow writes or something and that's c +ausing this issue.";
open (FHH, ">error.txt")|| die $!; $str1 = "But I think the device doesn't allow writes or something and +that's causing this issue."; $str2 = "But I think the device allow writes or something and that's c +ausing this issue."; my $line = 0; my $pat = qr/$str2/; while($str1){ $line++; if($str1 =~m/($pat)/igs){ my $pre = $`; } else{ print FHH "Line $line:$pre \'$pat\' is missing from $str2." } }
In the $str2 dont have "doesn't" as compare to $str1
So the output will be in other file called error.txt in this format: Line 1:cols 24 'doesn't' is missing from $str2
I have tried some modules in perl like: File::Compare, Text::Compare, List::Compare but they are not giving the proper answer.
Which strategy should I choose to get multiple lines store in two variables like a two whole file is store in two different variable and compare 2nd file on 1st and print position of mismatch in other file.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to campare two variable consist line and print difference
by GrandFather (Saint) on Sep 15, 2014 at 06:13 UTC | |
|
Re: How to campare two variable consist line and print difference
by soonix (Chancellor) on Sep 15, 2014 at 08:45 UTC | |
|
Re: How to campare two variable consist line and print difference
by Laurent_R (Canon) on Sep 15, 2014 at 06:19 UTC | |
by GrandFather (Saint) on Sep 15, 2014 at 09:19 UTC |