lfe011969 Posted October 21, 2010 Posted October 21, 2010 This is probably an easy question but I can neither find the answer nor figure it out on my own. Let's say I have a text file that is formated as such (these values are strings separated by a space): A B C D E F G H I J K L I know how to read the contents of the file and store it in a list but what I don't know how to do is break up the values of the individual list items. Running this code (taken from R.K. McSwain's website CAD Panacea): (defun c:test () (setq fp "L:/Scripts/test.txt") (setq fn (open fp "r")) (while (setq l (read-line file)) (setq lst (cons l lst)) ) (close file) (setq lst (reverse lst)) ) ...works as it should. The list is returned as "A B C D" "E F G H" "I J K L". So how can I extract a certain value from the 2nd line of the list? Let's say I want to assign a variable to the value of the letter F which is the 2nd letter of the 2nd line of the list? I'm stumped Lonnie Quote
Lee Mac Posted October 21, 2010 Posted October 21, 2010 You could use a string parser, as an example: ;;-------------------=={ String to List }==-------------------;; ;; ;; ;; Separates a string into a list of strings using a ;; ;; specified delimiter string ;; ;;------------------------------------------------------------;; ;; Author: Lee McDonnell, 2010 ;; ;; ;; ;; Copyright © 2010 by Lee McDonnell, All Rights Reserved. ;; ;; Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; str - string to process ;; ;; del - delimiter by which to separate the string ;; ;;------------------------------------------------------------;; ;; Returns: A list of strings ;; ;;------------------------------------------------------------;; (defun LM:str->lst ( str del / pos ) (vl-load-com) ;; © Lee Mac 2010 (if (setq pos (vl-string-search del str)) (vl-remove "" (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))) (list str) ) ) (LM:str->lst "A B C D" " ") Quote
BlackBox Posted October 21, 2010 Posted October 21, 2010 _$ (setq l (list "A B C D" "E F G H" "I J K L")) ("A B C D" "E F G H" "I J K L") _$ (foreach x l (setq l2 (append l2 (LM:str->lst x " ")))) ("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L") _$ Quote
Lee Mac Posted October 21, 2010 Posted October 21, 2010 Cheat's way: (defun test ( l ) (mapcar 'vl-princ-to-string (read (vl-string-translate "\"" " " (vl-prin1-to-string l)) ) ) ) Quote
BlackBox Posted October 21, 2010 Posted October 21, 2010 Longhand... (setq l (list "A B C D" "E F G H" "I J K L")) (mapcar '(lambda (x / oldListItem a) (while (< 0 (strlen (cond (oldListItem) ((setq oldListItem x))))) (if (= " " (setq a (substr oldListItem 1 1))) (setq oldListItem (vl-string-left-trim a oldListItem)) (progn (setq l2 (append l2 (list a))) (setq oldListItem (vl-string-left-trim a oldListItem)))))) l) Quote
lfe011969 Posted October 21, 2010 Author Posted October 21, 2010 Thanks guys! I'll try to digest your code samples before I leave work and post back my results. Quote
Lee Mac Posted October 21, 2010 Posted October 21, 2010 In vanilla, it might look like this: (defun str->lst ( str del ) ( (lambda ( s / x l ) (while (not (eq "" (setq x (substr str 1 1)))) (if (eq x del) (setq l (cons s l) s "") (setq s (strcat s x)) ) (setq str (substr str 2)) ) (reverse (cons s l)) ) "" ) ) Quote
alanjt Posted October 21, 2010 Posted October 21, 2010 (defun foo (l) (mapcar (function vl-princ-to-string) (read (vl-princ-to-string l)) ) ) Quote
Lee Mac Posted October 21, 2010 Posted October 21, 2010 Lol, I made it more difficult for myself Nice one Alan. Quote
alanjt Posted October 21, 2010 Posted October 21, 2010 Lol, I made it more difficult for myself Nice one Alan. Thanks. I looked at yours and went "Cool....wait a minute ". Quote
lfe011969 Posted October 21, 2010 Author Posted October 21, 2010 I can forsee not being able to use a " " (a space) as the character to which the string is broken up so what would be the best replacement for the space? Are there characters that cannot be used? Lonnie Quote
BIGAL Posted October 22, 2010 Posted October 22, 2010 Any character can be used as the delimater a - or _ a csv comma seperated variable is the most commonly used for trasnferring data, only hang up with , is that in addresses people put 1,34 fred st meaning unit 1 34 fred st the mail merge spits it on the comma. The other alternative is to used fixed spaced variables which use spaces to padd out the variables length within the lines. eg below may look wrong but is correct. 12345678901234567890 lie assf wow jkl lays assf wow jkl check spaces. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.