Substr

Substr

andreas7ZYXQ
Advocate Advocate
863 Views
4 Replies
Message 1 of 5

Substr

andreas7ZYXQ
Advocate
Advocate

I have a issue id like to solve.

In my substr i want to replace 16 with with pos..
i dont know how to get around it and make it work..

thanks.

 

 

(defun c:001 ()
 (setq plan "1");
 (setq pos "2")
 (setq name (getvar "dwgname"))
 (setq pos2 (substr name 16 1))
;(setq pos2 (substr name pos 1)) <---- someone that could give me some help and explain why it wint work? (if (= plan pos2) (princ "This is true") (princ "This is false") );end of if statement );defun

 

0 Likes
Accepted solutions (2)
864 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

your pos variable must be an INTEGER not a STRING 

 

(setq pos 2)

 

then for your father comparison must be plan an integer as well 

 

(defun c:001 ()
 (setq plan 1);
 (setq pos 2)
 (setq name (getvar "dwgname"))
 (setq pos2 (substr name 16 1))
 ;(setq pos2 (substr name pos 1)) <---- someone that could give me some help and explain why it wint work? 
 (if (= plan pos2)
   (princ "This is true")
   (princ "This is false")
  );end of if statement
 );defun

 

Read HERE about integers, real, strings... and conversions between them...

0 Likes
Message 3 of 5

john.uhden
Mentor
Mentor
Accepted solution

How can (= plan pos2) ever be true?

 

plan is an integer, but pos2 is a string.

John F. Uhden

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

@john.uhden wrote:

How can (= plan pos2) ever be true?

 

plan is an integer, but pos2 is a string.


Good point. Thx 

 

@andreas7ZYXQ Then keep your plan a string.

0 Likes
Message 5 of 5

andreas7ZYXQ
Advocate
Advocate

I changed to this. Thanks alot for the advice @ВeekeeCZ

 

(defun c:001 ()
 (setq plan "1");
 (setq pos "2")
 (setq name (getvar "dwgname"))
(setq pos2 (substr name (atoi pos) 1))
 (if (= plan pos2)
   (princ "This is true")
   (princ "This is false")
  );end of if statement
 );defun
0 Likes