Author Topic: Compare 2 strings & return a list of strings with the differences?  (Read 8428 times)

0 Members and 1 Guest are viewing this topic.

BazzaCAD

  • Guest
Does anyone have some code that will compare 2 strings & return a list of strings with the differences?
For example:
(setq a "This is a test")
(setq b "This isn't a test")
(StrCompare a b)
("This is" "n't" " a test")

OR

(setq a "123")
(setq b "1x3)
(StrCompare a b)
("1" "x" "3")

Or something similar to this?

uncoolperson

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #1 on: December 15, 2006, 12:57:27 PM »
hmm... i may have to take a break from actual work, this looks to be fun

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #2 on: December 15, 2006, 01:06:18 PM »
vl-string->list
mapcar
eq
cons
chr

...sorry, just thinking out loud.  Yeah, i think i'll participate as well after lunch.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #3 on: December 15, 2006, 01:24:59 PM »
This would be a tough one
Code: [Select]
(setq a "This is a X test")
(setq b "This isn't a test")
(StrCompare a b)
("This is" " n't" " a " "X"  " test")
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

BazzaCAD

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #4 on: December 15, 2006, 01:38:04 PM »
Thx for the help guys.
I was thinking of breaking the string up into a list of words separated by the blank space " ". Then compare each word, if they don't match then compare each letter to see what is different.
Or something like that...

(dos_strtokens "This is a test" " ")

uncoolperson

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #5 on: December 15, 2006, 04:41:27 PM »

it's not pretty, it's not perfect... but it's where i'm at so far


Code: [Select]
_$ (setq a "This is a X test")
(setq b "This isnt a test")
"This is a X test"
"This isnt a test"
_$ (compfun a b)
((T "test") (T "This is") (nil "nt") (T " a ") (nil "X "))


the nil and T are to point out if it represents something that is in both or just one.



Code: [Select]
(DEFUN compfun (string1 string2 / next retlist startmis tempstr swap backwards)
  (WHILE (AND (> (STRLEN string1) 0)
      (> (STRLEN string2) 0)
)
    (SETQ startmis (VL-STRING-MISMATCH string1 string2))
    (IF (> startmis 0)
      (SETQ retlist (APPEND (LIST
      (LIST T
    (IF backwards
      (VL-LIST->STRING
(REVERSE
  (VL-STRING->LIST (SUBSTR string1 1 startmis))
)
      )
      (SUBSTR string1 1 startmis)
    )
      )
    )
    retlist
    )
    string1 (SUBSTR string1 (+ 1 startmis))
    string2 (SUBSTR string2 (+ 1 startmis))
      )
    )
    (SETQ next (VL-STRING-SEARCH (SUBSTR string1 1 1) string2))
    (IF (> next 0)
      (SETQ retlist (APPEND (LIST
      (LIST f
    (IF backwards
      (VL-LIST->STRING
(REVERSE (VL-STRING->LIST (SUBSTR string2 1 next))
)
      )
      (SUBSTR string2 1 next)
    )
      )
    )
    retlist
    )
    string2 (SUBSTR string2 (+ next 1))
      )
      (IF swap
(SETQ tempstr string1
      string1 string2
      string2 tempstr
      swap    nil
)
(SETQ string1 (VL-LIST->STRING
  (REVERSE (VL-STRING->LIST string1))
)
      string2 (VL-LIST->STRING
  (REVERSE (VL-STRING->LIST string2))
)
      retlist (REVERSE retlist)
      backwards (= backwards nil)
      swap T
)
      )
    )
  )
  (VL-REMOVE '(nil "")
     (VL-REMOVE '(nil nil)
(APPEND (IF backwared
  (REVERSE retlist)
  retlist
)
(LIST (LIST nil
    (IF backwards
      (VL-LIST->STRING
(REVERSE (VL-STRING->LIST string1))
      )
      string1
    )
      )
      (LIST nil
    (IF backwards
      (VL-LIST->STRING
(REVERSE (VL-STRING->LIST string2))
      )
      string2
    )
      )
)
)
     )
  )
)

BazzaCAD

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #6 on: December 15, 2006, 05:45:08 PM »
WOW, I'm truly impressed, nice work...

This one works great.....
Code: [Select]
(setq a "this is a test")
"this is a test"
(setq b "this isn't a test")
"this isn't a test"
(compfun a b)
((T "this is") (nil "n't") (T " a test"))


But this one returns them out of order, can this be fixed?
Code: [Select]
(setq a "this is a X test")
"this is a X test"
(compfun a b)
((T "test") (T "this is") (nil "n't") (T " a ") (nil "X "))
« Last Edit: December 15, 2006, 05:46:24 PM by BazzaCAD »

BazzaCAD

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #7 on: December 15, 2006, 06:03:29 PM »
Also 1 letter words go into an loop..
(setq a "This is an X")
(setq b "This is an Y")
...

uncoolperson

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #8 on: December 15, 2006, 06:18:08 PM »
WOW, I'm truly impressed, nice work...

just wait till se7en does this in one line



here it is, the third arg is order mattering (i think it works like that...)

the fourth is to designate how many characters of accuracy are needed to be a 'match'

again i really do believe someone else here can make something neater, but this is my simple while i work shot.

Code: [Select]
_$ (setq a "This is an X")(setq b "This is an Y")(compfun a b nil 1)
"This is an X"
"This is an Y"
((T "This is an ") (nil "X") (nil "Y"))
_$ (setq b "this is a X test")(setq a "test this isn't")(compfun a b nil 1)
"this is a X test"
"test this isn't"
((T "est") (nil "his is a X t") (T "t") (nil " this isn't"))
_$ (setq b "this is a X test")(setq a "test this isn't")(compfun a b nil 2)
"this is a X test"
"test this isn't"
((T "test") (nil "this is a X ") (nil " this isn't"))
_$ (setq b "this is a X test")(setq a "test this isn't")(compfun a b t 2)
"this is a X test"
"test this isn't"
((nil "this is a X ") (T "test") (nil " this isn't"))
_$


Code: [Select]
(DEFUN compfun (string1 string2 ordermatters matchyness / next retlist startmis tempstr swap backwards)
  (WHILE (AND (> (STRLEN string1) matchyness)
      (> (STRLEN string2) matchyness)
)
    (SETQ startmis (VL-STRING-MISMATCH string1 string2))
    (IF (> startmis (1- matchyness))

      (SETQ retlist (APPEND (LIST
      (LIST T
    (IF backwards
      (VL-LIST->STRING
(REVERSE
  (VL-STRING->LIST (SUBSTR string1 1 startmis))
)
      )
      (SUBSTR string1 1 startmis)
    )
      )
    )
    retlist
    )
    string1 (SUBSTR string1 (+ 1 startmis))
    string2 (SUBSTR string2 (+ 1 startmis))
      )
    )

    (SETQ next (VL-STRING-SEARCH (SUBSTR string1 1 matchyness) string2))
    (IF (> next 0)
      (SETQ retlist (APPEND (LIST
      (LIST f
    (IF backwards
      (VL-LIST->STRING
(REVERSE (VL-STRING->LIST (SUBSTR string2 1 next))
)
      )
      (SUBSTR string2 1 next)
    )
      )
    )
    retlist
    )
    string2 (SUBSTR string2 (+ next 1))
      )
      (IF (or swap ordermatters)
(SETQ tempstr string1
      string1 string2
      string2 tempstr
      swap    nil
)
(SETQ string1 (VL-LIST->STRING
  (REVERSE (VL-STRING->LIST string1))
)
      string2 (VL-LIST->STRING
  (REVERSE (VL-STRING->LIST string2))
)
      retlist (REVERSE retlist)
      backwards (= backwards nil)
      swap T
)
      )
    )
  )
  (VL-REMOVE '(nil "")
     (VL-REMOVE '(nil nil)
(APPEND (IF (not backwared)
  (REVERSE retlist)
  retlist
)
(LIST (LIST nil
    (IF backwards
      (VL-LIST->STRING
(REVERSE (VL-STRING->LIST string1))
      )
      string1
    )
      )
      (LIST nil
    (IF backwards
      (VL-LIST->STRING
(REVERSE (VL-STRING->LIST string2))
      )
      string2
    )
      )
)
)
     )
  )
)

uncoolperson

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #9 on: December 15, 2006, 06:23:29 PM »
that one is even more prone to going bad...

sorry, maybe use it as a starting point (or an example of what not to do)

beer:30 pm!

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #10 on: December 15, 2006, 06:45:53 PM »
Yeah, that one turned out tougher then i thought. I got a small recursive procedure to work, but then i noticed a MAJOR flaw. (It would only work if the strings were in exact order.) *lol*

We'll have to play arround with this topic on monday.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BazzaCAD

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #11 on: December 15, 2006, 11:58:41 PM »
Actually if it makes things easier, I only need it to compare in 1 direction not both.
I.E. a --> b, not a <--> b

So:
(setq a "This is a X test")
(setq b "This isn't a test")
(StrCompare a b)

Would still be:
("This is" "n't" " a test")

Or anther way to think of it would be to only look for additions & changes, not for deletions...
So "n't" was added to the second string & the "X" was deleted...

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #12 on: December 16, 2006, 12:16:24 PM »
yeah, i got it but a one way compairison is still a lot of processing. Lets try and hash out some Pseudo code (Which is prolly really dangerous seeing as i havent even poured myself a cup of coffee yet.)

Code: [Select]
Compare two lists; pseudo code:
[]  given two lists.
o  pop off first item from each list.
o  take item 1 (i1) and compaire it with item 2 (i2)
~  if eq return i1
--  assemble or append new list
# Now this is where things get hairy. We can
# go in two diff. directions

# direction 1 (`Sofar' equality.)
# check existance of equality of list two with
# list one
# verified --e.g.
# ``This is a test''
# ``This isnt a test''
#        ^ Repeated chars `is' and the `is' from `isnt'
#          but since list one has this double char instance
#          as well it is equal sofar.
# This method requires us to cotinualy back trace and dep.
# on how far we back trace our process call doubled or trippled.
# This method would allow us to potentialy create a ``smarter''
# check. -e.g. (``isnt'' diff from ``is'') (no instace of ``X'')
# or the stuff you requested (+ of ``nt'' in list two)
# (- ``X'' in list 2)

# THIS IS SOME BIG CODE


# direction 2 (`Exact match')
# check for exact equality
# this method would result in a ``dumber'' return.
# ``This is a X test''
# ``This isnt a X test''
#          ^ Every thing after this point is diff.
#            so the rest of this string would be returned.
# However in this instace:
# ``This is a X test''
# ``This isnt a test''
#          ^^ ^ Are all differences and would be returned.
# This method requires a more exact string to get results you
# requested.

# THIS IS SIMPLE RECURSIVE CODE.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Joe Burke

  • Guest
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #13 on: December 17, 2006, 01:06:42 PM »
Personally I think once you cross-post the same question here and in the Autodesk customization NG, you don't deserve an answer. Because you are assuming your question is more important than the time and effort wasted in the process by others.

Frankly, I'm sick of seeing cross-posts. There no point reading here if most of what I see is a regurgitation of what I read elsewhere.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Compare 2 strings & return a list of strings with the differences?
« Reply #14 on: December 17, 2006, 06:02:48 PM »

There are only a couple of people who do it  Joe ... doesn't take long to learn which posts to ignore.

... but it's a pity we won't see you again :-(


 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.