Why the if condition can not be run?

Why the if condition can not be run?

skchui6159
Advocate Advocate
521 Views
8 Replies
Message 1 of 9

Why the if condition can not be run?

skchui6159
Advocate
Advocate

(vl-load-com)
(setq links "C:/sdsd/sdff/ppp")
(if (=(vl-string-search "//" links) F)
(vl-string-subst "//" "/" links)
)
(print links)

 

I want to subst all the "//" to "/". The condition of "if" is satisfactory, why nothing happen? Anyone help?

0 Likes
Accepted solutions (3)
522 Views
8 Replies
Replies (8)
Message 2 of 9

emilio24DRY
Enthusiast
Enthusiast

Is this within a lisp routine that you have created? If so, it might be beneficial to see all the code for reference.

0 Likes
Message 3 of 9

Kent1Cooper
Consultant
Consultant

What's F?

Kent Cooper, AIA
0 Likes
Message 4 of 9

BeKirra
Advisor
Advisor

@Kent1Cooper 

Guess OP means "F = false".

If so, here is an approach...

 

@skchui6159 wrote:

...
(if (=(vl-string-search "//" links) F)
...
)
(print links)

 

I want to subst all the "//" to "/". The condition of "if" is satisfactory, why nothing happen? Anyone help?


 

Try this:

(if (not (vl-string-search "//" links)

...

)

 

Hi @skchui6159 

AS @emilio24DRY mentioned, you might need to show us the entire code.

I suspect you also need to specify what will be happen if (vl-string-search "//" links) is true

 

HTH

 

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
0 Likes
Message 5 of 9

Kent1Cooper
Consultant
Consultant

[Missing a right parenthesis]

(if (not (vl-string-search "//" links))

Kent Cooper, AIA
0 Likes
Message 6 of 9

paullimapa
Mentor
Mentor
Accepted solution

If you want to replace all instances of "/" with "//" in string "C:/sdsd/sdff/ppp" I'd use this routine:

; aec_replace function to find & replace multiple instances
; https://www.cadtutor.net/forum/topic/68245-find-and-replace-multyple-letters/
 (defun aec_replace (new old string / pos)
  (while (setq pos (vl-string-search old string pos))
   (setq string (vl-string-subst new old string pos)
         pos    (+ pos (strlen new))
   ) ; setq
  ) ; while
  string
 ) ; defun

then:

(setq links "C:/sdsd/sdff/ppp")
(setq newlinks (aec_replace "//" "/" links))

!newlinks would return: 

"C://sdsd//sdff//ppp"

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 9

pbejse
Mentor
Mentor
Accepted solution

@skchui6159 wrote:

(vl-load-com)
(setq links "C:/sdsd/sdff/ppp")
(if (=(vl-string-search "//" links) F)
(vl-string-subst "//" "/" links)
)
(print links)

 

I want to subst all the "//" to "/". The condition of "if" is satisfactory, why nothing happen? Anyone help?


This return value for this line is an integer, so ensure that variable F is an integer, otherwise the condition will never evaluate to True

(vl-string-search "//" links)

If links  variable value is from a path, the line above will always be 2, unless you include a starting position for vl-string-search 

 

Also this line will never move forward, as only the first instance will change when used on your example

(vl-string-subst "//" "/" links)

You should also use vl-string-subst with a start-position argument or jsut simple using while function

 

Putting them all together

(setq links "C:/sdsd/sdff/ppp")
  (while (vl-string-search "/" links)
	(setq links (vl-string-subst "\\" "/" links))
    )

or via subfunction

(defun backtoforwardslash (links)
  (while (vl-string-search "/" links)
	(setq links (vl-string-subst "\\" "/" links))
    )
  links
)
_$ (backtoforwardslash  "C:/sdsd/sdff/ppp")
"C:\\sdsd\\sdff\\ppp"

HTH

 

 

 

0 Likes
Message 8 of 9

pbejse
Mentor
Mentor
Accepted solution

Also

(defun SingleToDouble (links)
  ((lambda (p)
     (while (Setq p (vl-string-search "/" links (+ p 2)))
       (setq links (vl-string-subst "//" "/" links p))
     )
     links
   )
    0
  )
)
_$ (SingleToDouble "C:/sdsd/sdff/ppp")
"C://sdsd//sdff//ppp"
_$ 

HTH

 

0 Likes
Message 9 of 9

komondormrex
Mentor
Mentor

@skchui6159 wrote:

(setq links "C:/sdsd/sdff/ppp")


you do not need to find and replace double slash in the links, cause that string goes fine for file paths which is strings in autolisp.

 

Command: (findfile "C:/Program Files/Autodesk/AutoCAD 2022/acad.exe")
"C:\\Program Files\\Autodesk\\AutoCAD 2022\\acad.exe"

 

but backslash is a different story. it won't work singularly in pathing because it is special character in autolisp language and in order to get it as it is you must double it. see return of findfile above.