Author Topic: Get characters between "." and "," in a string... STUMPED  (Read 6383 times)

0 Members and 1 Guest are viewing this topic.

Eddie D.

  • Newt
  • Posts: 29
Get characters between "." and "," in a string... STUMPED
« on: September 13, 2016, 11:49:27 AM »
OK guys, this has me stumped.

I have a text string where I need to get the string of characters between the LAST period before a comma and the comma.

For example:
string = "686.TS.1,(686.18)" I need to return the "1" between the period and comma.
or
string= "687B.101,(687B.TS.3)"  I need to return the "101" between the period and comma.

Anything after the comma is not needed.

This is probably simple, but I'm having a brain fart I guess.


MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get characters between "." and "," in a string... STUMPED
« Reply #1 on: September 13, 2016, 12:00:50 PM »
Code: [Select]
(defun _Whargarbl ( text / i j temp result )
    (and
        (setq i (vl-string-position (ascii ",") text nil t))
        (setq j (vl-string-position (ascii ".") (setq temp (substr text 1 i)) nil t))
        (setq result (substr temp (+ 2 j)))
    )
    result
)

(_Whargarbl "686.TS.1,(686.18)") >> "1"
(_Whargarbl "687B.101,(687B.TS.3)") >> "101"
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Eddie D.

  • Newt
  • Posts: 29
Re: Get characters between "." and "," in a string... STUMPED
« Reply #2 on: September 13, 2016, 12:06:22 PM »
AWESOME! Thanks!

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Get characters between "." and "," in a string... STUMPED
« Reply #3 on: September 13, 2016, 12:29:49 PM »
Here's another :)

Code - Auto/Visual Lisp: [Select]
  1. (defun foo (string / i)
  2.   (while (setq i (vl-string-search "." string))
  3.     (setq string (substr string (+ 2 i)))
  4.     (and (setq i (vl-string-search "," string)) (setq string (substr string 1 i)))
  5.   )
  6.   string
  7. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Eddie D.

  • Newt
  • Posts: 29
Re: Get characters between "." and "," in a string... STUMPED
« Reply #4 on: September 13, 2016, 03:49:08 PM »
Thanks, Ronjon!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get characters between "." and "," in a string... STUMPED
« Reply #5 on: September 13, 2016, 04:45:23 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Get characters between "." and "," in a string... STUMPED
« Reply #6 on: September 16, 2016, 12:23:43 PM »
Or this...

Code - Auto/Visual Lisp: [Select]
  1. (defun parse-str ( str )  
  2.  (last(vle-string-split "." (car (vle-string-split "," str))))
  3.   )
  4.  

 (parse-str "686.TS.1,(686.18)") = "1"
 (parse-str "687B.101,(687B.TS.3)") = "101"

The VLE functions are built into Bricscad or can be loaded into ACAD. (See attachment, also see link for supported vle-functions)


https://www.bricsys.com/bricscad/help/en_US/V16/DevRef/
« Last Edit: September 16, 2016, 12:36:17 PM by snownut2 »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get characters between "." and "," in a string... STUMPED
« Reply #7 on: September 16, 2016, 12:40:21 PM »
Quick and dirty alternative to vle-string-split that honors the argument signature and behavior (I think):

Code: [Select]
(defun _StringSplit ( keys string )
    ;;  (_StringSplit ":,." "Quote: This too, shall pass.")
    ;;  >> ("Quote" " This too" " shall pass")
    (mapcar 'vl-list->string
        (   (lambda ( codes delims / token result )
                (foreach x codes
                    (if (member x delims)
                        (setq result (cons token result) token  nil)
                        (setq token (cons x token))
                    )
                )
                (cons token result)
            )
            (reverse (vl-string->list string))
            (vl-string->list keys)
        )
    )
)

Edit: Revised to leave in leading/trailing nils as may be legit.
« Last Edit: September 16, 2016, 02:31:44 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Get characters between "." and "," in a string... STUMPED
« Reply #8 on: September 16, 2016, 01:41:49 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (s / F1)
  2.   (defun f1 (l i)
  3.     (cond ((member i l) (f1 (cdr (member i l)) i))
  4.           (l)
  5.     )
  6.   )
  7.   (vl-list->string (f1 (reverse (f1 (reverse (vl-string->list s)) (ascii ","))) (ascii ".")))
  8. )

test:

Code - Auto/Visual Lisp: [Select]
  1. (f "686.TS.1,(686.18)") ; "1"
  2. (f "687B.101,(687B.TS.3)") ; "101"
  3. (f "686.TS.1,(686.18),") ; "1"
  4. (f "687B.101,(687B.TS.3),") ; "101"

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get characters between "." and "," in a string... STUMPED
« Reply #9 on: September 17, 2016, 04:29:44 PM »
Love the efficiency / brevity EE. :)

On StringSplit ... another alt (not efficient, but no matter) simply because it's fun (and good for the grey muscle) to skin a cat multiple ways:

Code: [Select]
(defun _StringSplit ( delims text )
    ;;  (_StringSplit ":;," "A:B,C;D")
    ;;  >> ("A" "B" "C" "D")
    (   (lambda ( foo delims / result p )
            (while (setq p (car (vl-sort (mapcar 'foo delims) '>)))
                (setq
                    result (cons (substr text (+ 2 p)) result)
                    text   (substr text 1 p)
                )
            )
            (cons text result)
        )
        (lambda (d) (vl-string-position d text nil t))
        (vl-string->list delims)
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Get characters between "." and "," in a string... STUMPED
« Reply #10 on: September 19, 2016, 03:15:14 AM »
my version:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (d s / i)
  2.   ;;(setq d ":;," s "A:B,C;D")
  3.   ;;(f d s)
  4.   (cond ((= d "") (list s))
  5.         ((setq i (vl-string-position (ascii d) s))
  6.          (append (f (substr d 2) (substr s 1 i)) (f d (substr s (+ 2 i))))
  7.         )
  8.         ((f (substr d 2) s))
  9.   )
  10. )

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get characters between "." and "," in a string... STUMPED
« Reply #11 on: September 19, 2016, 08:48:42 AM »
[like]
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get characters between "." and "," in a string... STUMPED
« Reply #12 on: September 19, 2016, 10:46:16 AM »
Another:

Code: [Select]
(defun _StringSplit ( keys string )
    ;;  (_StringSplit ":;," "A:B,C;D")
    ;;  >> ("A" "B" "C" "D")
    (   (lambda ( delims / i result )
            (repeat (setq i (strlen string))
                (if (member (vl-string-elt string (setq i (1- i))) delims)
                    (setq
                        result (cons (substr string (+ 2 i)) result)
                        string (substr string 1 i)
                    )
                )
            )
            (cons string result)
        )
        (vl-string->list keys)
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Get characters between "." and "," in a string... STUMPED
« Reply #13 on: September 19, 2016, 12:45:27 PM »
...very rudimentary (I modified an old function)...
Code: [Select]
(defun ALE_String_ToListByKeys (InpStr KysStr / NewKys CarDlm SttPos EndPos TmpLst)
  (setq
    CarDlm (substr KysStr 1 1)  NewKys CarDlm
    NewKys (repeat (strlen KysStr) (setq NewKys (strcat CarDlm NewKys)))
    InpStr (vl-string-translate KysStr NewKys InpStr)
    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))
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Get characters between "." and "," in a string... STUMPED
« Reply #14 on: September 19, 2016, 12:50:26 PM »
new version:
Code - Auto/Visual Lisp: [Select]
  1. (defun f1 (d s)
  2.   (cond ((= d "") (read (strcat "(\"" s "\")")))
  3.         ((wcmatch s (strcat "*`" (substr d 1 1) "*")) (f1 d (vl-string-subst "\"\"" (substr d 1 1) s)))
  4.         ((f1 (substr d 2) s))
  5.   )
  6. )