Author Topic: need some suggestions on converting string to list  (Read 4854 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
need some suggestions on converting string to list
« on: April 05, 2012, 09:40:06 AM »
sorry for the title, i wasnt sure on what to name it.

im converting a string to a list using Alanjt's code listed here (thank you)
http://www.theswamp.org/index.php?topic=32845.0

im using a space as the separator, however if the item is a dimension like 6 1/2" and using the separator splits that into 2 list items, what can i do to not separate dimensions like that but still use the space as a separator to split my string into a list.. or is this just not possible?


Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: need some suggestions on converting string to list
« Reply #1 on: April 05, 2012, 10:02:31 AM »
I'm guessing this is related to this task:

http://www.theswamp.org/index.php?topic=41407.0

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #2 on: April 05, 2012, 10:17:38 AM »
yes Lee, it is but new topic

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: need some suggestions on converting string to list
« Reply #3 on: April 05, 2012, 10:22:05 AM »
Did Irneb not solve your problem?

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #4 on: April 05, 2012, 10:25:48 AM »
Did Irneb not solve your problem?

no ones really did. i have too many different scenarios for any of that to work. even the old code i was using didnt work properly.
so im starting over by breaking down a string to list then taking the number, converting it, then adding it back into the list and then convert the list back to a string.
thats the plan anyway

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #5 on: April 05, 2012, 10:36:01 AM »
Could you state a sample string? You'd probably need to find some way of distinguishing where the measurement starts and/or stops.

Another way could be to use the code I added here: http://www.theswamp.org/index.php?topic=41407.msg465217#msg465217

In particular the ExtractImperial function. That would extract only the dimension portion from the string. Thereafter you could split the string at the point where you find that and perform the normal str2lst idea on the 2 sides. Then simply an append.

The basis for this idea is contained in that thread as well. I mean the AddMetric method uses the string returned from ExtractImperial to search for the position in the original string - it simply uses vl-string-subst. You could use vl-string-search in the same way.
« Last Edit: April 05, 2012, 10:39:30 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #6 on: April 05, 2012, 10:57:29 AM »
I cannot figure why it "won't" work for you. Here's what I mean:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun ExtractImperial  (str / len n m c res)
  3.   (setq n 0 len (strlen str) res "")
  4.   (while (and (not (wcmatch res "*#',*#\"")) (< n len))
  5.     (while (and (<= (setq n (1+ n)) len) (not (wcmatch (substr str n 1) "[0-9]"))))
  6.     (setq m 0)
  7.     (while (and (<= (+ n (setq m (1+ m))) (1+ len)) (not (wcmatch (substr str n m) "*[~-'0-9/\" ]*"))))
  8.     (setq res (vl-string-trim " /-" (substr str n (1- m))) n (+ n m)))
  9.   (if (wcmatch res "*#',*#\"") res ""))
  10.  
  11. (defun str2lst (str / left dim pos split)
  12.   (defun split (str / )
  13.     (setq str (vl-string-translate " " "\002" (vl-string-trim " " str)))
  14.     (while (wcmatch str "*\002*") (setq str (vl-string-subst "\" \"" "\002" str)))
  15.     (read (strcat "(\"" str "\")")))
  16.   (vl-remove-if '(lambda (a) (eq a ""))
  17.     (if (not (eq (setq dim (ExtractImperial str)) ""))
  18.       (append (split (substr str 1 (setq pos (vl-string-search dim str)))) (list dim)
  19.               (str2lst (substr str (+ 1 pos (strlen dim)))))
  20.       (split str))))
Some test cases:
Code: [Select]
_$ (str2lst "Test Test Test 6'-5 2/3\"")
("Test" "Test" "Test" "6'-5 2/3\"")
_$ (str2lst "6'-5 2/3\"")
("6'-5 2/3\"")
_$ (str2lst "6'-5 2/3\" test test")
("6'-5 2/3\"" "test" "test")
_$ (str2lst "6'-5 2/3\"test test")
("6'-5 2/3\"" "test" "test")
_$ (str2lst "test6'-5 2/3\"test test")
("test" "6'-5 2/3\"" "test" "test")
_$ (str2lst "test 6'-5 2/3\" test test6'-5 2/3\"test test")
("test" "6'-5 2/3\"" "test" "test" "6'-5 2/3\"" "test" "test")
_$ (str2lst "Test Test Test 6'-5 2/3\" test")
("Test" "Test" "Test" "6'-5 2/3\"" "test")
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #7 on: April 05, 2012, 11:11:04 AM »
Could you state a sample string? You'd probably need to find some way of distinguishing where the measurement starts and/or stops.


yes i can. something like
four (4) @ 14 1/2" 90%%d apart

your coding works otherwise its when you throw the least expected string in there it doesnt convert.

i guess thats where i would need to find where the measure starts n stops

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #8 on: April 05, 2012, 11:14:37 AM »
i need to just separate the dimension and any number after it with a dash or a comma.   then it works.

i guess theses old dogs will just have to learn a new trick and change the way they do things

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #9 on: April 05, 2012, 11:18:58 AM »
spoke to soon

2 5/8" O.D. x 1 5/16" I.D.
it results in

2 5/8" (67mm) O.D. x 1 5/16" I.D.

leaves out the 1 5/16"

there is always something...  :-( :-o

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #10 on: April 06, 2012, 02:59:46 AM »
Ok, I thought that regular expressions should be used here. Seems it's not just more efficient, but also a lot more comprehensive.

I'm using the RX:Search function from my code here: http://caddons.svn.sourceforge.net/viewvc/caddons/Libraries/RegExp.LSP?revision=62&view=markup

Some tests:
Code: [Select]
_$ (RX:Search "four (4) @ 14 1/2\" 90%%d apart" "((\\d+'[ -]\\d \\d+/\\d+\")|(\\d+'[ -]\\d\")|(\\d+')|(\\d+ \\d+/\\d+\")|(\\d+/\\d+\"))")
((11 . "14 1/2\""))
_$ (RX:Search "2 5/8\" O.D. x 1 5/16\" I.D." "((\\d+'[ -]\\d \\d+/\\d+\")|(\\d+'[ -]\\d\")|(\\d+')|(\\d+ \\d+/\\d+\")|(\\d+/\\d+\"))")
((0 . "2 5/8\"") (14 . "1 5/16\""))
The 1st number is the start index where the pattern was found, the string is the portion matching the pattern. That would make it simpler to use substr to split the string into it's portions.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #11 on: April 06, 2012, 03:02:58 AM »
Sorry the regular expression search pattern should be changed to allow for the case where there's only an inch number shown (no feet and no fractions):
Quote
"((\\d+'[ -]\\d \\d+/\\d+\")|(\\d+'[ -]\\d\")|(\\d+')|(\\d+ \\d+/\\d+\")|(\\d+/\\d+\")|(\\d+\"))"
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #12 on: April 06, 2012, 03:25:42 AM »
Here's the AddMetric function - using the RX:Search:
Code: [Select]
(defun AddMetric (str /)
  (foreach val (reverse (RX:Search str "((\\d+'[ -]\\d \\d+/\\d+\")|(\\d+'[ -]\\d\")|(\\d+')|(\\d+ \\d+/\\d+\")|(\\d+/\\d+\")|(\\d+\"))"))
    (setq str (vl-string-subst (strcat (cdr val) " (" (rtos (cvunit (distof (cdr val) 4) "inch" "mm") 2 0) "mm)") (cdr val) str (car val))))
  str)

Seems to work fine now:
Code: [Select]
_$ (AddMetric "four (4) @ 14 1/2\" 90%%d apart")
"four (4) @ 14 1/2\" (368mm) 90%%d apart"
_$ (AddMetric "2 5/8\" O.D. x 1 5/16\" I.D.")
"2 5/8\" (67mm) O.D. x 1 5/16\" (33mm) I.D."
_$ (AddMetric "5'-2 5/8\" O.D. x 1 5/16\" I.D.")
"5'-2 5/8\" (1591mm) O.D. x 1 5/16\" (33mm) I.D."
_$ (AddMetric "5' 2 5/8\" O.D. x 1 5/16\" I.D.")
"5' 2 5/8\" (1591mm) O.D. x 1 5/16\" (33mm) I.D."
_$ (AddMetric "12'-6\"")
"12'-6\" (3810mm)"
_$ (AddMetric "12'-6\" dia circle")
"12'-6\" (3810mm) dia circle"
_$ (AddMetric "this is a 12'-6 1/2\" dia circle")
"this is a 12'-6 1/2\" (3823mm) dia circle"
_$ (AddMetric "this is a 12' dia circle")
"this is a 12' (3658mm) dia circle"
« Last Edit: April 06, 2012, 03:32:41 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #13 on: April 09, 2012, 08:44:43 AM »
it seems that if i use your search function from the link you gave, i need the rest of the functions also for it to work? it keeps asking for the other functions of your code to use the search function... or am i missing something in the translation?


andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #14 on: April 09, 2012, 09:17:06 AM »
ok so i think i figured this out, however it doesnt work for me like it did you

Command: (addmetric "2 5/8\" O.D. x 1 5/16\" I.D.")
"2 5/8\" (67mm) O.D. x 1 5/16\" I.D."


here is the code i have
Code: [Select]
;; conversion code by irneb from theswamp.org

(defun RX:Search (string pattern / lst col item)

   ;(if (RX:Start)
     ;(progn
       (vlax-Put *RX-Link* 'Pattern pattern) ;Set the pattern to match
       ;; Get the collection of match objects
       (setq col (vl-catch-all-apply 'vlax-Invoke (list *RX-Link* 'Execute string)))
       (if (not (vl-catch-all-error-p col))
         (progn
          ;; Loop through collection & place into return list
           (vlax-for item col
             ;; Get the position & length
             (setq lst (cons (cons (vlax-Get item 'FirstIndex) (vlax-Get item 'Value)) lst))
             (vlax-release-object item) ;Release the match object
           ) ;_ end of while
          (vlax-release-object col) ;Release the collection object
         ) ;_ end of progn
      ) ;_ end of if
       (reverse lst) ;Return list in correct order
     ;) ;_ end of progn
   ;) ;_ end of if
) ;_ end of defun
 


(defun ExtractImperial (str / len n m c res)
  (setq n 0 len (strlen str) res "")
  (while (and (not (wcmatch res "*#',*#\"")) (< n len))
    (while (and (<= (setq n (1+ n)) len) (not (wcmatch (substr str n 1) "[0-9]"))))
    (setq m 0)
    (while (and (<= (+ n (setq m (1+ m))) (1+ len)) (not (wcmatch (substr str n m) "*[~-'0-9/\" ]*"))))
    (setq res (vl-string-trim " /-" (substr str n (1- m))) n (+ n m)))
  (if (wcmatch res "*#',*#\"") res "")
)

(defun AddMetric (str /)
  (foreach val (reverse
  (RX:Search str "((\\d+'[ -]\\d \\d+/\\d+\")|(\\d+'[ -]\\d\")|(\\d+')|(\\d+ \\d+/\\d+\")|(\\d+/\\d+\")|(\\d+\"))"))
    (setq str (vl-string-subst (strcat (cdr val) " (" (rtos (cvunit (distof (cdr val) 4) "inch" "mm") 2 0) "mm)") (cdr val) str (car val)))) str)

; code from me
(DEFUN C:test ()

;
;; GET ENTITY
;
       (SETQ MT (ENTSEL "\nPLEASE PICK DESIRED TEXT"))
       (SETQ MTED (ENTGET (CAR MT)))
       (SETQ MTLL (ASSOC '1 MTED))
       (SETQ MTT (CDR MTLL))
   (setq MTT (addmetric MTT))
(SETQ TLN (CONS 1 MTT))
(SETQ TLNN (SUBST TLN MTLL MTED))
(ENTMOD TLNN)
(PRINC)
)
« Last Edit: April 09, 2012, 09:32:49 AM by andrew_nao »