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

0 Members and 1 Guest are viewing this topic.

jeff101217

  • Guest
Make strange string in to a list?
« on: April 01, 2010, 01:27:00 AM »
hi! everybody,i have a question as follow,need your help

i use autolisp created these strings by reading a text data as follow;

"*jsyq_g_list*G1*G2*G3*G4*G5*"
"*jsyq_p_list*P1*P2*P3*P4*P5*P6*"
"*jsyq_e_list*E1*E2*E3*E4*E5*E6*E7*"
"*jsyq_m_list*M1*M2*M3*M4*M5*M6*M7*M8*"
"*jsyq_h_list*H1*H2*H3*H4*H5*H6*H7*H8*H9*"
"*jsyq_b_list*B1*B2*B3*B4*B5*B6*B7*B8*B9*B10*"

i want change these strings in to string lists as follow

("jsyq_g_list""G1""G2""G3""G4""G5")
("jsyq_p_list""P1""P2""P3""P4""P5""P6")
("jsyq_e_list""E1""E2""E3""E4""E5""E6""E7")
("jsyq_m_list""M1""M2""M3""M4""M5""M6""M7""M8")
("jsyq_h_list""H1""H2""H3""H4""H5""H6""H7""H8""H9")
("jsyq_b_list""B1""B2""B3""B4""B5""B6""B7""B8""B9""B10")

i though this problem for a few days ,can't get the ansewer,
is there any easy ways to solve this?

thanks!

« Last Edit: April 01, 2010, 01:38:41 AM by jeff101217 »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Make strange string in to a list?
« Reply #1 on: April 01, 2010, 01:30:09 AM »
What was the original question posed by your teacher?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jeff101217

  • Guest
Re: Make strange string in to a list?
« Reply #2 on: April 01, 2010, 01:35:45 AM »
What was the original question posed by your teacher?

not my teacher's question , i want have a program which can read some items with some rules in the textbook,and put these items in to a dcl list box;

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Make strange string in to a list?
« Reply #3 on: April 01, 2010, 01:38:50 AM »
Show us what have you developed so far.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jeff101217

  • Guest
Re: Make strange string in to a list?
« Reply #4 on: April 01, 2010, 01:45:41 AM »
Show us what have you developed so far.

my program ,

(defun jsyq_read_basic_data()
  (setq jsyq_read_data_circle_check 0)
  (setq jsyq_read_data_text (open "c:\\lisp\\Datas\\jsyq_datas.txt" "r"))
  (setq jsyq_data_list '() jsyq_read_str 0)
  (while (= jsyq_read_data_circle_check 0)
    (if (/= jsyq_read_str nil)
      (progn
   (setq jsyq_read_str (read-line jsyq_read_data_text))
   (setq jsyq_data_list (cons jsyq_read_str jsyq_data_list))
   )
   (setq jsyq_read_data_circle_check 1)
      )
    )
  (setq jsyq_data_list (cdr jsyq_data_list))
  (setq jsyq_data_basic_g_list (cadr (cddddr jsyq_data_list)))
  (setq jsyq_data_basic_p_list (car (cddddr jsyq_data_list)))
  (setq jsyq_data_basic_e_list (car (cdddr jsyq_data_list)))
  (setq jsyq_data_basic_m_list (car (cddr jsyq_data_list)))
  (setq jsyq_data_basic_h_list (car (cdr jsyq_data_list)))
  (setq jsyq_data_basic_b_list (car jsyq_data_list))
  
  )

the items in the textbook

*jsyq_g_list*G1*G2*G3*G4*G5*
*jsyq_p_list*P1*P2*P3*P4*P5*P6*
*jsyq_e_list*E1*E2*E3*E4*E5*E6*E7*
*jsyq_m_list*M1*M2*M3*M4*M5*M6*M7*M8*
*jsyq_h_list*H1*H2*H3*H4*H5*H6*H7*H8*H9*
*jsyq_b_list*B1*B2*B3*B4*B5*B6*B7*B8*B9*B10*

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Make strange string in to a list?
« Reply #5 on: April 01, 2010, 03:14:36 AM »
Code: [Select]
(defun vk_StringSubstPat (NewPattern Pattern String / Pos NewPatternLen)
  (setq NewPatternLen (strlen NewPattern))
  (while (setq Pos (vl-string-search Pattern String Pos))
    (setq String (vl-string-subst NewPattern Pattern String Pos))
    (setq Pos (+ Pos NewPatternLen))
  )
  String
)
(mapcar
  (function
    (lambda (s)
      (read (strcat "(\"" (vk_StringSubstPat "\"\"" "*" (vl-string-trim "*" s)) "\")")
      )
    )
  )
  (list "*jsyq_g_list*G1*G2*G3*G4*G5*"
"*jsyq_p_list*P1*P2*P3*P4*P5*P6*"
"*jsyq_e_list*E1*E2*E3*E4*E5*E6*E7*"
"*jsyq_m_list*M1*M2*M3*M4*M5*M6*M7*M8*"
"*jsyq_h_list*H1*H2*H3*H4*H5*H6*H7*H8*H9*"
"*jsyq_b_list*B1*B2*B3*B4*B5*B6*B7*B8*B9*B10*"
  )
)

jeff101217

  • Guest
Re: Make strange string in to a list?
« Reply #6 on: April 01, 2010, 05:01:02 AM »
Code: [Select]
(defun vk_StringSubstPat (NewPattern Pattern String / Pos NewPatternLen)
  (setq NewPatternLen (strlen NewPattern))
  (while (setq Pos (vl-string-search Pattern String Pos))
    (setq String (vl-string-subst NewPattern Pattern String Pos))
    (setq Pos (+ Pos NewPatternLen))
  )
  String
)
(mapcar
  (function
    (lambda (s)
      (read (strcat "(\"" (vk_StringSubstPat "\"\"" "*" (vl-string-trim "*" s)) "\")")
      )
    )
  )
  (list "*jsyq_g_list*G1*G2*G3*G4*G5*"
"*jsyq_p_list*P1*P2*P3*P4*P5*P6*"
"*jsyq_e_list*E1*E2*E3*E4*E5*E6*E7*"
"*jsyq_m_list*M1*M2*M3*M4*M5*M6*M7*M8*"
"*jsyq_h_list*H1*H2*H3*H4*H5*H6*H7*H8*H9*"
"*jsyq_b_list*B1*B2*B3*B4*B5*B6*B7*B8*B9*B10*"
  )
)


thanks a lot~ :lol:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Make strange string in to a list?
« Reply #7 on: April 01, 2010, 05:35:16 AM »
Another  :-)

Code: [Select]
(defun StringStrip (str)
  ;; Lee Mac  ~  01.04.10

  (defun StringParser (str del)
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos)
            (StringParser (substr str (+ pos 1 (strlen del))) del))
      (list str)))

  (StringParser (vl-string-trim "*" str) "*"))

Code: [Select]
(StringStrip "*jsyq_g_list*G1*G2*G3*G4*G5*")
==> ("jsyq_g_list" "G1" "G2" "G3" "G4" "G5")

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Make strange string in to a list?
« Reply #8 on: April 01, 2010, 08:49:03 AM »
I want to play...

Code: [Select]
;;; Convert string to list, based on separator
;;; #Str - String to convert
;;; #Sep - Separator to break string into items
;;; Ex. - (AT:Str2Lst "1,2,3" ",") -> '("1" "2" "3")
;;; Alan J. Thompson, 11.11.09 / 04.01.10
(defun AT:Str2Lst (#Str #Sep / #Inc #List #Str)
  (while (setq #Inc (vl-string-search #Sep #Str))
    (setq #List (cons (substr #Str 1 #Inc) #List))
    (setq #Str (substr #Str (+ 2 #Inc)))
  ) ;_ while
  (vl-remove "" (reverse (cons #Str #List)))
) ;_ defun

Result:
Code: [Select]
(at:str2lst "*jsyq_g_list*G1*G2*G3*G4*G5*" "*")
("jsyq_g_list" "G1" "G2" "G3" "G4" "G5")


Updated last line of code.
« Last Edit: April 01, 2010, 11:09:09 AM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Make strange string in to a list?
« Reply #9 on: April 01, 2010, 09:05:14 AM »
Code: [Select]
  ;;++++++++++++++++++
  ;;  parser by CAB 
  ;;++++++++++++++++++
  (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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Make strange string in to a list?
« Reply #10 on: April 01, 2010, 09:16:29 AM »
LoL
Alan, we basically have the same thing.
However, I'm not sure why I appended mine instead of using cons. :ugly:
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Make strange string in to a list?
« Reply #11 on: April 01, 2010, 10:02:56 AM »
I was looking for the original thread on this subject but could not find it but did find these:

http://www.theswamp.org/index.php?topic=12985.0
http://www.theswamp.org/index.php?topic=19527.0
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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Make strange string in to a list?
« Reply #12 on: April 01, 2010, 12:09:21 PM »
The Counterpart: (Can't believe I never wrote one of these.)

Code: [Select]
;;; Convert List to String
;;; L - List to process
;;; S - Separator
;;; Ex. (AT:Lst2Str '("A" "B" "C") ",") -> "A,B,C"
;;; Alan J. Thompson, 04.01.10
(defun AT:Lst2Str (L S)
  (if (cdr L)
    (strcat (vl-princ-to-string (car L)) S (AT:Lst2Str (cdr L) S))
    (vl-princ-to-string (car L))
  ) ;_ if
) ;_ defun
« Last Edit: April 01, 2010, 03:02:51 PM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Make strange string in to a list?
« Reply #13 on: April 01, 2010, 12:15:55 PM »
Here is another way Alan.

Code: [Select]
(apply 'strcat (mapcar (function (lambda ( a ) (strcat a ","))) '("a" "b" "c")))

Granted you convert yours to a string, but that is easy enough also.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Make strange string in to a list?
« Reply #14 on: April 01, 2010, 12:37:32 PM »
Here is another way Alan.

Code: [Select]
(apply 'strcat (mapcar (function (lambda ( a ) (strcat a ","))) '("a" "b" "c")))

Granted you convert yours to a string, but that is easy enough also.

I've used something similar to that before when creating an ssget layer filter. Problem is, you end up with a trailing ",".
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox