Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

difference between two lines

by vinoth.ree (Monsignor)
on Jul 30, 2018 at 08:13 UTC ( [id://1219457]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Is there a way to find difference between two lines are only the blank space? The space character can be anywhere in the line.


All is well. I learn by answering your questions...

Replies are listed 'Best First'.
Re: difference between two lines
by Corion (Patriarch) on Jul 30, 2018 at 08:18 UTC

    What have you tried and how did your code fail?

    Some ideas would be:

    You could remove all spaces from both lines. If they are equal, the difference was only the blank space.

    If you want to know if two lines only differ by exactly one blank space, check first, if the absolute difference in their lengths is 1. Then do the above check.

    Alternatively, split both strings on spaces and check if the resulting lists are the same.

      Hi Corion

      I tried the first option(remove all spaces from both lines and compare). I thought there could be best way, that's why posted this question.


      All is well. I learn by answering your questions...

        So the first option worked? If so, there's no need for a "best way". If your code has some issues you'll have to be more specific, rather than hope someone provides a solution actually fits your needs without knowing them. Or if you want advice about your code, you'll have to post it.

        Edited: in hindsight my previous formulation sounded harsher than intended.

Re: difference between two lines
by afoken (Chancellor) on Jul 30, 2018 at 10:36 UTC

    diff -w (or -b, -Z, -E):

    (Note: -Z / --ignore-trailing-space seems to be a relative new option)

    /tmp>( echo ' Hello World !' ; echo 'blah' ) > 1.txt /tmp>( echo 'Hello World ! ' ; echo 'foo' ) > 2.txt /tmp>diff 1.txt 2.txt 1,2c1,2 < Hello World ! < blah --- > Hello World ! > foo /tmp>diff -w 1.txt 2.txt 2c2 < blah --- > foo /tmp>

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: difference between two lines
by thanos1983 (Parson) on Jul 30, 2018 at 09:54 UTC

    Hello vinoth.ree,

    You can try to print both strings and see what kind of special characters they contain. You can also compare the special characters output so you will know at the end if they are same or not.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; my $newLineChar = "\n"; my $tabChar = "\t"; my $carriageReturnChar = "\r"; my $formfeedCharacter = "\f"; my $backspaceCharacter = "\b"; my $bellOrBeepCharacter = "\a"; my $escapeCharacter = "\e"; $Data::Dumper::Terse = 1; $Data::Dumper::Useqq = 1; my $testString = join "", $newLineChar, $tabChar, $carriageReturnChar, $formfeedCharacter, $backspaceCharacter, $bellOrBeepCharacter, $escapeCharacter; say $testString; say Data::Dumper::qquote($testString);

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found