Author Topic: Get variable from a string..  (Read 5346 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
Get variable from a string..
« 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.
Keep smile...

zoltan

  • Guest
Re: Get variable from a string..
« Reply #1 on: February 24, 2006, 10:33:02 AM »
Check out my StrSpl function here: http://www.theswamp.org/forum/index.php?topic=8812.0

Joe Burke

  • Guest
Re: Get variable from a string..
« Reply #2 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")

zoltan

  • Guest
Re: Get variable from a string..
« Reply #3 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!

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Get variable from a string..
« Reply #4 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:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Joe Burke

  • Guest
Re: Get variable from a string..
« Reply #5 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.

LE

  • Guest
Re: Get variable from a string..
« Reply #6 on: February 24, 2006, 07:07:09 PM »

Joe Burke

  • Guest
Re: Get variable from a string..
« Reply #7 on: February 26, 2006, 09:43:26 AM »
Luis,

Is that your function, not John's? If so, I'm sorry... again.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get variable from a string..
« Reply #8 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
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get variable from a string..
« Reply #9 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get variable from a string..
« Reply #10 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. :?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

LE

  • Guest
Re: Get variable from a string..
« Reply #11 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
« Last Edit: February 27, 2006, 05:32:38 PM by LE »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Get variable from a string..
« Reply #12 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

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Get variable from a string..
« Reply #13 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....

;-)
Keep smile...

LE

  • Guest
Re: Get variable from a string..
« Reply #14 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 ... ...)