Problem with vl-string-left-trim

Problem with vl-string-left-trim

Kh.mbkh
Advocate Advocate
1,162 Views
11 Replies
Message 1 of 12

Problem with vl-string-left-trim

Kh.mbkh
Advocate
Advocate

Having some data of this text form : "Aname = 1236.65" ; we want recover the value 1236.65.

the "Aname" my change ; But we're sure that "=" is still present.

 

vl-string-left-trim appears to be the perfect solution (according to it's definition):

 

Removes the specified characters from the beginning of a string
(vl-string-left-trim character-set string)
Arguments: character-set: A string listing the characters to be removed
string : The string to be stripped of character-set.
Return Values : A string containing a substring of string with all leading characters in character-set removed

 

 

326.PNG

 
 
Any idea !
0 Likes
Accepted solutions (1)
1,163 Views
11 Replies
Replies (11)
Message 2 of 12

diagodose2009
Collaborator
Collaborator
Accepted solution

Here my test

Command: (setq strcd "Aname = 1236.65")
"Aname = 1236.65"
Command: (setq ndda.exe (vl-string-search "= " strcd 1))
6
Command: (if (= ndda.exe nil) (alert "Missing Equal="))
Command: (atof (substr strcd (+ ndda.exe 2) 255))
1236.65
0 Likes
Message 3 of 12

hak_vz
Advisor
Advisor

 

Command: (vl-string-left-trim "Aname = "  "Aname = 1236.65")
"1236.65"

Or simply

(vl-string-left-trim prefix  str)
(atof (vl-string-left-trim prefix  str))

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 4 of 12

Kh.mbkh
Advocate
Advocate

No, the "Aname"  change, it my be "rttt = 11" !

0 Likes
Message 5 of 12

Kh.mbkh
Advocate
Advocate

OK, so Substr was the perfect solution! Thanks!!

0 Likes
Message 6 of 12

hak_vz
Advisor
Advisor

@Kh.mbkh wrote:

No, the "Aname"  change, it my be "rttt = 11" !


If you want to use function vl-string-left-trim to remove unwanted prefix you have to add in search string whole prefix, and not just "=". For that reason this functions is not practical in case like yours where prefix changes. 

You can use code provided by @diagodose2009  or here you have a function that converts string to list

 (defun string_to_list ( str del / pos )
        (if (setq pos (vl-string-search del str))
            (cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
            (list str)
        )
    ) 
(string_to_list "Aname = 1236.65" " ")
("Aname" "=" "1236.65")

To get number part of a string you use

Command: (atof (last(string_to_list "Aname = 1236.65" " ")))
1236.65

You can also use "=" as string delimiter since

Command: (atof (last(string_to_list "Aname = 1236.65" "=")))
1236.65

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 7 of 12

martti.halminen
Collaborator
Collaborator

You have a potential bug here (not related to the main matter):

 

AutoLISP reader stops reading a symbol name at encountering a period, with no warning.

 

So, instead of the variable 

ndda.exe

you actually using NDDA.  If that name is used for some other purpose elsewhere in the program, you will overwrite its value.

 

_$ (read "ndda.exe")
NDDA

 

Message 8 of 12

martti.halminen
Collaborator
Collaborator

The others have already handled how to do the trimming.

 

Your problem with vl-string-left-trim is that it only removes any of the characters in character-set at the left end of the string, it stops when encountering the first character not in the set.

 

_$ (vl-string-left-trim  "=" "========PE = 1256")
"PE = 1256"
0 Likes
Message 9 of 12

john.uhden
Mentor
Mentor

@hak_vz 

That's the way.

Somewhere around here is my old @str2list function that accepts a multicharacter delimiter, should anyone need it.

John F. Uhden

0 Likes
Message 10 of 12

john.uhden
Mentor
Mentor

@martti.halminen 

That's not to say, though, that symbol names can't contain a period.  They can (well at least in 2002).

Command: (snvalid "a.1") T

Command: (snvalid "ndd.exe") T

John F. Uhden

0 Likes
Message 11 of 12

martti.halminen
Collaborator
Collaborator

Here you are talking about a different matter, which unfortunately has the same name...

 

SNVALID and its relatives like TBLOBJNAME etc. handle AutoCAD symbol table objects.

Those are language independent, i.e. you could work with them similarly in VBA, C# or C++, too.

 

What I was talking about is Lisp symbols, i.e. those objects without quotation marks the Lisp language uses for referring to functions, variables etc.  Here the problem is that when going from raw source code to runtime internal representations, the program is handled via the READ function, which misbehaves with periods.

 

Common Lisp:

CLO 3 > (symbol-name (read-from-string "ndd.exe"))
"NDD.EXE"

 

Visual Lisp:

_$ (vl-symbol-name (read "ndd.exe"))
"NDD"

 

The worst part is that Visual Lisp (or AutoLISP, whichever name is preferred) does this without any warning, ignoring the rest of the name after the period, continuing reading the next object after that.

So you are quietly referring to a different symbol than you think when reading the program.

- the program works quite nicely, until you use another name that collides with this, then the results may be odd.

 

_$ (setq aa 42)
42
_$ (setq aa.1 'foo)
FOO
_$ (setq aa.78 "something")
"something"
_$ aa
"something"
_$ aa.1
"something"
_$ aa.78
"something"
_$ (vl-symbol-name 'aa.78)
"AA"
_$ (defun test () 66)
TEST
_$ (setq test.66 99999)
99999
_$ (test)
; error: bad function: 99999
_1$ 

 

0 Likes
Message 12 of 12

john.uhden
Mentor
Mentor
@Anonymous_halminen
Ahah!
Caveat programmor.

John F. Uhden

0 Likes