TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: fixo on April 10, 2013, 12:56:00 PM

Title: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 12:56:00 PM
Guys,
I hope that someone has a full code, it is necessary for me for urgent work
till this night, or the request is to give me a links to a similar question

I will try to explain an existing situation

Say, i have the delimited text file of such contents:
(comma delimited ending with comma)
"ST1,1022.519,927.347,100.522,"
"KON1,1022.519,927.347,100.522,"
"2,1022.519,927.347,100.522,"
It can be of such contents
(comma delimited without ending char)
"ST1,1022.519,927.347,100.522"
"KON1,1022.519,927.347,100.522"
"2,1022.519,927.347,100.522"
Or even the such
(semicolon delimited ending with comma)
"2;1022.519;927.347;100.522,"
"ST1;1022.519;927.347;100.522,"
"KON1;1022.519;927.347;100.522,"
Or still the such
(semicolon delimited  ending with semicolon)
"2;1022.519;927.347;100.522;"
"ST1;1022.519;927.347;100.522;"
"KON1;1022.519;927.347;100.522;"

ETC... ETC..

Even line may starts from number or alpha-numeric part

The question is how to retrive a separator and to remove
ending separator in every line ( if this exists )
at runtime, in one shot only

What I have stuck on yet
Code: [Select]
(defun check_del (str_line /)
  (cond
    ((eq 44 (last (vl-string->list str_line ))) ",")
    ((eq 58 (last (vl-string->list str_line ))) ":")
    ((eq 59 (last (vl-string->list str_line ))) ";")   
    ((eq 9 (last (vl-string->list str_line ))) "\t")
    ((eq 0 (last (vl-string->list str_line )))
     (substr (vl-string-trim "0123456789" str_line) 1 1));it's working good, if the first item is numeric only
       (T (alert "Impossible to read your text\nAsk your dork programmer about.")(exit)))
  )

Hmm, I have just 4 hours to finish this work :)
Title: Re: How to retrieve delimiter from string
Post by: Marc'Antonio Alessi on April 10, 2013, 01:25:09 PM
you just need to remove the last not numeric character or you must also know the separator?
Title: Re: How to retrieve delimiter from string
Post by: BlackBox on April 10, 2013, 01:30:09 PM
Code: [Select]
(defun _GetDelim (s) (substr s (strlen s) 1))

(defun _RemEndDelim (lst / d)
  (if (setq d (_GetDelim (car lst)))
    (mapcar
      (function
        (lambda (x)
          (vl-string-right-trim d x)
        )
      )
      lst
    )
  )
)

Example:

Code: [Select]
_$ (_RemEndDelim (list "ST1,1022.519,927.347,100.522," "KON1,1022.519,927.347,100.522," "2,1022.519,927.347,100.522,"))
("ST1,1022.519,927.347,100.522" "KON1,1022.519,927.347,100.522" "2,1022.519,927.347,100.522")
_$
Title: Re: How to retrieve delimiter from string
Post by: Marc'Antonio Alessi on April 10, 2013, 01:33:26 PM
Code: [Select]
(defun _GetDelim (s) (substr s (strlen s) 1))

(defun _RemEndDelim (lst / d)
  (if (setq d (_GetDelim (car lst)))
    (mapcar
      (function
        (lambda (x)
          (vl-string-right-trim d x)
        )
      )
      lst
    )
  )
)

Example:

Code: [Select]
_$ (_RemEndDelim (list "ST1,1022.519,927.347,100.522," "KON1,1022.519,927.347,100.522," "2,1022.519,927.347,100.522,"))
("ST1,1022.519,927.347,100.522" "KON1,1022.519,927.347,100.522" "2,1022.519,927.347,100.522")
_$

>> (comma delimited without ending char)
Title: Re: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 01:34:43 PM
you just need to remove the last not numeric character or you must also know the separator?
Hi Marco, glad to see you :)
Yes I want to get separator and remove it at the end
if this is in there, otherwise just to know separator
Title: Re: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 01:45:45 PM
Code: [Select]
(defun _GetDelim (s) (substr s (strlen s) 1))

(defun _RemEndDelim (lst / d)
  (if (setq d (_GetDelim (car lst)))
    (mapcar
      (function
        (lambda (x)
          (vl-string-right-trim d x)
        )
      )
      lst
    )
  )
)

Example:

Code: [Select]
_$ (_RemEndDelim (list "ST1,1022.519,927.347,100.522," "KON1,1022.519,927.347,100.522," "2,1022.519,927.347,100.522,"))
("ST1,1022.519,927.347,100.522" "KON1,1022.519,927.347,100.522" "2,1022.519,927.347,100.522")
_$

Thanks so much, your function _RemEndDelim working good
on my files, but function _GetDelim is not good for me,
some time it return number instead of delimiter,
anyway thanks, friend, most part of work I'll be finish
Title: Re: How to retrieve delimiter from string
Post by: BlackBox on April 10, 2013, 01:58:48 PM
Thanks so much, your function _RemEndDelim working good
on my files, but function _GetDelim is not good for me,
some time it return number instead of delimiter,
anyway thanks, friend, most part of work I'll be finish

The _GetDelim function only returns the last character of the sample string. If the sample string does not include a deliminator, then still, the last character is returned.

To qualify that the deliminator returned is in fact a known deliminator, consider this adaptation:

Code: [Select]
(defun _RemEndDelim (lst)
  (mapcar
    (function
      (lambda (x)
        (vl-string-right-trim
          ","
          (vl-string-right-trim ";" x)
        )
      )
    )
    lst
  )
)

Example:
Code - Text: [Select]
  1. _$ (_RemEndDelim '("ST1,1022.519,927.347,100.522," "KON1,1022.519,927.347,100.522," "2,1022.519,927.347,100.522,"))
  2. ("ST1,1022.519,927.347,100.522" "KON1,1022.519,927.347,100.522" "2,1022.519,927.347,100.522")
  3.  
  4. _$ (_RemEndDelim '("ST1,1022.519,927.347,100.522" "KON1,1022.519,927.347,100.522" "2,1022.519,927.347,100.522"))
  5. ("ST1,1022.519,927.347,100.522" "KON1,1022.519,927.347,100.522" "2,1022.519,927.347,100.522")
  6.  
  7. _$ (_RemEndDelim '("2;1022.519;927.347;100.522," "ST1;1022.519;927.347;100.522," "KON1;1022.519;927.347;100.522,"))
  8. ("2;1022.519;927.347;100.522" "ST1;1022.519;927.347;100.522" "KON1;1022.519;927.347;100.522")
  9.  
  10. _$ (_RemEndDelim '("2;1022.519;927.347;100.522;" "ST1;1022.519;927.347;100.522;" "KON1;1022.519;927.347;100.522;"))
  11. ("2;1022.519;927.347;100.522" "ST1;1022.519;927.347;100.522" "KON1;1022.519;927.347;100.522")
  12. _$
  13.  
Title: Re: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 02:15:37 PM
Thanks again it's working good :)

Hve just one issue with _GetDelim, eg:

(_GetDelim "KON1\t1022.519\t927.347\t100.522");<-- return 2

(_GetDelim "KON1\t1022.519\t927.347\t100.522\t");<-- return \t , good one
Title: Re: How to retrieve delimiter from string
Post by: irneb on April 10, 2013, 02:33:07 PM
Try this one perhaps:
Code - Auto/Visual Lisp: [Select]
  1. (defun findDelimeter (input / )
  2.   (substr (vl-string-left-trim ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" (strcase input)) 1 1))
Title: Re: How to retrieve delimiter from string
Post by: BlackBox on April 10, 2013, 02:34:15 PM
Thanks again it's working good :)

Hve just one issue with _GetDelim, eg:

(_GetDelim "KON1\t1022.519\t927.347\t100.522");<-- return 2

(_GetDelim "KON1\t1022.519\t927.347\t100.522\t");<-- return \t , good one

Fixo, there's no need for _GetDelim anymore with my last code.  :-)

However, as I did not account for "\t" you might find this more useful:

Code - Auto/Visual Lisp: [Select]
  1. (defun _RemEndDelim (lst / trim)
  2.  
  3.   (defun trim (s)
  4.     (foreach char '("," ";" "\t" "\n" "\p") ;;<-- You set the list to remove here
  5.       (setq s (vl-string-right-trim char s))
  6.     )
  7.     s
  8.   )
  9.  
  10.   (mapcar (function (lambda (x) (trim x))) lst)
  11. )
  12.  

Example:
Code - Text: [Select]
  1. _$ (_RemEndDelim '("2;1022.519;927.347;100.522\t" "ST1;1022.519;927.347;100.522," "ST1;1022.519;927.347;100.522" "KON1;1022.519;927.347;100.522;"))
  2. ("2;1022.519;927.347;100.522" "ST1;1022.519;927.347;100.522" "ST1;1022.519;927.347;100.522" "KON1;1022.519;927.347;100.522")
  3. _$
  4.  

** Note that there is "\t" "," ";" and no delim used in same list.
Title: Re: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 02:43:38 PM
Thanks again
I thought the same way
That is I ended up with:
Code: [Select]
(defun _GetDelim (str_line / dels)
  (setq dels (list 9 44 58 59))
  (car
    (vl-remove-if-not
      '(lambda (x)
(member x dels)
       ) ;_ end of lambda
      (vl-string->list str_line)
    ) ;_ end of vl-remove-if-not
  ) ;_ end of car
) ;_ end of defun

The problem was resolved, thanks to all, guys :)
Title: Re: How to retrieve delimiter from string
Post by: Marc'Antonio Alessi on April 10, 2013, 02:55:00 PM
Hi Marco, glad to see you :)
...
The problem was resolved, thanks to all, guys :)
...
here I am after dinner (my wife is severe)...
I glad you solved, good night.
Title: Re: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 03:08:27 PM
Try this one perhaps:
Code - Auto/Visual Lisp: [Select]
  1. (defun findDelimeter (input / )
  2.   (substr (vl-string-left-trim ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" (strcase input)) 1 1))
Thanks Irne`
findDelimeter woks nice too,
sorry for the bealting

Regards to all, guys :)
What the nice forum !
Title: Re: How to retrieve delimiter from string
Post by: BlackBox on April 10, 2013, 03:18:08 PM
I'm glad you got this sorted, Fixo... I guess I was just confused.

Based on this:
The question is how to retrive a separator and to remove
ending separator in every line ( if this exists )
at runtime, in one shot only



Your code:
Code - Auto/Visual Lisp: [Select]
  1. (defun _GetDelim (str_line / dels)
  2.   (setq dels (list 9 44 58 59))
  3.   (car
  4.     (vl-remove-if-not
  5.       '(lambda (x)
  6.          (member x dels)
  7.        ) ;_ end of lambda
  8.       (vl-string->list str_line)
  9.     ) ;_ end of vl-remove-if-not
  10.   ) ;_ end of car
  11. )
  12.  

Returns:
Code - Text: [Select]
  1. _$ (_GetDelim "ST1,1022.519,927.347,100.522,")
  2. 44
  3.  

... You've only identified the deliminator, and still must remove it from the list of strings.



My code:
Code - Auto/Visual Lisp: [Select]
  1. (defun _RemEndDelim (lst / trim)
  2.  
  3.   (defun trim (s)
  4.     (foreach char '("," ";" "\t" "\n" "\p") ;;<-- You set the list to remove here
  5.       (setq s (vl-string-right-trim char s))
  6.     )
  7.     s
  8.   )
  9.  
  10.   (mapcar (function (lambda (x) (trim x))) lst)
  11. )
  12.  

Returns:
Code - Text: [Select]
  1. _$ (_RemEndDelim '("2;1022.519;927.347;100.522\t" "ST1;1022.519;927.347;100.522\t" "KON1;1022.519;927.347;100.522\t"))
  2. ("2;1022.519;927.347;100.522" "ST1;1022.519;927.347;100.522" "KON1;1022.519;927.347;100.522")
  3. _$
  4.  

My code has already removed all deliminators, for each string in the entire list.



Speed test:
Code - Auto/Visual Lisp: [Select]
  1. (bench '(_GetDelim)
  2.        '("ST1,1022.519,927.347,100.522,")
  3.        10000
  4. )
  5.  
  6. (bench '(_RemEndDelim)
  7.        '(("2;1022.519;927.347;100.522,"
  8.           "ST1;1022.519;927.347;100.522;"
  9.           "ST1;1022.519;927.347;100.522;"
  10.           "KON1;1022.519;927.347;100.522\t"
  11.          )
  12.         )
  13.        10000
  14. )
  15.  

Results:
Code - Text: [Select]
  1. _GETDELIM
  2. Elapsed: 608
  3. Average: 0.0608
  4. _$
  5.  
  6. _REMENDDELIM
  7. Elapsed: 390
  8. Average: 0.0390
  9.  
  10.  
  11. _$
  12.  
Title: Re: How to retrieve delimiter from string
Post by: irneb on April 10, 2013, 03:28:45 PM
@BlackBox: Why the foreach? You could accomplish the same thing thus:
Code - Auto/Visual Lisp: [Select]
  1. (defun _RemEndDelim (lst)
  2.   (mapcar (function (lambda (str) (vl-string-right-trim ",;\t\n\p" str))) lst))
Title: Re: How to retrieve delimiter from string
Post by: BlackBox on April 10, 2013, 03:30:04 PM
@BlackBox: Why the foreach? You could accomplish the same thing thus:
Code - Auto/Visual Lisp: [Select]
  1. (defun _RemEndDelim (lst)
  2.   (mapcar (function (lambda (str) (vl-string-right-trim ",;\t\n\p" str))) lst))

Oh, well that's easy... You forgot that I am inept, my Zef slang bro.  :lmao:
Title: Re: How to retrieve delimiter from string
Post by: BlackBox on April 10, 2013, 03:34:42 PM
Revised function:
Code - Auto/Visual Lisp: [Select]
  1. (defun _RemEndDelim (lst)
  2.   (mapcar (function (lambda (x) (vl-string-right-trim ",;\t\n\p" x)))
  3.           lst
  4.   )
  5. )
  6.  

Revised speed test results:
Code - Text: [Select]
  1. _GETDELIM
  2. Elapsed: 624
  3. Average: 0.0624
  4.  
  5.  
  6. _$
  7.  
  8. _REMENDDELIM
  9. Elapsed: 78
  10. Average: 0.0078
  11.  
  12.  
  13. _$
  14.  

... It's just a bit faster, thanks to Irne. :beer:
Title: Re: How to retrieve delimiter from string
Post by: irneb on April 10, 2013, 04:05:55 PM
Whaaa ha ha! Inept can be applied to me in some cases too .... no make that all cases depending on which way my head is screwed on at the moment ;)

Anyhow fixo, why would you need to "know" what the delimiter is? Usually with these delimited strings you simply want to split them into usable items.
Code - Auto/Visual Lisp: [Select]
  1. (defun split-string (input / dPos)
  2.   (if (setq dPos (vl-remove-if 'null (mapcar (function (lambda (del) (vl-string-position del input))) '(44 59 9 10 112))))
  3.     (cons (substr input 1 (setq dPos (apply 'min dPos))) (split-string (substr input (+ 2 dPos))))
  4.     (list input)))
  5.  
  6. (defun RemEndDelim (lst)
  7.   (mapcar (function (lambda (str) (vl-string-right-trim ",;\t\n\p" str))) lst))
  8.  
  9. (defun split-string-list (input)
  10.   (mapcar (function (lambda (line) (split-string (RemEndDelim line)))) input))
If you need to keep track of the delimiter so you can then later save back the list into a similar file, then by all means use my findDelimiter function:
Code - Auto/Visual Lisp: [Select]
  1. (defun findDelimeter (input / )
  2.   (substr (vl-string-left-trim ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" (strcase input)) 1 1))
  3.  
  4. (defun read-delim-file (fileName / file line lines)
  5.   (if (setq file (open fileName "r"))
  6.     (progn (while (setq line (read-line file)) (setq lines (cons line lines)))
  7.       (close file)
  8.       (cons (findDelimeter (car lines)) (split-string-list (reverse lines))))))
  9.  
  10. (defun write-delim-file (dataList fileName / file)
  11.   (if (setq file (open fileName "w"))
  12.     (progn (foreach line (cdr dataList)
  13.              (princ (car line) file)
  14.              (foreach item (cdr line)
  15.                (princ (car dataList) file)
  16.                (princ item file))
  17.              (princ "\n" file))
  18.       (close file))))
Title: Re: How to retrieve delimiter from string
Post by: fixo on April 10, 2013, 04:20:50 PM
Quote
Anyhow fixo, why would you need to "know" what the delimiter is? Usually with these delimited strings you simply want to split them into usable items.
The reason is on some files were badly formatted, I almost finish my work on today,
Many thanks to all and regards :)