Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

using wcmatch in a list

12 REPLIES 12
Reply
Message 1 of 13
Anonymous
1788 Views, 12 Replies

using wcmatch in a list

I know wcmatch is for use with strings but is there a way search a list
similar to wcmatch?
12 REPLIES 12
Message 2 of 13
Anonymous
in reply to: Anonymous

Hi,

You have to build one. You could define a little function:

;;;=====================================
;;; lematch stends for List Element MATCH
(defun lematch (lst target) (or (car (member target lst))))
;;;=====================================

Command: (lematch '(1 2 3) 2)
T

Command: (lematch '("a" "b" "c") "b")
T

HTH

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.


"andrew.nao" a écrit dans le message de news:
6306459@discussion.autodesk.com...
I know wcmatch is for use with...
Message 3 of 13
Anonymous
in reply to: Anonymous

how would you get this to work with wildcards?
or cant it?

or to work with (vl-directory-files) function?
Message 4 of 13
Anonymous
in reply to: Anonymous

This would be for a particular type of lists, one that contains string
items. The first of the next functions checks each element of the list and
if at least one of them contains the pattern, returns T. Or if you want to
have the list with the elements that match the pattern, you could use the
second one. The rules for the pattern are the same as for (wcmatch ...)
function. And (vl-directory-files) can be passed as argument to (lematch
...) function, since it returns a list of files:

(lematch (vl-directory-files path "*.dwg" 1) pattern)

;;;=================
(defun lematch (lst pattern)
(or
(vl-remove-if-not
'(lambda (x)
(wcmatch x pattern)
)
lst
)
)
)
;;;=================
(defun lematch (lst pattern)
(vl-remove-if-not
'(lambda (x)
(wcmatch x pattern)
)
lst
)
)
;;;=================

HTH

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.


"andrew.nao" a écrit dans le message de news:
6306474@discussion.autodesk.com...
how would you get this to work...


Message 5 of 13
Kent1Cooper
in reply to: Anonymous

Since (vl-directory-files) returns a list, you can certainly work with that. But (member) only looks for exact matches, so if you want wildcard matching, you probably need to use (wcmatch) on each item, something like this:

{code}
(defun WCML (thelist teststring / inthere)
; for Wild-Card Match items in List
(foreach item thelist
(if (wcmatch item (strcat "*" teststring "*"))
(setq inthere T)
); end if
); end foreach
inthere
); end defun
{code}

Testing:

(setq test '("abc" "def" "ghi"))

Command: (wcml test "b")
T

Command: (wcml test "hi")
T

Command: (wcml test "m")
nil

--
Kent Cooper


andrew.nao wrote...
how would you get this to work with wildcards?
....
or to work with (vl-directory-files) function?
Kent Cooper, AIA
Message 6 of 13
Anonymous
in reply to: Anonymous

It depends on whether you want all the matching elements in the list, or
only want to know if there are any matching elements, or all elements match.

For the former, what SomeBuddy shows will work.

To find out if at least one element matches:

{code}

(vl-some '(lambda (s) (wmatch s pattern)) list-of-strings)

{code}

To find out if all elements match:

{code}

(vl-every '(lambda (s) (wmatch s pattern)) list-of-strings)

{code}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"andrew.nao" wrote in message
news:6306474@discussion.autodesk.com...
how would you get this to work with wildc...


Message 7 of 13
cab2k
in reply to: Anonymous

And if you wanted to know where in the list you could use this.
{code};; CAB 12/17/2009
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (pat lst / i plst)
(setq i 0)
(mapcar
(function
(lambda (x)
(and (wcmatch x pat) (setq plst (cons i plst)))
(setq i (1+ i))))
lst)
(reverse plst))

(defun c:test()
(princ (getpos "*25" '("0425" "123" "345" "625")))
(princ)
){code}
Message 8 of 13
cab2k
in reply to: Anonymous

If you want the items themselves.
{code};; CAB 12/17/2009
;; returns a list matching items from the list
(defun getMatching (pat lst / i mlst)
(mapcar
(function
(lambda (x)
(and (wcmatch x pat) (setq mlst (cons x mlst)))))
lst)
(reverse mlst))

(defun c:test()
(print (getMatching "*25" '("0425" "123" "345" "625")))
(princ)
){code}
Message 9 of 13
diagodose2009
in reply to: Anonymous

Use: '(quote' not '(function' for compatibile with other Cad-LISP programs. at :.

(function
(lambda (x)
(and (wcmatch x pat) (setq mlst (cons x mlst)))))
lst)

replace :
(function
(quote (x)
(and (wcmatch x pat) (setq mlst (cons x mlst)))))
lst) Edited by: diagodose2009 on Dec 18, 2009 8:46 AM
Message 10 of 13
Anonymous
in reply to: Anonymous

Nice solutions Tony. I would just underline a small typo for the OP,
obviously it's about 'wcmatch' and not 'wmatch' 🙂

Regards

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.


"Tony Tanzillo" a écrit dans le message de
news: 6306852@discussion.autodesk.com...
It depends on whether you want all the matching elements in the list, or
only want to know if there are any matching elements, or all elements match.

For the former, what SomeBuddy shows will work.

To find out if at least one element matches:

{code}

(vl-some '(lambda (s) (wmatch s pattern)) list-of-strings)

{code}

To find out if all elements match:

{code}

(vl-every '(lambda (s) (wmatch s pattern)) list-of-strings)

{code}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"andrew.nao" wrote in message
news:6306474@discussion.autodesk.com...
how would you get this to work with wildca...


Message 11 of 13
cab2k
in reply to: Anonymous

diagodose2009
Don't quite follow your example. Care to elaborate?
Message 12 of 13
Anonymous
in reply to: Anonymous

excellent

appreciate the help
Message 13 of 13
Anonymous
in reply to: Anonymous

Maybe theese can help.

Marco

; Function: ALE_List_MemberWcMatch
;
; Version 1.00 - 05/12/2006
;
; Description:
; Searches a list for an occurrence of wcmatch and returns the remainder
; of the list, starting with the first occurrence of the expression
;
; Arguments:
; WcMStr = Wcmatch string [STR]
; ex.: "Line,*Polyline,Arc,Circle,Ellipse,Spline"
; In_Lst: the list of strings in which to search for wcmatch
;
; Return Values:
; A list, otherwise nil, if there is no wcmatch occurrence in lst.
;
; Example:
; (ALE_List_MemberWcMatch
; "*rc*" '("Line" "Polyline" "Arc" "Circle" "Ellipse" "Spline")
; )
; =>("Arc" "Circle" "Ellipse" "Spline")
;
(defun ALE_List_MemberWcMatch (WcmStr In_Lst)
(vl-member-if
'(lambda (LmbDat) (wcmatch LmbDat WcmStr))
In_Lst
)
)
; Function: ALE_List_RemoveIfCarNotWcMatch
;
; Version 1.00 - 05/12/2006
;
; Description:
; Returns all elements of the supplied list that Wcmatch WcMStr
;
; Arguments:
; WcMStr = Wcmatch string [STR]
; ex.: "Line,*Polyline,Arc,Circle,Ellipse,Spline"
; In_Lst: the list of lists of strings in which to search for wcmatch
;
; Return Values: A list
;
; Example:
; (ALE_List_RemoveIfCarNotWcMatch
; "*rc*"
; '(("Line" 1000 1.0)("Polyline" 1005 4.0)("Arc" 1002 3.0)("Circle" 1001
7.0)("Ellipse" 1011 1.1)("Spline" 1013 1.3))
; )
; =>("Arc" "Circle")
;
(defun ALE_List_RemoveIfCarNotWcMatch (WcmStr In_Lst)
(vl-remove-if-not
'(lambda (LmbDat) (wcmatch (car LmbDat) WcmStr))
In_Lst
)
)
; Function: ALE_List_RemoveIfNotWcMatch
;
; Version 1.00 - 05/12/2006
;
; Description:
; Returns all elements of the supplied list that Wcmatch WcMStr
;
; Arguments:
; WcMStr = Wcmatch string [STR]
; ex.: "Line,*Polyline,Arc,Circle,Ellipse,Spline"
; In_Lst: the list of strings in which to search for wcmatch
;
; Return Values: A list
;
; Example:
; (ALE_List_RemoveIfNotWcMatch
; "*rc*" '("Line" "Polyline" "Arc" "Circle" "Ellipse" "Spline")
; )
; =>("Arc" "Circle")
;
(defun ALE_List_RemoveIfNotWcMatch (WcmStr In_Lst)
(vl-remove-if-not
'(lambda (LmbDat) (wcmatch LmbDat WcmStr))
In_Lst
)
)


{code}
"andrew.nao" ha scritto nel messaggio
news:6306459@discussion.autodesk.com...
I know wcmatch is for use with st...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost