Jump to content

String Comparison - Does NOT Equal But Should


Recommended Posts

Posted

I have a routine that reads two files and reports if there are differences.

 

The data is place in "out1" for file 1 and "out2" for file 2.

 

As a test, I have it read the same file. It says that they are not equal.

 

When I do this it reports a "YES!!"

 

 (if (= out1 out1)
    (princ "\nYES!!")
    (princ "\nno.")
 )

When I do this it reports a "no."

 

 (if (= out1 out2)
    (princ "\nYES!!")
    (princ "\nno.")
 )

Any thoughts as to what is going on?

 

Below is the complete routine:

 

(defun c:ctf (/ fil fl i mycount out1 out2 rec)
 ;; String to List  -  Lee Mac
 ;; Separates a string using a given delimiter
 ;; str - [str] String to process
 ;; del - [str] Delimiter by which to separate the string
 ;; Returns: [lst] List of strings
 (defun lm:str->lst (str del / pos)
   (if    (setq pos (vl-string-search del str))
     (cons (substr str 1 pos) (lm:str->lst (substr str (+ pos 1 (strlen del))) del))
     (list str)
   )
 )   ; End of lm:str->lst
 ; ===========================================================================================================
 ; Read File 1

 (setq fl nil)
 (setq fil nil)
 (if (setq fl (open "C:\\Users\\ggleason\\Documents\\CADWorx\\CADWorx.Projects\\Exxon.Joliet.2015-151\\IsoLayers\\ExxonMobil.Iso.Border.Acad.v.2007.dwg.txt" "r"))
   (progn 
      (setq mycount 0)
      (while (setq fil (read-line fl))
         (setq mycount (1+ mycount))
         ; SYNTAX: (princ "\nMy Name is \nJohn")(princ)
         ; (princ mycount)(princ "\n")
         (setq out1 (cons (lm:str->lst fil "\t") out1))
      )   ; End of "While" loop
      (close fl)
      (princ (strcat (itoa mycount) " records read in File 1\n"))
   )
 )   ; End of "If" statement for File 1
 ;; Reverse results to put them in correct order
 (setq out1 (reverse out1))
 ; End of File 1
 ; ===========================================================================================================
 ; Read File 2
 
 (setq fl nil)
 (setq fil nil)
 (if (setq fl (open "C:\\Users\\ggleason\\Documents\\CADWorx\\CADWorx.Projects\\Exxon.Joliet.2015-151\\IsoLayers\\ExxonMobil.Iso.Border.Acad.v.2007.dwg.txt" "r"))
   (progn 
      (setq mycount 0)
      (while (setq fil (read-line fl))
         (setq mycount (1+ mycount))
         ; SYNTAX: (princ "\nMy Name is \nJohn")(princ)
         ; (princ mycount)(princ "\n")
         (setq out2 (cons (lm:str->lst fil "\t") out2))
      )   ; End of "While" loop
      (close fl)
      (princ (strcat (itoa mycount) " records read in File 2\n"))
   )
 )   ; End of "If" statement for File 2
 ;; Reverse results to put them in correct order
 (setq out2 (reverse out2))
 ; End of File 2
 ; ===========================================================================================================
         ; SYNTAX: (if (= 1 3) "YES!!" "no.")
 (if (= out1 out2)
    (princ "\nYES!!")
    (princ "\nno.")
 )
 (princ "\n ------------------------------------------------")
 (princ "\n")
 (princ out1)
 (princ "\n ------------------------------------------------")
 (princ "\n")
 (princ out2)
 (princ "\n ------------------------------------------------")
(princ)
)                    ; End of c:ctf

Greg

Posted

Is there some reason why you are not comparing each line as read from the two files ? I did code on a test file and it came back No.

Posted

Per the documentation, the '=' function should be used for numerical or string comparison, not for list comparison - observe:

_$ (= '(1 2 3) '(1 2 3))
nil

Instead, you should use the equal function.

_$ (equal '("a" "b" "c") '("a" "b" "c"))
T

Posted

Aside of Lee's most obvious suggestion (using the equal function), heres an alternative if one deals only with list of strings:

 

(vl-every '= '("a" "b" "c") '("a" "b" "c") '("a" "b" "c"))

 

hence:

 

(vl-every '= ListOfStrings1 ListOfStrings2 ListOfStrings3 ...)

 

and even:

 

(apply 'vl-every (cons '= ListOfStringLists))

 

Although I'd use equal myself, since it works with nested lists.

Posted
Is there some reason why you are not comparing each line as read from the two files ? I did code on a test file and it came back No.

 

I would have like to have done that, but I was not sure how to go about it.

 

Greg

Posted
Per the documentation, the '=' function should be used for numerical or string comparison, not for list comparison - observe:

_$ (= '(1 2 3) '(1 2 3))
nil

Instead, you should use the equal function.

_$ (equal '("a" "b" "c") '("a" "b" "c"))
T

 

Thank you for clearing that up.

 

Also, I appreciate all of the great information you put out there for us to learn from.

 

Greg

Posted
Aside of Lee's most obvious suggestion (using the equal function), heres an alternative if one deals only with list of strings:

 

(vl-every '= '("a" "b" "c") '("a" "b" "c") '("a" "b" "c"))

 

hence:

 

(vl-every '= ListOfStrings1 ListOfStrings2 ListOfStrings3 ...)

 

and even:

 

(apply 'vl-every (cons '= ListOfStringLists))

 

Although I'd use equal myself, since it works with nested lists.

 

That is great information. Thank you.

 

Greg

Posted
Aside of Lee's most obvious suggestion (using the equal function), heres an alternative if one deals only with list of strings:

(vl-every '= '("a" "b" "c") '("a" "b" "c") '("a" "b" "c"))

There is a major difference between vl-every '= and equal

_$ (equal '("a" "b") '("a" "b" "c")) -> nil

_$ (vl-every '= '("a" "b") '("a" "b" "c")) -> T
Posted
There is a major difference between vl-every '= and equal

_$ (equal '("a" "b") '("a" "b" "c")) -> nil

_$ (vl-every '= '("a" "b") '("a" "b" "c")) -> T

 

Thanks for pointing this out, Stefan!

I've forgot that vl-every processes like mapcar.

So that suggestion should be used in conjunction with equal length check of the lists.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...