from string to list

from string to list

Moshe-A
Mentor Mentor
1,621 Views
6 Replies
Message 1 of 7

from string to list

Moshe-A
Mentor
Mentor

Guys hi,

 

i looking for the most efficient way (a function) to convert a string to list. the string is coming from a dcl list_box.

 

 

from this: "0 5 9 12 18"

to this:      '(0 5 9 12 18)

 

thanks

Moshe

 

0 Likes
Accepted solutions (1)
1,622 Views
6 Replies
Replies (6)
Message 2 of 7

pbejse
Mentor
Mentor
Accepted solution
(read (Strcat "("  "0 5 9 12 18" ")"))

 

(Setq info  "0 5 9 12 18" ")
(read (Strcat "("  info ")"))

 

Message 3 of 7

Moshe-A
Mentor
Mentor

thanks pbejse

Excellent  Smiley LOL

 

0 Likes
Message 4 of 7

john.uhden
Mentor
Mentor

That's great for space-delimited strings.

What about comma or tab-delimited strings, or multicharacter delimited strings?

I use this:

  ;;---------------------------------------------------------
  ;; Function to convert a string with delimiters into a list
  ;; pat is the delimiter and can contain multiple characters
  (defun @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))
         )
      )
   )
Command: (setq str "1<>2<>5<>9<>12<>18")
"1<>2<>5<>9<>12<>18"

Command: (@str2list str "<>")
("1" "2" "5" "9" "12" "18")

John F. Uhden

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor

@john.uhden wrote:

That's great for space-delimited strings.

What about comma or tab-delimited strings, or multicharacter delimited strings?

 


 

The request is for specifically converting the value from DCL list box with multiple selection options.

The format will always be similar to this--> "0 5 9 12 18"  "2 4 10"

  

Say, what about, converting the string value depending on what type it is.

"0 5 9 12 Apple Banana 12.34 Man"  --> (0 5 9 12 "Apple" "Banana" 12.34 "Man")

 

 

0 Likes
Message 6 of 7

pbejse
Mentor
Mentor

To make it more interesting

"0 5 9 12 Apple Banana 12.34 Man (1.0 2.0 0.0) (2.0 4.0 0.0)"

to

(list 0 5 9 12 "Apple" "Banana" 12.34 "Man" '(1.0 2.0 0.0) '(2.0 4.0 0.0))

 

 

 

0 Likes
Message 7 of 7

john.uhden
Mentor
Mentor

Very interesting, if there were a use for it.

My apologies for missing the DCL usage.  I guess I had better check out what I have ever done with multi-select list boxes.  Your solution looks perfect.

John F. Uhden

0 Likes