ksperopoulos Posted November 20, 2013 Posted November 20, 2013 Is there an easy way to do this? I am trying to use vl-string-left-trim to remove all characters that are either a letter or a symbol. I am able to remove them as long as I know everything that will ever be in this string, but I don't know if I always will. I would rather have a safety net that would catch anything just in case it changes. Quote
Lee Mac Posted November 20, 2013 Posted November 20, 2013 Use vl-string->list to convert the string to a list of ASCII character codes, and then use vl-remove-if with an appropriate predicate function to process this list, removing those codes which do not meet your criteria, before using vl-list->string to convert the resulting list of ASCII codes back to a string. Here is an ASCII character code reference. Quote
BIGAL Posted November 21, 2013 Posted November 21, 2013 Lee what about your parse numbers.lsp but with ascii values set to be non numbers as an example of method. Quote
ksperopoulos Posted November 21, 2013 Author Posted November 21, 2013 Lee Mac - thank you for the response. I tried a similar method to what you are suggesting earlier today with no success. I got the ascii codes returned after running vl-string->list but when I try to use vl-remove-if, I get an error stating "bad argument type for compare". I know it is probably somehow linked to a mistake I am making trying to get the value to return "T" but my brain is fried. I think my searches are leading me down the right path on how to do things, but my inexperience keeps me from moving very quickly in that direction. I will pick this back up tomorrow. Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 (edited) An example ... (vl-list->string (vl-remove-if (function (lambda (u) (or (< 64 u 91) (< 96 u 123)))) (vl-string->list "Abcd12345XwZ"))) Edited November 21, 2013 by Tharwat Thanks Marko Quote
marko_ribar Posted November 21, 2013 Posted November 21, 2013 Look into highlighted number, Tharwat... (vl-list->string (vl-remove-if (function (lambda (u) (or (< 64 u 91) (< [highlight]96[/highlight] u 123)))) (vl-string->list "Abcd12345XwZ"))) Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 Look into highlighted number, Tharwat... Thanks Marko for the correction , Appreciated a lot . Quote
pBe Posted November 21, 2013 Posted November 21, 2013 An example ... Why not? (vl-list->string (vl-remove-if-[b]not[/b] (function (lambda (u) [b](< 47 u 58)[/b])) (vl-string->list str) ) ) Quote
pBe Posted November 21, 2013 Posted November 21, 2013 Vanilla (defun numbers1 (str / a b) (setq a "") (repeat (strlen str) (if (< 47 (ascii (setq b (substr str 1 1))) 58) (setq a (strcat a b)) ) (setq str (substr str 2)) ) a ) (defun numbers2 (str / a b) (repeat (strlen str) (if (< 47 (ascii (setq b (substr str 1 1))) 58) (setq a (cons b a)) ) (setq str (substr str 2)) ) (apply 'strcat (reverse a)) ) Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 Why not? Another ... (mapcar 'vl-list->string (list (vl-remove-if '(lambda (u) (not (member u '(48 49 50 51 52 53 54 55 56 57)) ) ) (vl-string->list "Abcd12345X7789wZ") ) ) ) Quote
Lee Mac Posted November 21, 2013 Posted November 21, 2013 Lee what about your parse numbers.lsp but with ascii values set to be non numbers as an example of method. That certainly provides another route for the OP to consider. Though, I'm not entirely sure of the desired result, i.e. whether the OP wishes to retain only integers or also retain hyphens & periods and hence decimal numbers also. Vanilla Nice one pBe Here is a recursive alternative: (defun numbers ( s / c ) (cond ( (wcmatch s "~*#*") "") ( (zerop (setq c (ascii s))) s) ( (< 47 c 58) (strcat (chr c) (numbers (substr s 2)))) ( (numbers (substr s 2))) ) ) Another ... (mapcar 'vl-list->string (list (vl-remove-if '(lambda (u) (not (member u '(48 49 50 51 52 53 54 55 56 57)) ) ) (vl-string->list "Abcd12345X7789wZ") ) ) ) I don't understand why you are using mapcar here? It just seems redundant as you seem to be constructing a list for the sole purpose of using mapcar in the code, which will then return a list rather than a string... Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 I don't understand why you are using mapcar here? Because the return of the (list (vl-remove-if ..... would be a list , and I know it is a little bit long one so we can shorten it to this without brackets all around as the previous one of course . (vl-list->string (vl-remove-if '(lambda (u) (not (member u '(48 49 50 51 52 53 54 55 56 57)) ) ) (vl-string->list "Abcd12345X7789wZ") ) ) Quote
Lee Mac Posted November 21, 2013 Posted November 21, 2013 Because the return of the (list (vl-remove-if ..... would be a list Exactly my point - the code is constructing an unnecessary list, and then processing this single-item list using mapcar - both the list and mapcar are entirely redundant as you have shown with the code in your next post. Quote
ksperopoulos Posted November 21, 2013 Author Posted November 21, 2013 The way Tharwat shows with all the ascii codes listed is the path I started down, but when I got to lambda, my brain couldn't fit anything else in for the day. I drank a couple beers, lost a few brain cells, and am back at it again today. Hopefully it will stick. Thanks for all the suggestions! BTW - I am looking at keeping all numbers and a period. Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 (vl-list->string (vl-remove-if '(lambda (u) (not (member u '([color=blue]32[/color] 48 49 50 51 52 53 54 55 56 57)) ) ) (vl-string->list "Abcd12345X7789wZ") ) ) BTW - I am looking at keeping all numbers and a period. Just by adding the number 32 would take care of the period factor Quote
ksperopoulos Posted November 21, 2013 Author Posted November 21, 2013 Just by adding the number 32 would take care of the period factor Wouldn't the ascii code be 46, not 32? Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 Wouldn't the ascii code be 46, not 32? If you are talking about the decimal point , then you are right but if it is a free space it should be 32 . Quote
ksperopoulos Posted November 21, 2013 Author Posted November 21, 2013 Sorry, decimal point (period) is what I meant. Thanks for clarifying. Quote
Tharwat Posted November 21, 2013 Posted November 21, 2013 Sorry, decimal point (period) is what I meant. Thanks for clarifying. You're welcome . Quote
ksperopoulos Posted November 21, 2013 Author Posted November 21, 2013 Does lambda need more than one argument. I keep getting an error that says there are too few arguments? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.