Author Topic: how can i achieve greater than/less than  (Read 3210 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
how can i achieve greater than/less than
« on: August 14, 2014, 03:24:16 PM »
never mind i dont got it

Code: [Select]
(setq am1 "andrew")
 (while (< > (strlen am1) 26)
                 (setq am1 (strcat am1 " "))
); end while

how can i get am1 to add spaced if less than 26 and remove spaces if greater than 26?
i thought i had it but i was wrong

« Last Edit: August 14, 2014, 03:28:26 PM by andrew_nao »

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: how can i achieve greater than/less than
« Reply #1 on: August 14, 2014, 03:48:08 PM »
Adding is easy;

Code - Auto/Visual Lisp: [Select]
  1. (setq am1 "andrew")
  2.  (while (< (strlen am1) 26)
  3.                  (setq am1 (strcat am1 " "))
  4. ); end while
  5.  

For the subtracting though you will need to convert the text to ascii number list, then subtract the 1st number from the list and finally convert back to text.
However are you sure it will always be spaces you are subtracting from the list, would you want to only subtract the 1st number from the list if it is a space ?

Something like this will work;

Code - Auto/Visual Lisp: [Select]
  1. (setq am1 "                                       testing"
  2.       am1L (vl-string->list am1)
  3.       )
  4. (while (and (> (vl-list-length am1l)26)
  5.             (= (car am1L) 32)
  6.             )
  7.   (setq am1L (cdr am1L))
  8.   )
  9. (setq am1 (vl-list->string am1L))
  10.  
  11.  
« Last Edit: August 14, 2014, 04:03:01 PM by snownut2 »

andrew_nao

  • Guest
Re: how can i achieve greater than/less than
« Reply #2 on: August 14, 2014, 04:00:44 PM »
Adding is easy;

Code - Auto/Visual Lisp: [Select]
  1. (setq am1 "andrew")
  2.  (while (< (strlen am1) 26)
  3.                  (setq am1 (strcat am1 " "))
  4. ); end while
  5.  

For the subtracting though you will need to convert the text to ascii number list, then subtract the 1st number from the list and finally convert back to text.
However are you sure it will always be spaces you are subtracting from the list, would you want to only subtract the 1st number from the list if it is a space ?

how do you convert text to ascii and back?

it will always be spaces
i tried this, but i wasnt successful

Code: [Select]
(while (< (strlen am1) 26)
(progn
(while (< (strlen am1) 26)
    (setq am1 (strcat am1 " "))
    ); end while
   )
(progn
(while (> (strlen am1) 26)
    (setq am1 (vl-string-right-trim " " am1))
    ); end while
   )
)

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: how can i achieve greater than/less than
« Reply #3 on: August 14, 2014, 04:04:22 PM »
see my previous post I just added the subtraction section....

ronjonp

  • Needs a day job
  • Posts: 7531
Re: how can i achieve greater than/less than
« Reply #4 on: August 14, 2014, 04:15:14 PM »
Another to add spaces to end.

Code: [Select]
(defun _foo (string len) (repeat (- len (strlen string)) (setq string (strcat string " "))) string)
;; Pad spaces at end
(_foo "testing" 26)
;; Remove spaces and pad ( assuming the excess characters are spaces )
(_foo (vl-string-right-trim " " "testing") 26)
« Last Edit: August 14, 2014, 04:20:09 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i achieve greater than/less than
« Reply #5 on: August 14, 2014, 04:22:17 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( s ) (substr (strcat s "                          ") 1 26))

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i achieve greater than/less than
« Reply #6 on: August 14, 2014, 04:25:09 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( s n )
  2.     (cond
  3.         (   (< n (strlen s))
  4.             (foo (substr s 1 (1- (strlen s))) n)
  5.         )
  6.         (   (< (strlen s) n)
  7.             (foo (strcat s " ") n)
  8.         )
  9.         (   s   )
  10.     )
  11. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo "Andrew" 26)
  2. "Andrew                    "

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i achieve greater than/less than
« Reply #7 on: August 14, 2014, 04:29:35 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( s n / p )
  2.     (setq p " ")
  3.     (repeat (1+ (fix (/ (log n) (log 2)))) (setq p (strcat p p)))
  4.     (substr (strcat s p) 1 n)
  5. )

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: how can i achieve greater than/less than
« Reply #8 on: August 14, 2014, 04:32:46 PM »
My last post deducted all but 26 characters of text from the beginning of the text, this code takes all but the 26 characters from the end of the text.

Code - Auto/Visual Lisp: [Select]
  1. (setq am1 "    testing                              "
  2.       am1L (vl-string->list am1)
  3.       )
  4. (while (> (vl-list-length am1L)26)
  5.   (setq am1L (reverse(cdr (reverse am1L))))
  6.   )
  7. (setq am1 (vl-list->string am1L))
  8.  
  9.  

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i achieve greater than/less than
« Reply #9 on: August 14, 2014, 04:39:51 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( s n / p )
  2.     (repeat n (setq p (cons 32 p)))
  3.     (vl-list->string (mapcar '(lambda ( a b ) a) (append (vl-string->list s) p) p))
  4. )

andrew_nao

  • Guest
Re: how can i achieve greater than/less than
« Reply #10 on: August 15, 2014, 10:01:28 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( s n / p )
  2.     (repeat n (setq p (cons 32 p)))
  3.     (vl-list->string (mapcar '(lambda ( a b ) a) (append (vl-string->list s) p) p))
  4. )

there is that lambda again.. i gotta start learning to use that

thanks everyone for your replies

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: how can i achieve greater than/less than
« Reply #11 on: August 15, 2014, 05:10:48 PM »
How about this
Code - Auto/Visual Lisp: [Select]
  1. (defun foo (s)
  2.     (substr
  3.         (strcat (vl-string-trim " " s)
  4.                 "                          ")
  5.     26)
  6. )

While simple, it doesn't allow for a variable length of string to be returned.

Error checking would be needed if the passed string's length is greater 26 characters.

I am sure there are other ways to handle this, but this has always been my preferred method of handling things:
Step 1) Trim all whitespace.
Step 2) Concatenate the result with a string of characters that is the maximum length needed.
Step 3) Trim the resultant string to the maximum length needed.

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie