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

0 Members and 1 Guest are viewing this topic.

fixo

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

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to retrieve delimiter from string
« Reply #1 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?

BlackBox

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

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to retrieve delimiter from string
« Reply #3 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)

fixo

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

fixo

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

BlackBox

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

fixo

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

irneb

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

BlackBox

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

fixo

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

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to retrieve delimiter from string
« Reply #11 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.

fixo

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

BlackBox

  • King Gator
  • Posts: 3770
Re: How to retrieve delimiter from string
« Reply #13 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.  
"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 #14 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))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.