Author Topic: Make strange string in to a list?  (Read 10818 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Make strange string in to a list?
« Reply #30 on: April 02, 2010, 07:48:37 AM »
You're welcome Jeff :-)

You've now got plenty of examples to learn from  :-)

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: Make strange string in to a list?
« Reply #31 on: July 20, 2010, 10:53:06 AM »
Hi :) thanks for all your contribution~

Because I often need to deal with txt data, so the delim is not only one char, but maybe like " \t\n,;"

(" \t\n,;" means " " "\t" "n" "," ";"  can all be delims)

so, for a delim represent more than one char, I write such code, I hope you can shorten it :)

Code: [Select]
;;;by qjchen@gmail.com
(defun q:str:delim(str delim / l1 l2)
  (setq str (vl-string->list str) delim (vl-string->list delim))
  (while str
    (if (not (member (car str) delim))
             (setq l1 (cons (car str) l1))
             (if l1 (setq l2 (cons (vl-list->string (reverse l1)) l2) l1 nil))
    )
    (setq str (cdr str))
  )
  (if l1 (setq l2 (cons (vl-list->string (reverse l1)) l2)))
  (reverse l2)
)

(q:str:delim "a,bb c\tdd\ne" " ,\t\n")
(q:str:delim "a,bb c\tdd\ne" " ,\t\n")=> ("a" "bb" "c" "dd" "e")

before I wrote this my own code, I always use the following std-lib function, it can be used in Autocad R14
Code: [Select]
(defun STD-STRING->LIST (s / lst)
  (if (= (type s) 'STR)
    (while (/= s "")
      (setq lst (cons (ascii (substr s 1 1)) lst) s (substr s 2))
    )
  )
  (reverse lst)
)
(defun STD-STRTOK (s delims / len s1 i c lst)
  (setq delims (std-string->list delims)
len (strlen s) s1 "" i (1+ len)
  )
  (while (> (setq i (1- i)) 0)
    (setq c (substr s i 1))
    (if (member (ascii c) delims)
      (if (/= s1 "") (setq lst (cons s1 lst) s1 ""))
      (setq s1 (strcat c s1))
    )
  )
  (if (/= s1 "") (cons s1 lst) lst)
)


(STD-STRTOK "a,bb c\tdd\ne" " ,\t\n")


« Last Edit: July 20, 2010, 11:01:03 AM by qjchen »
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
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: Make strange string in to a list?
« Reply #33 on: July 20, 2010, 12:32:58 PM »
>  ... std-lib ...
Reini Urban's or Vladimir Nesterovsky's lib(s) is a very good resource to learn from.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: Make strange string in to a list?
« Reply #34 on: July 20, 2010, 08:14:02 PM »
Maybe this?

http://www.theswamp.org/index.php?topic=22034.msg265916#msg265916

Alan, it seems not the all the same as this post.

in my code, the delim need not to be multi continuous char, but like a list of delim,

" \t\n,;" means " " "\t" "\n" "," ";"  can all be delim separately, need not to be adjacent.

sorry for my poor English, I am not sure can you understand my meaning.

To Seven: I also learn a lot from Reini Urban's STD-LIB function.



http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

hermanm

  • Guest
Re: Make strange string in to a list?
« Reply #35 on: July 20, 2010, 08:56:09 PM »
Code: [Select]
;;;-------------------strtok------------
;;;creates a list of tokens from a delimited string
;;;delim must be one distinct character, but may be repeated
;;;-----------------------------------------
(defun strtok (str delim / out pos)
  (setq delim (ascii delim))
  (while (setq pos (vl-string-position delim str))
    (if (> pos 0);not just adjacent delimiters
      (setq out (cons (substr str 1 pos) out))
    )
    (setq str (substr str (+ 2 pos)))
  )
  (if (> (strlen str) 0);not a trailing delimiter
    (reverse (cons str out))
    (reverse out)
  )
);strtok
Code: [Select]
Command: (strtok "*jsyq_p_list*P1*P2*P3*P4*P5*P6*" "*")
("jsyq_p_list" "P1" "P2" "P3" "P4" "P5" "P6")

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Make strange string in to a list?
« Reply #36 on: July 21, 2010, 05:58:26 PM »
Perhaps:

Code: [Select]
(defun LM:StringParser ( str del )
  ;; © Lee Mac  ~  14.06.10
  (if (setq pos (vl-string-search del str))
    (vl-remove ""
      (cons (substr str 1 pos)
        (LM:StringParser
          (substr str (+ pos 1 (strlen del))) del
        )
      )
    )
    (list str)
  )
)

efernal

  • Bull Frog
  • Posts: 206
Re: Make strange string in to a list?
« Reply #37 on: April 09, 2012, 09:50:14 AM »
I use this...
Code - Auto/Visual Lisp: [Select]
  1. (DEFUN ef_destrincha (string chars / cn id sn lista saida)
  2.   (SETQ sn    (STRLEN string)
  3.         cn    1
  4.         saida ""
  5.         lista nil
  6.   )
  7.   (REPEAT sn
  8.     (SETQ id   (CAR chars)
  9.           char (SUBSTR string cn 1)
  10.     )
  11.     (IF (/= char id)
  12.       (SETQ saida (STRCAT saida char)
  13.             cn    (1+ cn)
  14.       )
  15.       (SETQ string (SUBSTR string (1+ cn))
  16.             lista  (APPEND lista (LIST saida))
  17.             saida  ""
  18.             cn     1
  19.             chars  (IF chars
  20.                      (CDR chars)
  21.                      nil
  22.                    )
  23.       )
  24.     )
  25.   )
  26.   (IF (AND string (> (STRLEN string) 0))
  27.     (SETQ lista (APPEND lista (LIST string)))
  28.   )
  29.   lista
  30. )
  31.  
  32. (ef_destrincha
  33.   "Vamos:ver o que|podemos fazer com[esta rotina de \"ef_destrincha\"]baseada em listas"
  34.   '(":" "|" "[" "]")
  35. )
  36. (ef_destrincha
  37.   "Let's see:what we can do|with this[\"ef_destrincha\" routine]based on lists"
  38.   '(":" "|" "[" "]")
  39. )
  40. ;;      returns
  41. ("Vamos" "ver o que" "podemos fazer com" "esta rotina de \"ef_destrincha\"" "baseada em listas")
  42. ("Let's see" "what we can do" "with this" "\"ef_destrincha\" routine" "based on lists")
  43.  
e.fernal