How do I convert Getstring to a list?

How do I convert Getstring to a list?

doglips
Advocate Advocate
1,830 Views
11 Replies
Message 1 of 12

How do I convert Getstring to a list?

doglips
Advocate
Advocate

How do I change ("1 2 3 4 5") to ("1" "2" "3" "4" "5") ?

 

Thanks

0 Likes
Accepted solutions (1)
1,831 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

There are many routines on this Forum about splitting text strings apart around whatever delimiting character you like.  Some Searching will find a lot of options.

Kent Cooper, AIA
0 Likes
Message 3 of 12

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can find many routines to split a string into a list.

Here're two examples:

Iterative

(defun str2lst (str sep / len lst)
  (setq len (strlen sep))
  (while (setq pos (vl-string-search sep str))
    (setq lst (cons (substr str 1 pos) lst)
	  str (substr str (+ len pos 1))
    )
  )
  (reverse (cons (substr str 1 pos) lst))
)

Recursive:

(defun str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
    (cons (substr str 1 pos) (str2lst (substr str (+ (strlen sep) pos 1)) sep))
    (list str)
  )
)

 

(str2lst '("1 2 3 4 5") " ") returns ("1" "2" "3" "4" "5") 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 12

doglips
Advocate
Advocate

Works like a charm. Thanks

0 Likes
Message 5 of 12

john.uhden
Mentor
Mentor

Here's some ancient history...

;; This function takes a delimited string, and returns a list:
;;------------------------------
;;Don: (02-09-02)
;;
;;First let me point out that the one you call JohnU is actually
;;Marc'Antonio's from November.
;;
;;As Marc'Antonio sagely reminded us, setting string symbols is slower than
;;setting integers.
;;
;;Your offering is the almost the same a Luis' except that yours doesn't
;;provide for validating the input or for multiple-character delimiters.
;;
;;While Marc'Antonio's method is the fastest, I've grown a liking to the
;;multiple-character delimiter, which his will not handle.
;;
;;Taking the best from Luis and Marc'Antonio, plus acknowledgments to Eric
;;Schneider, I now offer the following...
(defun @Anonymous_str2list (str pat / i j n lst)
  (cond
    ((/= (type str)(type pat) 'STR))
    ((= str pat)'(""))
    (T
      (setq i 0 n (strlen pat))
      (while (setq j (vl-string-search pat str i))
        (setq lst (cons (substr str (1+ i)(- j i)) lst)
              i (+ j n)
        )
      )
      (reverse (cons (substr str (1+ i)) lst))
    )
  )
)
;; It's not as fast as Marc'Antonio's, but pretty close.

 Just one thing... you showed your string actually as a list of a string.  I doubt you really meant that, but it's okay because you can process the string within the list.

John F. Uhden

0 Likes
Message 6 of 12

pbejse
Mentor
Mentor

@doglips wrote:

How do I change ("1 2 3 4 5") to ("1" "2" "3" "4" "5") ?


Another

 

(mapcar 'itoa
	(read (strcat "(" (Car lst) ")")))

("1" "2" "3" "4" "5")

or just 
(read (strcat "(" (Car lst) ")"))
(1 2 3 4 5)

 

But more importantly , how did you end up this that list in the first place?

Maybe what you need is a routine that will build the list to be like the end result to begin with.

Is that from a list box multiple selection value?

 

0 Likes
Message 7 of 12

doglips
Advocate
Advocate
The user wanted to see the input at the command line so I used Getstring
0 Likes
Message 8 of 12

pbejse
Mentor
Mentor

@doglips wrote:
The user wanted to see the input at the command line so I used Getstring

I see, and is it always numbers? Why do you need individual strings? what are you planning to use the for? 

Anyway, try this

 

(mapcar 'itoa (read (strcat "(" (lisped "Type it here") ")")))

 

way cooler than command prompt for strings

 

0 Likes
Message 9 of 12

JBerns
Advisor
Advisor

@doglips,

 

Lee Mac ( @Lee_Mac) has a great collection of utilities.

 

Here are few examples for converting a string to a list:

http://www.lee-mac.com/stringtolist.html 

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 10 of 12

diagodose2009
Collaborator
Collaborator

You must doTrim inside Text "  Hellow   World"  , the result must be (list "Hello" "World").

You must replace repetitve Spaces into one-space 

This function is very-usefull for (read-line file-text-fromDisk)

Command: R25
16="(This function takes a delimited string, and returns a list)"=" This
function takes a delimited string, and returns a list "
11="(Hello World)"=" Hello World

"C:r25"

 

0 Likes
Message 11 of 12

john.uhden
Mentor
Mentor
Is there a list of what words should get misspelled in the conversion? 😂

John F. Uhden

0 Likes
Message 12 of 12

Sea-Haven
Mentor
Mentor

What about if you want two characters as one string ?  Then use comma as part of the getstring eg "1,2,3   4,A,B,C D E" then same method but comma.

0 Likes