Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to campare two variable consist line and print difference

by Mjpaddy (Acolyte)
on Sep 15, 2014 at 06:00 UTC ( [id://1100557]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks

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.

Please Help!

Thanks in advance

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

    A good strategy would be to show us what you have tried and tell us how it failed.

    Your description is not altogether clear nor consistent. Your last significant paragraph is talking about files whereas the rest of your node talks about comparing individual lines. It may help if you tell us why you need to do this. Solutions that work well for some problems will fail horribly if used for a different class of problem.

    Perl is the programming world's equivalent of English
Re: How to campare two variable consist line and print difference
by soonix (Canon) 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
    Hmm, it is a bit difficult to state the position of an unmatched text pattern... More generally, the idea should probably be to use a regex to check if you can match "doesn't" or "don't", but you don't give enough details on the circumstances. May be something like:
    if ($string =~ /doesn't/ or $string =~ /don't/) { do_something(); } else { something_else(); }

      Consider:

      my $str1 = "The device doesn't allow writes and that's causing this is +sue."; my $str2 = "The device allow writes and that's causing this issue."; my $str3 = $str1 ^ $str2; print "Difference found at $+[0]\n" if $str3 =~ /^(\0+)/;
      Perl is the programming world's equivalent of English

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100557]
Approved by GrandFather
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-20 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found