Converse string to list

Converse string to list

Browning_Zed
Advocate Advocate
616 Views
6 Replies
Message 1 of 7

Converse string to list

Browning_Zed
Advocate
Advocate

Dear forum members, help.
How can a string be converted to a list?
"100,250,50"    >>>    '(100 250 50)

This is as an example. That is, when converting, the "x, x, x" pattern must be parsed into '(x x x).

0 Likes
Accepted solutions (2)
617 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

Will they always be whole numbers?  It makes a difference to which AutoLisp function is most appropriate to convert the pieces of text string to the numbers in the final list.

Kent Cooper, AIA
0 Likes
Message 3 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Kent1Cooper wrote:

Will they always be whole numbers?  ....


If they are, this seems to do it:

(defun TCD2LN ; = Text Comma-Delimited {to} List of Numbers
  (txt / LN)
  (while (wcmatch txt "*`,*"); still any comma(s)?
    (setq
      LN (cons (atoi (substr txt 1 (vl-string-position 44 txt))) LN)
      txt (substr txt (+ (vl-string-position 44 txt) 2))
    ); setq
  ); while
  (reverse (cons (atoi txt) LN)); add last number
); defun

Command: (TCD2LN "123,456,789")
(123 456 789)

Command: (TCD2LN "1,12,123,1234")
(1 12 123 1234)

 

It does not check whether the supplied text string is appropriate, but could be made to.

Kent Cooper, AIA
Message 4 of 7

Browning_Zed
Advocate
Advocate

Yes, it will always be whole numbers. Thank you for your help.

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor
Accepted solution

@Browning_Zed wrote:

Yes, it will always be whole numbers. Thank you for your help.


 

Another

(Defun _SNL (str);<-- String Numbers to List
(read (strcat "(" 
	(vl-string-translate  "," " " str) ")" 	))
  )

_SNL
_$ (_SNL "100,250,50")
(100 250 50)
_$ (_SNL "1,12,123,1234")
(1 12 123 1234)

 

HTH

0 Likes
Message 6 of 7

pbejse
Mentor
Mentor

Re: Converse string to list 

 

A literal solution  😊

 

converse to string.png

 

 

0 Likes
Message 7 of 7

Sea-Haven
Mentor
Mentor

May be known as laces only $10 add to shopping LIST.

SeaHaven_0-1628658540017.png

 

 

0 Likes