How could find each string from tab delimited string

Discussion in 'AutoCAD' started by shyam, Feb 21, 2004.

  1. shyam

    shyam Guest

    Hello,
    I have a tab delimited string such as ""jack\tis\ta\tgood\tboy"
    I would like to read and store each string such as "Jack", "is", "a", "good", "boy" in separate variables.
    A example lisp will be higly appreciated. Thank you.
     
    shyam, Feb 21, 2004
    #1
  2. You can store string in list with:

    ; 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:(ALE_String2List "jack\tis\ta\tgood\tboy" "\t")
    ("jack" "is" "a" "good" "boy")

    or:

    ;John Uhden, Cadlantic/formerly CADvantage
    ; delimiter = 1 or more characters

    (defun Str2List (str pat / i j n lst)
    (cond
    ((/= (type str)(type pat) 'STR))
    ((= str pat)'(""))
    (T
    (setq i 0 n (strlen pat))
    (while (setq j (vl-string-search pat str i))
    (setq lst (cons (substr str (1+ i)(- j i)) lst)
    i (+ j n)
    )
    )
    (reverse (cons (substr str (1+ i)) lst))
    )
    )
    )

    Then you can store items in list in variables with a foreach.
     
    Marc'Antonio Alessi, Feb 21, 2004
    #2
  3. There is a Parse function located at
    http://code.acadx.com/visuallisp/036.htm

    --
    R. Robert Bell, MCSE
    www.AcadX.com


    Hello,
    I have a tab delimited string such as ""jack\tis\ta\tgood\tboy"
    I would like to read and store each string such as "Jack", "is", "a",
    "good", "boy" in separate variables.
    A example lisp will be higly appreciated. Thank you.
     
    R. Robert Bell, Feb 21, 2004
    #3
  4. Hi Herman,

    my aplologies for delay:
    IMHO strings like "jack\tis\ta\tgood\tboy" and "this,is,,a,string,,foo"
    are originate from the reading of lines in files where the data are
    structured
    (database) and where every field is separated by a delimiter, if a field is
    empty it is represented by an null string in the resultant list.

    Then I think that either better that the lists in exit have an equal number
    of elements to have the rigth value of each field and postpone the possible
    elimination of the empty elements (null strings) during the following
    elaboration of the resultant lists.

    Example1:

    Name1 Name2 Age H W
    ----------------------------------
    Marco Alessi 47 1.70 80

    "Marco,Alessi,47,1.70,80"
    Marco Alessi is 47 years old, height 1.70 mt and weight is 80 Kg


    Example2:

    Name1 Name2 Age H W
    ----------------------------------
    Marco Alessi 1.70 80

    "Marco,Alessi,,1.70,80"
    Marco Alessi is 1.70 years old, height 80 mt and weight is nil Kg


    --

    Sorry for my english.


    Cheers.

    Marco
     
    Marc'Antonio Alessi, Feb 24, 2004
    #4
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.