TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Andrea on February 24, 2006, 10:30:54 AM
-
Hi All..
I would like to know the best and shorter way to get all item in this string...
"0*0v*234*test*variables*5677.89*12.4"
to have
"0"
"0v"
"234"
"test"
"variables"
"5677.89"
"12.4"
For now i use car cadr addr etc...
but just curious if there is anoother way.
thanks.
-
Check out my StrSpl function here: http://www.theswamp.org/forum/index.php?topic=8812.0
-
I grow weary of folks pointing to their web sites for no other reason than to promote themselves or turn a buck.
This is a well known function which certainly does not need reinvention.
;String to List - by John Uhden
(defun String2List (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))
)
)
) ;end
Command: (string2list "jack\tis\ta\tgood\tboy" "\t")
("jack" "is" "a" "good" "boy")
-
Huh?? Who is pointing to what web site?
Thanks Joe. I knew there was a better way to do that than my crap function!
-
I grow weary of folks pointing to their web sites for no other reason than to promote themselves or turn a buck.
This is a well known function which certainly does not need reinvention.
;String to List - by John Uhden
(defun String2List (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))
)
)
) ;end
Command: (string2list "jack\tis\ta\tgood\tboy" "\t")
("jack" "is" "a" "good" "boy")
Someone a little grumpy today? :lmao:
-
Huh?? Who is pointing to what web site?
Thanks Joe. I knew there was a better way to do that than my crap function!
My sincerest apologies. Obviously I mis-read and thought you were pointing to somewhere other than theswamp.
Time for me shut-up and go to bed.
-
;String to List - by John Uhden
?
:roll:
-
Luis,
Is that your function, not John's? If so, I'm sorry... again.
-
Here is my reinvention of the routine.
;;++++++++++++++++++
;; phraser by CAB
;;++++++++++++++++++
(defun sphraser (str delim / ptr lst)
(while (setq ptr (vl-string-search delim str))
(setq lst (cons (substr str 1 ptr) lst))
(setq str (substr str (+ ptr 2)))
)
(reverse (cons str lst))
)
Although very similar to John's.
I used it in this routine and others so I think it works OK.
http://www.theswamp.org/forum/index.php?topic=7263.msg89789#msg89789
-
Here is my reinvention of the routine.
< snip >
hehehehehehehe .. yes, that one has had a few authors over the years. I have one published 97 by John Gibb and Jeff Pilcher, and I recokon that some could claim earlier publication dates.
There may have been a version in the NEW Rider book "Inside AutoLisp" [ Rusty Gesner (et al) 1989 or so ] .. but my copy has been borrowed . and I can't check.
-
That's the thing about being new to LISP <mid 2003> seems that everything has been done at least twice already. :?
-
Luis,
Is that your function, not John's? If so, I'm sorry... again.
Hi Joe;
I do not know where my original function is now... has been a while.
Here is what my friend Don Butler did:
;;------------------------------
;;Don: (02-09-02)
;;
;;First let me point out that the one you call JohnU is astually
;;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 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.
And lately I wrote a new version using C++/ARX here:
http://www.theswamp.org/forum/index.php?topic=8460.0
-
Here is my
if (= (strlen pat) 1)
(defun str-str-lst (str pat / i)
(cond ((= str "") nil)
((setq i (vl-string-search pat str))
(cons
(substr str 1 i)
(str-str-lst (substr str (+ 2 i)) pat)
) ;_ cons
)
(t (list str))
) ;_ cond
) ;_ defun
if (> (strlen pat) 1)
(defun str-str-lst (str pat / i)
(cond ((= str "") nil)
((setq i (vl-string-search pat str))
(cons
(substr str 1 i)
(str-str-lst (substr str (+ (strlen pat) 1 i)) pat)
) ;_ cons
)
(t (list str))
) ;_ cond
) ;_ defun
-
Here is the arx function, if anyone wants to give it a try
Usage:
delimiter = as string can include multiple patterns - see samples.
(StringToList < string > < delimiter >)
Samples:
Code:
Command: (stringtolist)
Invalid: function expects two string arguments!
nil
Command: (stringtolist "this is only a test" " ")
("this" "is" "only" "a" "test")
Command: (stringtolist "this\\is\\only\\a\\test" "\\")
("this" "is" "only" "a" "test")
Command: (stringtolist "this\\is*only*a\\test" "\\*")
("this" "is" "only" "a" "test")
Command: (stringtolist "this\\is*only*a-test" "\\*-")
("this" "is" "only" "a" "test")
For AutoCAD 2004-2006 and vertical products.
Note: the source code is under the (was C++) forum... in the String To List topic.
LE...your ARX is cool..
but tha fact is we need every time to be sure that the arx is loaded..
so i need to modify some lisp and sometime...remake some package installation.
but I like that....if there a way to make a VLX containing ARX.....please let me know..
I'll put also the DosLIB....
;-)
-
but tha fact is we need every time to be sure that the arx is loaded..
Just place the arx file into any of the support folders of autocad and use the normal loading for an arx (arxload "stringtolist.arx" nil)... inside of any MNL or acaddoc.lsp
I can make the arx to register itself for autoloading, but I prefer to leave the arx simple as-is.
but I like that....if there a way to make a VLX containing ARX.....please let me know..
I'll put also the DosLIB....
No, but you might want to place inside the same call of (arxload ... ...)