Author Topic: How to retrieve delimiter from string  (Read 2250 times)

0 Members and 1 Guest are viewing this topic.

BlackBox

  • King Gator
  • Posts: 3745
Re: How to retrieve delimiter from string
« Reply #15 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:
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3745
Re: How to retrieve delimiter from string
« Reply #16 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:
"How we think determines what we do, and what we do determines what we get."

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to retrieve delimiter from string
« Reply #17 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))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

fixo

  • Guest
Re: How to retrieve delimiter from string
« Reply #18 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 :)