Author Topic: I'm not understanding what is going on here.  (Read 622 times)

0 Members and 1 Guest are viewing this topic.

Eddie D.

  • Newt
  • Posts: 28
I'm not understanding what is going on here.
« on: September 09, 2019, 01:21:15 PM »
I'm getting an unexpected result from vl-string-left-trim that I don't understand.


This works is as expected:
(vl-string-left-trim  "PREFIX: " "PREFIX: ABCDEF")
Result: "ABCDEF"


This result has me confused:
(vl-string-left-trim  "PREFIX: " "PREFIX: FEDCBA")
Result: "DCBA"


I get a similar unexpected result if the beginning of the expected return string begins with E, F, I, P, R, and X.
What am I missing here?


efernal

  • Newt
  • Posts: 196
Re: I'm not understanding what is going on here.
« Reply #1 on: September 09, 2019, 01:28:40 PM »
Command: (vl-string-left-trim  "PREFIX: " "PREFIX: FEDCBA")
"DCBA"

same here...
e.fernal

efernal

  • Newt
  • Posts: 196
Re: I'm not understanding what is going on here.
« Reply #2 on: September 09, 2019, 01:31:47 PM »
vl-string-trim


Removes the specified characters from the beginning and end of a string

(vl-string-trim char-set str)

Arguments

char-set

A string listing the characters to be removed.

str

The string to be trimmed of char-set.

Return Values

The value of str, after any characters have been trimmed.

Examples

_$ (vl-string-trim " \t\n" " \t\n STR \n\t ")

"STR"

_$ (vl-string-trim "this is junk" "this is junk Don't call this
junk! this is junk")

"Don't call this junk!"

_$ (vl-string-trim " " " Leave me alone ")

"Leave me alone"
e.fernal

MP

  • Seagull
  • Posts: 17400
Re: I'm not understanding what is going on here.
« Reply #3 on: September 09, 2019, 01:46:38 PM »
To add to what efernal posted ...

There's a number of ways to achieve (what I believe is) your intended result.

Observe what results from the following code snips which represent one of several ways:

(setq text "PREFIX: FEDCBA")

(if (< 0 (setq i (vl-string-mismatch "PREFIX: " text)))
    (substr text (1+ i))
    text
)


>>  "FEDCBA"

(setq text "prefix: fedcba")

(if (< 0 (setq i (vl-string-mismatch "PREFIX: " text)))
    (substr text (1+ i))
    text
)


>>  "prefix: fedcba"

(setq text "prefix: fedcba")

(if (< 0 (setq i (vl-string-mismatch "PREFIX: " (strcase text))))
    (substr text (1+ i))
    text
)


>>  "fedcba"


You may wish to review the documentation for these functions:

vl-string-trim:
http://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-FF1DA441-C9D8-4C99-BC6E-B2A72B562389

vl-string-subst:
http://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-D8EE91DC-D4DB-43E0-9AFE-5FA166C0896D

vl-string-mismatch:
http://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-D1EA6F47-146E-44BC-BBCA-40B7A874C0B8

etc.

Cheers.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Eddie D.

  • Newt
  • Posts: 28
Re: I'm not understanding what is going on here.
« Reply #4 on: September 09, 2019, 01:58:09 PM »
Thanks for the replies, guys!


efernal, I get the same results with vl-string-trim.


MP, thanks for the examples, but I still don't understand why I get the results I'm getting with vl-string-trim/vl-string-left-trim.


Any ideas?



Grrr1337

  • Swamp Rat
  • Posts: 695
Re: I'm not understanding what is going on here.
« Reply #5 on: September 09, 2019, 02:00:39 PM »
I was about to suggest this, but MP was faster -

Code - Auto/Visual Lisp: [Select]
  1. (defun left-trim-string ( pref str )
  2.   ('((f RemoveNth pref str) (f pref str))
  3.     '(( cL sL / i )
  4.       (cond
  5.         ( (or (not sL) (not cL)) (vl-list->string sL))
  6.         ( (setq i (vl-position (car cL) sL)) (f (cdr cL) (RemoveNth i sL)) )
  7.         ( (f (cdr cL) sL) )
  8.       )
  9.     )
  10.     '(( n L / i )
  11.       (setq i -1) (apply 'append (mapcar '(lambda (x) (if (/= (setq i (1+ i)) n) (list x))) L))
  12.     )
  13.     (vl-string->list pref) (vl-string->list str)
  14.   )
  15. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (left-trim-string "PREFIX:" "PREFIX: FEDCBA") >> " FEDCBA"
  2. _$ (left-trim-string "PREFIX: " "PREFIX: FEDCBA") >> "FEDCBA"
  3. _$ (left-trim-string "PREFFIX: " "PREFIX: FEDCBA") >> "EDCBA"
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: I'm not understanding what is going on here.
« Reply #6 on: September 09, 2019, 02:04:39 PM »
Thanks for the replies, guys!


efernal, I get the same results with vl-string-trim.


MP, thanks for the examples, but I still don't understand why I get the results I'm getting with vl-string-trim/vl-string-left-trim.


Any ideas?

Trim as you found out uses the characters as a "character set" and any matching characters, not words, will be removed.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Eddie D.

  • Newt
  • Posts: 28
Re: I'm not understanding what is going on here.
« Reply #7 on: September 09, 2019, 02:19:26 PM »
Cab,
I understand how vl-string-trim-* works (at least I thought I did), but the issue I'm having is that it works correctly EXCEPT when the expected return string begins with E, F, I, P, R, AND X.
I don't understand why.

tombu

  • Newt
  • Posts: 152
  • ByLayer=>Not0
Re: I'm not understanding what is going on here.
« Reply #8 on: September 09, 2019, 02:41:40 PM »
Cab,
I understand how vl-string-trim-* works (at least I thought I did), but the issue I'm having is that it works correctly EXCEPT when the expected return string begins with E, F, I, P, R, AND X.
I don't understand why.
You called for the characters "P, R, E, F, I, X, :, and " " to be removed from the left and they were.
As characters A and D were not included as characters to trim that's what the results started at.
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

Eddie D.

  • Newt
  • Posts: 28
Re: I'm not understanding what is going on here.
« Reply #9 on: September 09, 2019, 03:48:03 PM »
Tombu, THANK YOU!! I understand now, and CAB's explanation makes sense now.


To me, the documentation didn't quite explain well enough for my hard head to absorb.


THANKS, Everyone!!!


It's been a typical Monday today.

Lee Mac

  • Seagull
  • Posts: 12255
  • London, England
Re: I'm not understanding what is going on here.
« Reply #10 on: September 09, 2019, 05:08:52 PM »
Interesting use of vl-string-mismatch MP, it is an underutilised function  :-)

Though it may trip up if the supplied string contains a partial match with the pattern, e.g.:

(setq text "PRELIMINARY")

(if (< 0 (setq i (vl-string-mismatch "PREFIX: " text)))
    (substr text (1+ i))
    text
)


>> "LIMINARY"


I would likely have approached it this way:

Code - Auto/Visual Lisp: [Select]
  1. (defun ltrim ( pre str / len )
  2.     (if (and (wcmatch str (strcat pre "*")) (< 0 (setq len (strlen pre))))
  3.         (ltrim pre (substr str (1+ len)))
  4.         str
  5.     )
  6. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (ltrim "PREFIX: " "PREFIX: FEDCBA")
  2. "FEDCBA"
  3.  
  4. _$ (ltrim "PREFIX: " "PREFIX: PREFIX: ABCDEF")
  5. "ABCDEF"
« Last Edit: September 09, 2019, 05:26:54 PM by Lee Mac »

MP

  • Seagull
  • Posts: 17400
Re: I'm not understanding what is going on here.
« Reply #11 on: September 09, 2019, 05:29:10 PM »
Very salient point Lee, thanks for the cautionary note. :)

Here's how I might use vl-string-match in an actual function:

Code: [Select]
(defun _strip-prefix ( pfx text / i )
    (if (eq (strlen pfx) (setq i (vl-string-mismatch (strcase pfx) (strcase text))))
        (substr text (1+ i))
        text
    )
)

(_strip-prefix "prefix: " "prefix: fedcba")      >> "fedcba"
(_strip-prefix "prefix: " "preliminary: fedcba") >> "preliminary: fedcba"


Cheers!
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

gile

  • Water Moccasin
  • Posts: 2224
  • Marseille, France
Re: I'm not understanding what is going on here.
« Reply #12 on: September 09, 2019, 05:56:44 PM »
Hi,

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Code: [Select]
_$ (RegExpReplace "prefix: fedcba" "prefix: " "" T T)
"fedcba"
_$ (RegExpReplace "preliminary: fedcba" "prefix: " "" T T)
"preliminary: fedcba"
_$ (RegExpReplace "PREFIX: FEDCBA" "prefix: " "" T T)
"FEDCBA"
_$ (RegExpReplace "PREFIX: FEDCBA" "prefix: " "" nil T)
"PREFIX: FEDCBA"
_$ (RegExpReplace "prefix: prefix: fedcba" "prefix: " "" T T)
"fedcba"
_$ (RegExpReplace "prefix: prefix: fedcba" "prefix: " "" T nil)
"prefix: fedcba"

And now they have three problems, because of French comments...
Code - Auto/Visual Lisp: [Select]
  1. ;;--------------------------------------------------------------------------------------------------------;;
  2. ;;                                         Expressions Régulières                                         ;;
  3. ;;--------------------------------------------------------------------------------------------------------;;
  4. ;;                                                                                                        ;;
  5. ;; Fonctions LISP pour émuler les propriétés et méthodes du type RegExp de vbscript.                      ;;
  6. ;;                                                                                                        ;;
  7. ;; Référence                                                                                              ;;
  8. ;; http://tahe.developpez.com/tutoriels-cours/vbscript/?page=page_4#LIV-B                                 ;;
  9. ;; https://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx                                          ;;
  10. ;;                                                                                                        ;;
  11. ;; Gilles Chanteau                                                                                        ;;
  12. ;;--------------------------------------------------------------------------------------------------------;;
  13.  
  14.  
  15. ;; RegExpSet
  16. ;; Retourne l'instance de VBScript.RegExp courante après avoir défini ses propriétés.
  17. ;;
  18. ;; Arguments
  19. ;; pattern    : Motif à rechercher.
  20. ;; ignoreCase : Si non nil, la recherche est faite sans tenir compte de la casse.
  21. ;; global     : Si non nil, recherche toutes les occurrences du modèle ;
  22. ;;              si nil, recherche uniquement la première occurrence.
  23.  
  24. (defun RegExpSet (pattern ignoreCase global / regex)
  25.   (setq regex
  26.          (cond
  27.            ((vl-bb-ref '*regexp*))
  28.            ((vl-bb-set '*regexp* (vlax-create-object "VBScript.RegExp")))
  29.          )
  30.   )
  31.   (vlax-put regex 'Pattern pattern)
  32.   (if ignoreCase
  33.     (vlax-put regex 'IgnoreCase acTrue)
  34.     (vlax-put regex 'IgnoreCase acFalse)
  35.   )
  36.   (if global
  37.     (vlax-put regex 'Global acTrue)
  38.     (vlax-put regex 'Global acFalse)
  39.   )
  40.   regex
  41. )
  42.  
  43. ;; RegExpTest
  44. ;; Retourne T si une correspondance avec le motif a été trouvée dans la chaîne ; sinon, nil.
  45. ;;
  46. ;; Arguments
  47. ;; string     : Chaîne dans la quelle on recherche le motif.
  48. ;; pattern    : Motif à rechercher.
  49. ;; ignoreCase : Si non nil, la recherche est faite sans tenir compte de la casse.
  50. ;;
  51. ;; Exemples :
  52. ;; (RegexpTest "foo bar" "Ba" nil)  ; => nil
  53. ;; (RegexpTest "foo bar" "Ba" T)    ; => T
  54. ;; (RegExpTest "42C" "[0-9]+" nil)  ; => T
  55.  
  56. (defun RegExpTest (string pattern ignoreCase)
  57.   (= (vlax-invoke (RegExpSet pattern ignoreCase nil) 'Test string) -1)
  58. )
  59.  
  60. ;; RegExpExecute
  61. ;; Retourne la liste des correspondances avec le motif trouvées dans la chaine.
  62. ;; Chaque correspondance est renvoyée sous la forme d'une sous-liste contenant :
  63. ;; - la valeur de la correspondance,
  64. ;; - l'index du premier caractère (base 0)
  65. ;; - une liste des sous groupes.
  66. ;;
  67. ;; Arguments
  68. ;; string     : Chaîne dans la quelle on recherche le motif.
  69. ;; pattern    : Motif à rechercher.
  70. ;; ignoreCase : Si non nil, la recherche est faite sans tenir compte de la casse.
  71. ;; global     : Si non nil, recherche toutes les occurrences du modèle ;
  72. ;;              si nil, recherche uniquement la première occurrence.
  73. ;;
  74. ;; Exemples
  75. ;; (RegExpExecute "foo bar baz" "ba" nil nil)               ; => (("ba" 4 nil))
  76. ;; (RegexpExecute "12B 4bis" "([0-9]+)([A-Z]+)" T T)        ; => (("12B" 0 ("12" "B")) ("4bis" 4 ("4" "bis")))
  77. ;; (RegexpExecute "-12 25.4" "(-?\\d+(?:\\.\\d+)?)" nil T)  ; => (("-12" 0 ("-12")) ("25.4" 4 ("25.4")))
  78.  
  79. (defun RegExpExecute (string pattern ignoreCase global / sublst lst)
  80.   (vlax-for match (vlax-invoke (RegExpSet pattern ignoreCase global) 'Execute string)
  81.     (setq sublst nil)
  82.     (vl-catch-all-apply
  83.       '(lambda ()
  84.          (vlax-for submatch (vlax-get match 'SubMatches)
  85.            (if submatch
  86.              (setq sublst (cons submatch sublst))
  87.            )
  88.          )
  89.        )
  90.     )
  91.     (setq lst (cons (list (vlax-get match 'Value)
  92.                           (vlax-get match 'FirstIndex)
  93.                           (reverse sublst)
  94.                     )
  95.                     lst
  96.               )
  97.     )
  98.   )
  99.   (reverse lst)
  100. )
  101.  
  102. ;; RegExpReplace
  103. ;; Retourne la chaîne après remplacement des correspondances avec le motif.
  104. ;;
  105. ;; Arguments
  106. ;; string     : Chaîne dans la quelle on recherche le motif.
  107. ;; pattern    : Motif à rechercher.
  108. ;; newStr     : Chaîne de remplacement.
  109. ;; ignoreCase : Si non nil, la recherche est faite sans tenir compte de la casse.
  110. ;; global     : Si non nil, recherche toutes les occurrences du modèle ;
  111. ;;              si nil, recherche uniquement la première occurrence.
  112. ;;
  113. ;; Exemples :
  114. ;; (RegExpReplace "foo bar baz" "a" "oo" nil T)                  ; => "foo boor booz"
  115. ;; (RegExpReplace "foo bar baz" "(\\w)\\w(\\w)" "$1_$2" nil T)   ; => "f_o b_r b_z"
  116. ;; (RegExpReplace "$ 3.25" "\\$ (\\d+(\\.\\d+)?)" "$1 &#8364;" nil T)  ; => "3.25 &#8364;"
  117.  
  118. (defun RegExpReplace (string pattern newStr ignoreCase global)
  119.   (vlax-invoke (RegExpSet pattern ignoreCase global) 'Replace string newStr)
  120. )
  121.  
« Last Edit: September 09, 2019, 05:59:55 PM by gile »
Speaking English as a French Frog

VovKa

  • Swamp Rat
  • Posts: 1144
  • Ukraine
Re: I'm not understanding what is going on here.
« Reply #13 on: September 09, 2019, 07:09:40 PM »
Lee, (LTRIM "a," "a,bc")
MP, (vl-string-mismatch pfx text 0 0 t)

MP

  • Seagull
  • Posts: 17400
Re: I'm not understanding what is going on here.
« Reply #14 on: September 09, 2019, 07:53:36 PM »
(looked at the help link I posted earlier) - ignore-case-p - gotcha vovka - thanks!
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox