String to List

Discussion in 'AutoCAD' started by Chip Harper, Jun 23, 2004.

  1. Chip Harper

    Chip Harper Guest

    I have looked through older posts but I'm still lost .... I want to store
    data in a txt file, read it and use it in my lisp. I can read it ok but I
    can't put the stuff into usable form. For ease of reading (for us human
    types) I want to place a layer name, layer color, linetype and plot/no plot
    on a single line in the file. This is something I put together to focus on
    the string to list conversion portion. My thinking was to use READ to get
    the first portion of the text string then use STRLEN add 1 for the space to
    make a new string out of the last three pieces use read again and so on but
    I get ..bad argument type: stringp S-TEXT.. as usual any and all help is
    appreciated. I'm still new at this so I would like to avoid VLISP at this
    point.

    (defun c:stol ( )
    (setq TARGET "S-TEXT 1 CONTINUOUS PLOT")
    (setq SREAD (read TARGET))
    (setq LEN1 (strlen SREAD))
    )
     
    Chip Harper, Jun 23, 2004
    #1
  2. Sounds like this is what you need, although there
    is a small bit of vlisp involved.

    ; creates multiple strings from a single string
    ; - string, string to breakdown
    ; [d] - string, separator
    ; return: list of strings
    (defun parse (s d / i l tmp rtn)
    (cond
    ((/= "" d)
    (setq l (strlen d))
    (while (setq i (vl-string-search d s))
    (setq tmp (substr s 1 i))
    (setq rtn (cons tmp rtn))
    (setq s (substr s (1+ (+ l (strlen tmp))) (strlen s)))
    )
    (vl-remove "" (reverse (cons s rtn)))
    )
    (t s)
    )
    )

    (parse "S-TEXT 1 CONTINUOUS PLOT" " ")

    returns

    ("S-TEXT" "1" "CONTINUOUS" "PLOT")
     
    Jason Piercey, Jun 23, 2004
    #2
  3. if LISP is the only thing you need to read the
    data with, then just write each sublist out in
    a format that can be easily read back with the
    (read) function or the (load) function:

    ("apples" 25 1.49 "Delicious")
    ("oranges" 42 1.39 "Valencia")
    ("cherries" 250 2.99 "Bing")

    The above can be read back into a list using
    the (read) function. It can be written using
    (print), but keep in mind that it will round off
    floating point values at the 5th place.

    To read the entire file using load, preceed the
    first line with '(quote' and add a single ')' to the
    end of the last line, and then just assign the
    result of (load) to your list:

    (quote
    ("apples" 25 1.49 "Delicious")
    ("oranges" 42 1.39 "Valencia")
    ("cherries" 250 2.99 "Bing")
    )




    AutoCAD based Security Planning Solutions:
    http://www.caddzone.com/securityplanning
     
    Tony Tanzillo, Jun 23, 2004
    #3
  4. Here is a subroutine I use (a lot) to take any string and create a list
    based on common separator character(s). Note that multiple separator
    characters are allowed. So, when you are writing out the data to a file,
    make sure you use a separator character that is NOT a character used in any
    of the variables.

    Examples -
    (setq Test "123,456,789")
    (setq LTest (GETTEXTLIST Test ","))
    returns
    ("123" "456" "789")

    (setq Test "123 ,456 ,789")
    (setq LTest (GETTEXTLIST Test ","))
    returns
    ("123 " "456 " "789")

    (setq LTest (GETTEXTLIST Test " ,")) ;example of use of multiple separater
    characters
    returns
    ("123" "456" "789")

    (defun GETTEXTLIST (TTT TF / LTF KKS KKF KKK KKL)
    (setq LIST_TEXT nil)
    (setq LTF (strlen TF))
    (setq KKS 1 KKF 0 KKK 1 KKL (strlen TTT))
    (while (<= KKK KKL)
    (if (= (substr TTT KKK LTF) TF)
    (progn
    (setq LIST_TEXT (append LIST_TEXT (list (substr TTT KKS (- KKK
    KKS)))))
    (setq KKS (+ KKK LTF))
    )
    )
    (setq KKK (1+ KKK))
    )
    (setq LIST_TEXT (append LIST_TEXT (list (substr TTT KKS))))
    )
     
    Alan Henderson @ A'cad Solutions, Jun 23, 2004
    #4
  5. ; delimiter = 1 character
    (defun ALE_String2List (InpStr CarDlm / SttPos EndPos TmpLst)
    (setq
    CarDlm (ascii CarDlm) SttPos 0
    EndPos (vl-string-position CarDlm InpStr)
    )
    (while EndPos
    (setq
    TmpLst (cons (substr InpStr (1+ SttPos) (- EndPos SttPos)) TmpLst)
    SttPos (1+ EndPos) EndPos (vl-string-position CarDlm InpStr SttPos)
    )
    )
    (reverse (cons (substr InpStr (1+ SttPos)) TmpLst))
    )

    Command: (setq TARGET "S-TEXT 1 CONTINUOUS PLOT")
    "S-TEXT 1 CONTINUOUS PLOT"

    Command: (ALE_String2List TARGET " ")
    ("S-TEXT" "1" "CONTINUOUS" "PLOT")
     
    Marc'Antonio Alessi, Jun 23, 2004
    #5
  6. Chip Harper

    zeha Guest

    shorter way

    (mapcar 'vl-princ-to-string (read (strcat "(" "S-TEXT 1 CONTINUOUS PLOT" ")")));;;->("S-TEXT" "1" "CONTINUOUS" "PLOT")
     
    zeha, Jun 23, 2004
    #6
  7. Chip Harper

    Chip Harper Guest

    I saw this example before when I was reading older threads, because this is
    the format I really want I tried it, but without success, too stupid to see
    the trees for the forest I guess.... I get an error when I try to "load it".
    This is what I was trying ...it was late so maybe a typo or I was trying to
    climb the tree from the wrong side??


    (setq MASTER_LIST (load "layers.txt"))

    layers.txt looked like this....

    (quote
    ("S-TEXT" 1 "CONTINUOUS" "PLOT")
    )
     
    Chip Harper, Jun 23, 2004
    #7
  8. Chip Harper

    Chip Harper Guest

    Thanks to everyone for the help, I do have the String to List portion
    working.... I am still trying to figure out what I am doing wrong when
    trying Tony's suggestion however... :)
     
    Chip Harper, Jun 23, 2004
    #8
  9. Chip Harper

    Chip Harper Guest

    OK, got a couple of cups of coffee in me and started from scratch and I've
    got it! WooHoo.. time to go home, quit while I'm ahead. :)

    Thanks again to everyone.
     
    Chip Harper, Jun 23, 2004
    #9
  10. Tony Tanzillo, Jun 23, 2004
    #10
  11. Chip Harper

    Chip Harper Guest

    Must have been a typo somewhere last night Tony... got it licked this
    morning...Thanks :)
     
    Chip Harper, Jun 23, 2004
    #11
  12. Yes, if the delimiter is a space.


    Comando: (mapcar 'vl-princ-to-string (read (strcat "("
    "S-TEXT,1,CONTINUOUS,PLOT" ")")))
    ("S-TEXT,1,CONTINUOUS,PLOT")

    --

    Marc'Antonio Alessi
    http://xoomer.virgilio.it/alessi
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    --
    ")")));;;->("S-TEXT" "1" "CONTINUOUS" "PLOT")
     
    Marc'Antonio Alessi, Jun 23, 2004
    #12
  13. and shorter way but not faster:

    (defun SpacedString2List (SpdStr)
    (mapcar
    'vl-princ-to-string
    (read (strcat "(" SpdStr ")"))
    )
    )


    ALE_STRING2LIST > 100000 iterations: 4.04 secs.

    SPACEDSTRING2LIST > 100000 iterations: 5.32 secs.
     
    Marc'Antonio Alessi, Jun 23, 2004
    #13
  14. Chip Harper

    BillZ Guest

    I had just got to the point in a program that I needed this subr.
    Works perfect!
    Thanks

    Bill
     
    BillZ, Jun 24, 2004
    #14
  15. Chip Harper

    Chip Harper Guest

    Glad I was able to get an answer lined up and ready for you! :)
     
    Chip Harper, Jun 25, 2004
    #15
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.