Author Topic: Recursion help string->list  (Read 7534 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
Recursion help string->list
« on: March 21, 2008, 10:13:26 AM »
I'm trying to figure out how to write a function recursively. What I'd like it to do is:
Take a string and verify it's a string and strcase it
Check the string for comma separation
If there are commas in the string, recursively run through the string and create a list of the different strings without the commas.
If there are no commas, simply make it a list
Example: "huron,ontario,michigan,erie,superior" -> ("HURON" "ONTARIO" "MICHIGAN" "ERIE" SUPERIOR")
Here is an example of what I've got so far.
Code: [Select]
(defun
      str->lst (string)
   (cond ((= (type string) 'str) ;is it a string
  (setq string (strcase string))
  (cond ((= (type (setq comma (vl-string-position 44 string))) 'int)
 ;has commas
(while comma
    (str->lst (setq str (substr string 1 comma)))
    (setq string
    (vl-string-left-trim
       (strcat str ",")
       string
    )
  comma (vl-string-position 44 string))
)
)
(t (setq string (list string)))
  )
)
(t
  (princ "\nArgument required is not a text string. Exiting.")
  (exit)
)
   )
   str->lst
)
What am I not doing right, here?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Recursion help string->list
« Reply #1 on: March 21, 2008, 10:29:30 AM »
Why Recursion?
Code: [Select]
;  parser by CAB single character delim, match ","
(defun sparser (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))
)
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Recursion help string->list
« Reply #2 on: March 21, 2008, 10:35:56 AM »
I think i agree with CAB; i think recursion isnt as straight forward as you want it to be in this instance and might cause a performance hit.

CAB try `repeat'; i think that could speed it up a bit.


EDIT: stupid keyboards; they hinder my expressive powers.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
Re: Recursion help string->list
« Reply #3 on: March 21, 2008, 10:38:54 AM »
Because I want it.
Nice routine by the way.
Without recursion here's what I came up with:
wildcard is a string: "*LINE,ARC,CIRC*"
the return would be: ("*LINE" "ARC" "CIRC*")
Code: [Select]
(while (setq comma (vl-string-position 44 ;|","|; wildcard))
     (setq selset   (append
       selset
       (list (setq str (substr wildcard 1 comma))
       )
    )
   wildcard (vl-string-left-trim
       (strcat str ",")
       wildcard
    )
     )
  )
(setq selset (append selset (list wildcard)))
Not the entire code, but I'm sure you get the gist. Also, the way this is working it feels like recursion would be cleaner code. I'll expound more later.
« Last Edit: March 21, 2008, 10:57:04 AM by Daron »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Recursion help string->list
« Reply #4 on: March 21, 2008, 10:42:45 AM »
I think i agree with CAB; i think recursion isnt as straight forward as you want it to be in this instance and might cause a performance hit.

CAB try `repeat'; i think that could speed it up a bit.


EDIT: stupid keyboards; they hinder my expressive powers.

Bet you a beer it won't.  :-)
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Recursion help string->list
« Reply #5 on: March 21, 2008, 10:44:19 AM »
CAB,
Wait, what am i talking about? I was thinking; i cant think of a good way to use repeat...so scratch that thought. Your good.

Darron, I will take a quick look at your code in a second.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Recursion help string->list
« Reply #6 on: March 21, 2008, 10:45:24 AM »
I think i agree with CAB; i think recursion isnt as straight forward as you want it to be in this instance and might cause a performance hit.

CAB try `repeat'; i think that could speed it up a bit.


EDIT: stupid keyboards; they hinder my expressive powers.

Bet you a beer it won't.  :-)

Ah!? ...You beet me to it.

No i wont bet you a beer cause i was being stupid.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Recursion help string->list
« Reply #7 on: March 21, 2008, 10:49:39 AM »
BTW, Did you write that code? I got one i use, almost exactly the same, but i dont know where it came from (usually i document who i got code from).

Code: [Select]
(defun strParse (aStr delim / strList pos)
 ;;===================================================================;
 ;; StrParse                                                          ;
 ;;-------------------------------------------------------------------;
 ;; This function will take a string and parse it out into a list of  ;
 ;; of strings.                                                       ;
 ;;                                                                   ;
 ;; Arguments: aStr - A string to parse                               ;
 ;;            delim - A string of the delimiter                      ;
 ;;                                                                   ;
 ;; Example: (StrParse "This is a test string" " ")                   ;
 ;; Returns -> ("This" "is" "a" "test" "string")                      ;
 ;;                                                                   ;
 ;; History: Unknown                                                  ;
 ;;===================================================================;
  (while
    (setq pos (vl-string-search delim aStr 0))
    (setq strList (cons (substr aStr 1 POS) strList)
          aStr (substr aStr (+ pos 2)))
    )
  (reverse (cons aStr strList))
 )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Recursion help string->list
« Reply #8 on: March 21, 2008, 10:54:53 AM »
Hi,

Here's mine (I think I posted it yet), it's recursive.

Code: [Select]
;; STR2LST
;; Transforms a string with separator into a list of strings
;;
;; Arguments
;; str = the string
;; sep = the separator pattern

(defun str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
    (cons (substr str 1 pos)
  (str2lst (substr str (+ (strlen sep) pos 1)) sep)
    )
    (list str)
  )
)

And the reverse function

Code: [Select]
;; LST2STR
;; Returns a string which is the concatenation of a list and a separator
;;
;; Arguments
;; str = the string
;; sep = the separator pattern

(defun lst2str (lst sep)
  (if (cadr lst)
    (strcat (vl-princ-to-string (car lst))
    sep
    (lst2str (cdr lst) sep)
    )
    (vl-princ-to-string (car lst))
  )
)

Quote
Why Recursion?
Because it's often the first thing comming to my mind (may be a way of thinking) and because I found it quite elegant.
« Last Edit: March 21, 2008, 11:12:08 AM by gile »
Speaking English as a French Frog

daron

  • Guest
Re: Recursion help string->list
« Reply #9 on: March 21, 2008, 11:00:59 AM »
Thanks gile. I'll look at that.

daron

  • Guest
Re: Recursion help string->list
« Reply #10 on: March 21, 2008, 11:24:26 AM »
That rules gile. Thanks a lot.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Recursion help string->list
« Reply #11 on: March 21, 2008, 11:30:17 AM »
BTW, Did you write that code? I got one i use, almost exactly the same, but i dont know where it came from (usually i document who i got code from).

Code: [Select]
(defun strParse (aStr delim / strList pos)
 ;;===================================================================;
 ;; StrParse                                                          ;
 ;;-------------------------------------------------------------------;
 ;; This function will take a string and parse it out into a list of  ;
 ;; of strings.                                                       ;
 ;;                                                                   ;
 ;; Arguments: aStr - A string to parse                               ;
 ;;            delim - A string of the delimiter                      ;
 ;;                                                                   ;
 ;; Example: (StrParse "This is a test string" " ")                   ;
 ;; Returns -> ("This" "is" "a" "test" "string")                      ;
 ;;                                                                   ;
 ;; History: Unknown                                                  ;
 ;;===================================================================;
  (while
    (setq pos (vl-string-search delim aStr 0))
    (setq strList (cons (substr aStr 1 POS) strList)
          aStr (substr aStr (+ pos 2)))
    )
  (reverse (cons aStr strList))
 )

Yes I did along with this one:
Code: [Select]
;  parser by CAB multi char delim, match "xyz"
(defun sparser (str delim / dlen ptr lst)
  (setq dlen (1+ (strlen delim)))
  (while (setq ptr (vl-string-search delim str))
    (setq lst (cons (substr str 1 ptr) lst))
    (setq str (substr str (+ ptr dlen)))
  )
  (reverse(cons str lst))
)
Here is the link to our last discussion:
http://www.theswamp.org/index.php?topic=12985.0
« Last Edit: March 21, 2008, 11:34:12 AM by CAB »
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Recursion help string->list
« Reply #12 on: March 21, 2008, 11:41:28 AM »
*click*

I will have to chew on that code later today.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Recursion help string->list
« Reply #13 on: March 21, 2008, 11:53:27 AM »
Nice one gile.

Here is my reverse.
Code: [Select]
(defun lst2str (lst sep / str)
  (setq str (apply 'strcat (mapcar '(lambda (x) (strcat x sep)) lst)))
  (substr str 1 (1- (strlen str)))
)
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.

daron

  • Guest
Re: Recursion help string->list
« Reply #14 on: March 21, 2008, 12:08:06 PM »
Speaking of chewing, here I go making things more difficult than they absolutely need to be again. What I was ultimately trying to do besides understand recursion was to take the idea of filtering entsel that Chuck Gabriel started a topic on. I remembered 7's little gem and thought it would be cool to make that more robust. I was thinking of using mapcar lambda functions to parse through each item of the string argument and compare it with the selected result. Funny thing is, I was planning on using wcmatch, but since I haven't done much with wcmatch in a long while, I forgot that it can take a comma delimited string and use it to compare the selected item. While this was a good learning experience, it was obviously unnecessary for what I really needed. However, here is the code with 7's original name intact so we know where it started.
Code: [Select]
(defun entse7 (wildcard / ent)
   (cond ((setq ent (entsel (strcat "Get any " wildcard " type of object: ")))
  (if (not (wcmatch (cdr (assoc 0 (entget (car ent)))) wildcard))
     (entse7 wildcard)
  )
)
(t (entse7 wildcard))
   )
  ent
)
To really tighten it up, I suppose it would be a good idea to gather a list of possible named items and compare the list with it, but I won't.

Now, you might still be asking, why do I need a filtered list of objects for a function that can only return one object? The intent here is to use entse7 in a loop to gather objects in a list, then manipulate that list according to order of selection. Ssget is just too loose for what I'm looking at. 'zat make sense?
« Last Edit: March 21, 2008, 12:22:52 PM by Daron »