Author Topic: How to wildcard search in string list  (Read 3331 times)

0 Members and 1 Guest are viewing this topic.

vincent.r

  • Newt
  • Posts: 101
How to wildcard search in string list
« on: December 05, 2019, 10:18:58 AM »
Hi folks,

I do have string list, ("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl" .....)
I want to extract all "ele_*" items with their position in list, but not able to get it. I tried with mapcar, lambda, vl-string-search .
Any help will be highly appreciable.

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: How to wildcard search in string list
« Reply #1 on: December 05, 2019, 10:34:34 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l / eles k kk )
  2.   (setq eles (vl-remove-if-not '(lambda ( x ) (wcmatch x "ele*")) l))
  3.   (setq k -1)
  4.   (foreach x l
  5.     (setq k (1+ k))
  6.     (if (vl-position x eles)
  7.       (setq kk (cons k kk))
  8.     )
  9.   )
  10.   (reverse kk)
  11. )
  12.  

Untested, but I suppose it's fine...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ronjonp

  • Needs a day job
  • Posts: 7526
Re: How to wildcard search in string list
« Reply #2 on: December 05, 2019, 10:53:48 AM »
Another way:
Code - Auto/Visual Lisp: [Select]
  1. (defun _foo (m l / n)
  2.   (setq n -1)
  3.   (vl-remove 'nil (mapcar '(lambda (x) (setq n (1+ n)) (if (wcmatch x m) (cons x n))) l))
  4. )
  5. (_foo "ele*"
  6.       '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl")
  7. )
  8. ;; (("ele_gl" . 4) ("ele_fl" . 5) ("ele_sl" . 6) ("ele_tl" . 7))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: How to wildcard search in string list
« Reply #3 on: December 05, 2019, 10:59:46 AM »
Another way:
Code - Auto/Visual Lisp: [Select]
  1. (defun _foo (m l / n)
  2.   (setq n -1)
  3.   (vl-remove 'nil (mapcar '(lambda (x) (setq n (1+ n)) (if (wcmatch x m) (cons x n))) l))
  4. )
  5. (_foo "ele*"
  6.       '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl")
  7. )
  8. ;; (("ele_gl" . 4) ("ele_fl" . 5) ("ele_sl" . 6) ("ele_tl" . 7))

Elegant, you are better - iterating just once...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ronjonp

  • Needs a day job
  • Posts: 7526
Re: How to wildcard search in string list
« Reply #4 on: December 05, 2019, 11:12:56 AM »
Thanks :) .. I guess it could be as simple as this too:
Code - Auto/Visual Lisp: [Select]
  1. (defun _foo (m l / n r)
  2.   (setq n -1)
  3.   (foreach x l (setq n (1+ n)) (if (wcmatch x m) (setq r (cons (cons x n) r))))
  4.   (reverse r)
  5. )
  6. (_foo "ele*"
  7.       '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl")
  8. )
  9. ;; (("ele_gl" . 4) ("ele_fl" . 5) ("ele_sl" . 6) ("ele_tl" . 7))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

vincent.r

  • Newt
  • Posts: 101
Re: How to wildcard search in string list
« Reply #5 on: December 06, 2019, 08:47:02 AM »
Thanks :) .. I guess it could be as simple as this too:
Code - Auto/Visual Lisp: [Select]
  1. (defun _foo (m l / n r)
  2.   (setq n -1)
  3.   (foreach x l (setq n (1+ n)) (if (wcmatch x m) (setq r (cons (cons x n) r))))
  4.   (reverse r)
  5. )
  6. (_foo "ele*"
  7.       '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl")
  8. )
  9. ;; (("ele_gl" . 4) ("ele_fl" . 5) ("ele_sl" . 6) ("ele_tl" . 7))


Thanks!  You both are genius.

I used ribarm's solution, it is generating just a list of positions and it is simple to handle for me. One iteration version for my learning and future reference. Thanks again guys. Really appreciated your efforts.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: How to wildcard search in string list
« Reply #6 on: December 06, 2019, 09:06:50 AM »
If you just require a list of zero-based indexes, here are three variants:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( p l / i r )
  2.     (setq i 0)
  3.     (foreach x l
  4.         (if (wcmatch x p) (setq r (cons i r)))
  5.         (setq i (1+ i))
  6.     )
  7.     (reverse r)
  8. )
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( p l / i )
  2.     (setq i -1)
  3.     (vl-remove nil (mapcar '(lambda ( x ) (setq i (1+ i)) (if (wcmatch x p) i)) l))
  4. )
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( p l / bar )
  2.     (defun bar ( p l i )
  3.         (cond
  4.             (   (null l) nil)
  5.             (   (wcmatch (car l) p) (cons i (bar p (cdr l) (1+ i))))
  6.             (   (bar p (cdr l) (1+ i)))
  7.         )
  8.     )
  9.     (bar p l 0)
  10. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (foo "ele*" '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl"))
  2. (4 5 6 7)

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: How to wildcard search in string list
« Reply #7 on: December 06, 2019, 12:16:22 PM »
Something adapted from https://www.theswamp.org/index.php?topic=50539.0.

Code - Auto/Visual Lisp: [Select]
  1. (defun match_index (l str / i j x r)
  2.   (setq j -1)
  3.   (while
  4.     (setq x (vl-some
  5.               '(lambda (x)
  6.                  (if (wcmatch x str) x)
  7.                )
  8.                l
  9.             )
  10.    )
  11.    (setq i (vl-position x l)
  12.          r (cons
  13.               (cons x (setq j (+ 1 i j)))
  14.               r
  15.             )
  16.           l (cdr (member x l))
  17.     )
  18.   )
  19.   (reverse r)
  20. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (match_index '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl") "ele*")
  2. (("ele_gl" . 4) ("ele_fl" . 5) ("ele_sl" . 6) ("ele_tl" . 7))

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to wildcard search in string list
« Reply #8 on: December 06, 2019, 02:33:43 PM »
Something for fun:
Code - Auto/Visual Lisp: [Select]
  1. ( '( (f m L) (f 0 (car L) (cdr L) m))
  2.   '( ( i x L m )
  3.     (if (or x L)
  4.       (append (if (wcmatch x m) (list (cons x i))) (f (1+ i) (car L) (cdr L) m))
  5.     )
  6.   )
  7.   "ele*"
  8.   '("beam_gl" "beam_fl" "beam_sl" "beam_tl" "ele_gl" "ele_fl" "ele_sl" "ele_tl" "column_gl")
  9. )
(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)
  )
)
vevo.bg