TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Andrea on February 24, 2006, 10:30:54 AM

Title: Get variable from a string..
Post 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.
Title: Re: Get variable from a string..
Post by: zoltan on February 24, 2006, 10:33:02 AM
Check out my StrSpl function here: http://www.theswamp.org/forum/index.php?topic=8812.0
Title: Re: Get variable from a string..
Post by: Joe Burke on February 24, 2006, 11:36:29 AM
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")
Title: Re: Get variable from a string..
Post by: zoltan on February 24, 2006, 11:41:58 AM
Huh??  Who is pointing to what web site?

Thanks Joe.  I knew there was a better way to do that than my crap function!
Title: Re: Get variable from a string..
Post by: ronjonp on February 24, 2006, 11:53:11 AM
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:
Title: Re: Get variable from a string..
Post by: Joe Burke on February 24, 2006, 12:10:33 PM
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.
Title: Re: Get variable from a string..
Post by: LE on February 24, 2006, 07:07:09 PM
;String to List - by John Uhden

?

 :roll:
Title: Re: Get variable from a string..
Post by: Joe Burke on February 26, 2006, 09:43:26 AM
Luis,

Is that your function, not John's? If so, I'm sorry... again.
Title: Re: Get variable from a string..
Post by: CAB on February 26, 2006, 01:01:17 PM
Here is my reinvention of the routine.

Code: [Select]
  ;;++++++++++++++++++
  ;;  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
Title: Re: Get variable from a string..
Post by: Kerry on February 26, 2006, 01:11:28 PM
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.
Title: Re: Get variable from a string..
Post by: CAB on February 26, 2006, 01:20:09 PM
That's the thing about being new to LISP <mid 2003> seems that everything has been done at least twice already. :?
Title: Re: Get variable from a string..
Post by: LE on February 27, 2006, 05:29:07 PM
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:

Code: [Select]
;;------------------------------
;;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
Title: Re: Get variable from a string..
Post by: ElpanovEvgeniy on March 05, 2006, 09:15:58 AM
Here is my

if (= (strlen pat) 1)
Code: [Select]
(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)
Code: [Select]
(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
Title: Re: Get variable from a string..
Post by: Andrea on March 06, 2006, 09:04:52 AM
Here is the arx function, if anyone wants to give it a try

Quote
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....

;-)
Title: Re: Get variable from a string..
Post by: LE on March 06, 2006, 10:21:15 AM
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.

Quote
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 ... ...)