Beware vl-princ-to-string

Beware vl-princ-to-string

john.uhden
Mentor Mentor
696 Views
0 Replies
Message 1 of 1

Beware vl-princ-to-string

john.uhden
Mentor
Mentor

I thought this might make for an easier way to find and convert numeric strings to numbers.

I wanted to separate a phrase into a list of 3 components... '(Prefix # Suffix)

So I came up with this:

(defun @convert1 (str / pos prefix # #$ suffix)
     (setq pos 1)
     (while (not (wcmatch (substr str pos 1) "#"))
       (setq pos (1+ pos))
     )
     (setq prefix (substr str 1 (1- pos))
           # (read (substr str pos))
           #$ (vl-princ-to-string #)
           suffix (substr str (+ pos (strlen #$)))
     )
     (list prefix # suffix)
   )

Alas, (vl-princ-to-string 23.00) returns "23.0" not "23.00"

So...

(@convert1 "TC  23.00 X") returns ("TC " 23.0 "0 X") not ("TC " 23.00 " X")

 

Here's the workaround

(defun @convert2 (str / pos1 pos2 prefix #$ suffix)
     (setq pos1 1)
     (while (and (not (wcmatch (substr str pos1 1) "#"))(not (wcmatch (substr str pos1 2) "-#")))
       (setq pos1 (1+ pos1))
     )
     (setq prefix (substr str 1 (1- pos1)))
     (setq pos2 pos1)
     (while (wcmatch (substr str pos2 1) "-,`.,#")
       (setq pos2 (1+ pos2))
     )
     (setq #$ (substr str pos1 (- pos2 pos1))
           suffix (substr str (+ pos1 (strlen #$)))
     )
     (list prefix #$ suffix)
   )

Command: (@convert2 "TC 23.00 X")
("TC " "23.00" " X")

John F. Uhden

0 Likes
697 Views
0 Replies
Replies (0)