View Full Version : convert String to List
avinash00002002
2007-04-28, 07:21 AM
I have a problem with a list. I want to convert string to list.
there is three nos. of data first member size, second its part no. and third is length of that member.
"L50X50X6 45 4000" or
"L100X100X8 92 2483"
I want to convert it to list. like... (L50X50X6 45 4000) OR (L 100X100X8 92 2483).
can it is possible.
kpblc2000
2007-04-28, 08:40 AM
Look at the http://forums.augi.com/showthread.php?t=48298
function _kpblc-string-parser
Terry Cadd
2007-04-30, 10:59 PM
Try the following and see if it will work for you.
Terry
;-------------------------------------------------------------------------------
; WordList - Returns a list of words in a sentence
; Arguments: 1
; Sentence$ = String to convert into a list strings
; Syntax: (WordList "Words in a sentence") = (list "Words" "in" "a" "sentence")
; Returns: List of words or strings that were seperated by spaces in a sentence
;-------------------------------------------------------------------------------
(defun WordList (Sentence$ / Cnt# Item$ Mid$ Num# ReturnList@)
(setq Cnt# 1 Num# 1)
(repeat (strlen Sentence$)
(setq Mid$ (substr Sentence$ Cnt# 1))
(if (= Mid$ " ")
(progn
(setq Item$ (substr Sentence$ Num# (- Cnt# Num#)))
(if (/= Item$ "")
(setq ReturnList@ (append ReturnList@ (list Item$)))
);if
(setq Num# (1+ Cnt#))
);progn
);if
(setq Cnt# (1+ Cnt#))
);repeat
(if (not ReturnList@)
(setq ReturnList@ (list Sentence$))
(if (/= (substr Sentence$ Num#) "")
(setq ReturnList@ (append ReturnList@ (list (substr Sentence$ Num#))))
);if
);if
ReturnList@
);defun WordList
ElpanovEvgeniy
2007-05-01, 07:36 AM
(defun str-lst (s p)
(if (vl-string-search p s)
(str-lst (vl-string-subst "\"\"" p s) p)
(read (strcat "(\"" s "\")"))
) ;_ if
) ;_ defun
(str-lst "L50X50X6 45 4000" " ")
(str-lst "L100X100X8 92 2483" " ")
(str-lst "L50X50X6;;;45;;;4000" ";;;")
(str-lst "L100X100X8-=Divider=-92-=Divider=-2483" "-=Divider=-")
CAB2k
2007-05-01, 01:37 PM
Nice code Evgeniy. :)
peter
2007-05-01, 02:47 PM
This is my generalized solution for converting strings into lists, I use it with csv files.
(csvstringtolist "this is my test" " ") returns '("this" "is" "my" "list)
or or the comma separated
(csvstringtolist "this,is,my,test" ",") returns '("this" "is" "my" "list)
It also works for multiple letter delimiters like "**" or "\\"
Peter
(defun CSVStringToList (strText strChar / intPosition lstStrings)
(while (setq intPosition (vl-string-search strChar strText 0))
(setq lstStrings (cons (substr strText 1 intPosition) lstStrings)
strText (substr strText (+ intPosition 1 (strlen strChar)))
)
)
(if lstStrings
(reverse (cons strText lstStrings))
(list strText)
)
)
CAB2k
2007-05-01, 03:42 PM
Similar to the one I have.
; parser by CAB multi char delim
(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))
)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.