yonderbob Posted October 30, 2015 Share Posted October 30, 2015 Hi guys, I've been trying to get the savepath directory of my current drawing into a variable so i can modify it's string to only get the numbers out of the string. I have had no succes with "VL-filename-directory" keep getting a bad retrun. Also tried the Getenv path but that returns autocad install path which is not what i'm looking for. This is want i have so far (setq Path (getenv "path")) (setq LastPath (vl-string-trim "Z:\whatever" path)) (setq NoTroep (vl-list->string (vl-remove-if-not '(lambda (i) (< 47 i 58)) (vl-string->list path) ) ) ) Hope any of you could point me in the right direction. Thanks Bob Quote Link to comment Share on other sites More sharing options...
hmsilva Posted October 30, 2015 Share Posted October 30, 2015 Try (getvar 'dwgprefix) Henrique Quote Link to comment Share on other sites More sharing options...
yonderbob Posted October 30, 2015 Author Share Posted October 30, 2015 Hmsilva Thank!!! Works better then i expected , so simple too, saves some string alterations Bob Quote Link to comment Share on other sites More sharing options...
hmsilva Posted October 30, 2015 Share Posted October 30, 2015 You're welcome, Bob Glad I could help Henrique Quote Link to comment Share on other sites More sharing options...
yonderbob Posted October 30, 2015 Author Share Posted October 30, 2015 It came with some issues with reading multiple search paths so i want to make an easier code if that's possible. Is it possible to get a string return from within a string that only contains a set amount of numbers. For example, the string is : "i have 12 eggs but i need 654321 eggs so i still needs 54321 more" I would like it to return only the string of numbers which contains 6 numbers, so it only returns the 654321 numbers in the string. Hope you get me Thanks, Bob Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2015 Share Posted October 30, 2015 Is it possible to get a string return from within a string that only contains a set amount of numbers.For example, the string is : "i have 12 eggs but i need 654321 eggs so i still needs 54321 more" I would like it to return only the string of numbers which contains 6 numbers, so it only returns the 654321 numbers in the string. You could use Regular Expressions, or alternatively a function similar to the following: (defun getpattern ( str pat / tmp ) (cond ( (= "" str) nil) ( (or (wcmatch (setq tmp (substr str 1 (1- (strlen str)))) pat) (wcmatch (setq tmp (substr str 2)) pat) ) (getpattern tmp pat) ) ( (wcmatch str pat) str) ) ) Example: _$ (setq s "i have 12 eggs but i need 654321 eggs so i still needs 54321 more") "i have 12 eggs but i need 654321 eggs so i still needs 54321 more" _$ (getpattern s "*######*") "654321" Quote Link to comment Share on other sites More sharing options...
hmsilva Posted October 30, 2015 Share Posted October 30, 2015 You could use Regular Expressions, or alternatively a function similar to the following: (defun getpattern ( str pat / tmp ) (cond ( (= "" str) nil) ( (or (wcmatch (setq tmp (substr str 1 (1- (strlen str)))) pat) (wcmatch (setq tmp (substr str 2)) pat) ) (getpattern tmp pat) ) ( (wcmatch str pat) str) ) ) Nice function, Lee! I would use something like this (defun 6numbers (str / lst num str1) (setq lst (mapcar 'chr (vl-string->list str)) str1 "" ) (foreach l lst (if (wcmatch l "#") (setq str1 (strcat str1 l)) (if (and (/= str1 "") (= (strlen str1) 6) ) (setq num str1) (setq str1 "") ) ) ) num ) only accept as valid result, a string with six consecutive numbers, if more, does not validate. Henrique Quote Link to comment Share on other sites More sharing options...
yonderbob Posted October 30, 2015 Author Share Posted October 30, 2015 Wow Lee, Your function goes way beyond my understanding, don't get it logically at all haha. Your expression makes sense and looks nice, So thanks for those. Henrigue, I like yours as i understand it haha . Thanks guys for the help and lessons appreciate it alot. Time to build now Bob Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2015 Share Posted October 30, 2015 Nice function, Lee! Thank you Henrique Here's another way: (defun get6digits ( str / foo ) (defun foo ( lst / rtn ) (if (setq rtn (mapcar '(lambda ( a b ) a) lst '(0 0 0 0 0 0))) (if (vl-every '(lambda ( x ) (< 47 x 58)) rtn) (vl-list->string rtn) (foo (cdr lst)) ) ) ) (foo (vl-string->list str)) ) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2015 Share Posted October 30, 2015 Wow Lee,Your function goes way beyond my understanding, don't get it logically at all haha. Your expression makes sense and looks nice, So thanks for those. Thank you Bob - the function essentially shortens the string from either end until the supplied wildcard pattern is no longer validated, leaving the section of string matching the pattern. Quote Link to comment Share on other sites More sharing options...
hmsilva Posted October 30, 2015 Share Posted October 30, 2015 Thank you Henrique Here's another way: (defun get6digits ( str / foo ) (defun foo ( lst / rtn ) (if (setq rtn (mapcar '(lambda ( a b ) a) lst '(0 0 0 0 0 0))) (if (vl-every '(lambda ( x ) (< 47 x 58)) rtn) (vl-list->string rtn) (foo (cdr lst)) ) ) ) (foo (vl-string->list str)) ) Nice one too, Lee! Cheers Henrique Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2015 Share Posted October 30, 2015 Nice one too, Lee! Cheers Henrique Thanks! - Though, this will be a lot more efficient: (defun get6digits2 ( str / foo ) (defun foo ( lst / tmp ) (if (setq lst (vl-member-if '(lambda ( x ) (< 47 x 58)) lst)) (if (wcmatch (setq tmp (vl-list->string lst)) "######*") (substr tmp 1 6) (foo (cdddr (cdddr lst))) ) ) ) (foo (vl-string->list str)) ) Benchmark: _$ (setq s "i have 12 eggs but i need 654321 eggs so i still needs 54321 more") "i have 12 eggs but i need 654321 eggs so i still needs 54321 more" Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s): (GET6DIGITS2 S)................1123 / 12.27 <fastest> (6NUMBERS S)...................1903 / 7.24 (GETPATTERN S "*######*")......3339 / 4.13 (GET6DIGITS S)................13775 / 1.00 <slowest> Quote Link to comment Share on other sites More sharing options...
hmsilva Posted October 30, 2015 Share Posted October 30, 2015 Thanks! - Though, this will be a lot more efficient: Far more efficient! Henrique Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2015 Share Posted October 30, 2015 Far more efficient! Henrique This should become a 'challenge' thread Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2015 Share Posted October 30, 2015 There is still room for improvement! (defun get6digits3 ( str / l n x ) (setq l (vl-string->list str)) (while (and (setq x (car l)) (< (length (setq n (if (< 47 x 58) (cons x n)))) 6) (setq l (cdr l)) ) ) (vl-list->string (reverse n)) ) _$ (benchmark '((get6digits2 s) (get6digits3 s))) Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s): (GET6DIGITS3 S).....1700 / 4.98 <fastest> (GET6DIGITS2 S).....8471 / 1.00 <slowest> _$ (benchmark '((getpattern s "*######*") (6numbers s) (get6digits s) (get6digits2 s) (get6digits3 s))) Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s): (GET6DIGITS3 S)................1903 / 20.82 <fastest> (GET6DIGITS2 S)................3698 / 10.71 (6NUMBERS S)...................6224 / 6.37 (GETPATTERN S "*######*").....10358 / 3.83 (GET6DIGITS S)................39624 / 1.00 <slowest> Quote Link to comment Share on other sites More sharing options...
hmsilva Posted October 30, 2015 Share Posted October 30, 2015 There is still room for improvement! _$ (benchmark '((getpattern s "*######*") (6numbers s) (get6digits s) (get6digits2 s) (get6digits3 s))) Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s): (GET6DIGITS3 S)................1903 / 20.82 <fastest> (GET6DIGITS2 S)................3698 / 10.71 (6NUMBERS S)...................6224 / 6.37 (GETPATTERN S "*######*").....10358 / 3.83 (GET6DIGITS S)................39624 / 1.00 <slowest> Always in search of perfection! Nicely done! Henrique Quote Link to comment Share on other sites More sharing options...
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.