Author Topic: Get TAG name List question  (Read 8933 times)

0 Members and 1 Guest are viewing this topic.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Get TAG name List question
« Reply #15 on: July 31, 2011, 07:37:21 AM »
This is mine...
Code: [Select]
(defun foo (lst / a val)
  (setq lst (reverse lst))
  (while
    (and lst
(or
   (not (setq val (vl-string->list (cdar lst))))
   (vl-every '(lambda (x) (= x 32)) val)
   )
)
    (setq a (car lst)
  lst (cdr lst)
  )
    )
  (if a (list (car lst) a) (list (car lst)))
  )
But I wonder, what result would you want for this list '(("TAG1" . " ") ("TAG2" . "") ("TAG3" . "") ("TAG4" . "   "))

My function returns (nil ("TAG1" . " "))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get TAG name List question
« Reply #16 on: July 31, 2011, 07:57:22 AM »
Try:

Code: [Select]
(defun foo ( lst )
  (cond
    ( (null (cddr lst))
      (if
        (or
          (wcmatch (cdadr lst) "*@*")
          (wcmatch (cdadr lst) "*#*")
        )
        (cdr lst)
        lst
      )
    )
    ( (and
        (wcmatch (cdadr  lst) "~*@*")
        (wcmatch (cdadr  lst) "~*#*")
        (wcmatch (cdaddr lst) "~*@*")
        (wcmatch (cdaddr lst) "~*#*")
      )
      (list (car lst) (cadr lst))
    )
    ( (foo (cdr lst)))
  )
)

But I should imagine it could be written better.

jaydee

  • Guest
Re: Get TAG name List question
« Reply #17 on: July 31, 2011, 10:24:22 AM »
Hi.
This code from LM provide the correct result as long as theres no SPACES as NULL value

Code: [Select]
(defun foo ( lst )
  (cond
    ( (null (cddr lst))
      (if (/= "" (cdadr lst)) (cdr lst) lst)
    )
    ( (and
        (eq "" (cdadr  lst))
        (eq "" (cdaddr lst))
      )
      (list (car lst) (cadr lst))
    )
    ( (foo (cdr lst)) )
  )
)


(foo '(("TAG1" . "") ("TAG2" . "") ("TAG3" . "") ("TAG4" . "") ("TAG5" . "")("TAG6" . "")))
===> (("TAG1" . "") ("TAG2" . ""))

(foo '(("TAG1" . "AAA") ("TAG2" . "BBB") ("TAG3" . "CCC") ("TAG4" . "") ("TAG5" . "")("TAG6" . "")))
===> (("TAG3" . "CCC") ("TAG4" . ""))

(foo '(("TAG1" . "AAA") ("TAG2" . "BBB") ("TAG3" . "") ("TAG4" . "DDD") ("TAG5" . "")("TAG6" . "")))
===> (("TAG4" . "DDD") ("TAG5" . ""))

(foo '(("TAG1" . "AAA") ("TAG2" . "") ("TAG3" . "CCC") ("TAG4" . "DDD") ("TAG5" . "EEE")("TAG6" . "")))
===> (("TAG5" . "EEE") ("TAG6" . ""))

(foo '(("TAG1" . "AAA") ("TAG2" . "BBB") ("TAG3" . "CCC") ("TAG4" . "DDD") ("TAG5" . "EEE")("TAG6" . "FFF")))
===> (("TAG6" . "FFF"))



The latest (foo) function from Lee still treat a NULL value with space as real value

Code: [Select]
(foo '(("TAG1" . "AAA") ("TAG2" . "BBB") ("TAG3" . " ") ("TAG4" . "DDD") ("TAG5" . " ")("TAG6" . " ")))
===> (("TAG6" . " "))

Corrcect result should be

===> (("TAG4" . "DDD") ("TAG5" . ""))



The foo function above from Lee is what i want, the issue is it treats a NULL value with space in it as a real value.
My experience is that

( _  indicates null value with space)

a "" value is common
a "_" null value with single space is very rare
a "__" I have not come across null value with 2 or more spaces.

Hope i explain properly.

Thankyou


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get TAG name List question
« Reply #18 on: July 31, 2011, 05:39:27 PM »
Using my code in reply #16:

Code: [Select]
_$ (foo '(("TAG1" . "AAA") ("TAG2" . "BBB") ("TAG3" . " ") ("TAG4" . "DDD") ("TAG5" . " ")("TAG6" . " ")))
(("TAG4" . "DDD") ("TAG5" . " "))

 :?

jaydee

  • Guest
Re: Get TAG name List question
« Reply #19 on: July 31, 2011, 07:53:50 PM »
Sorry about this lee. Its early in the morning and i must have all these different (foo) functions loaded for testing.
You are absolutely right.

Thankyou

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get TAG name List question
« Reply #20 on: August 01, 2011, 07:17:40 AM »
Perhaps this as well?
Code: [Select]
(defun TagTest (lst / n)
  (setq n (length lst))
  (while (and (>= (setq n (1- n)) 0) (eq (vl-string-trim " " (cdr (nth n lst))) "")))
  (if (>= n 0)
    (list (nth n lst) (nth (1+ n) lst))
    (list (last lst))
  )
)

Edit: Just re-read again. That odd-condition (i.e. (("TAG6" . "FFF"))) is making my head spin  :| . In this case Lee's recursive method's best for your case.
« Last Edit: August 01, 2011, 07:21:36 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get TAG name List question
« Reply #21 on: August 01, 2011, 08:27:11 AM »
Good call with the vl-string-trim method - much easier than all my wildcard patterns.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get TAG name List question
« Reply #22 on: August 01, 2011, 08:59:52 AM »
Thanks. I'm just trying to figure out what to do with that all used / all not used condition. AFAICT it seems that if the last tag "Tag6" already has a (non-blank) value you want it, and only it in the returned list?  :|

What would you want if there are only blank values in the list? Only (("Tag1" . "")) ?

If that's the case then maybe this would "finally"  be working:
Code: [Select]
(defun TagTest (lst / n)
  (setq n (length lst))
  (while (and (>= (setq n (1- n)) 0) (eq (vl-string-trim " " (cdr (nth n lst))) "")))
  (if (>= n 0)
    (cons (nth n lst) (nth (1+ n) lst))
    (cons (car lst) nil)
  )
)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get TAG name List question
« Reply #23 on: August 01, 2011, 09:14:45 AM »
Aggggg! I'm being "stupid"!
Code: [Select]
(defun TagTest (lst / n)
  (setq n (length lst))
  (while (and (>= (setq n (1- n)) 0) (eq (vl-string-trim " " (cdr (nth n lst))) "")))
  (if (>= n 0)
    (if (< (1+ n) (length lst))
      (list (nth n lst) (nth (1+ n) lst))
      (list (nth n lst))
    )
    (list (car lst))
  )
)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

jaydee

  • Guest
Re: Get TAG name List question
« Reply #24 on: August 01, 2011, 06:54:05 PM »
Thankyou Irned.
Your method seem to work very well.

I use Lee's method to test the the return list length, if 1 then last one is fill.

With your method, all filled or all empty will return one single dotted pair. therefore i need to test the value, if NULL value then its the first in the list, else last in the list.

Thankyou for your help


One more question?

Where can i find all the reference on these
I knew about these
car
cdr
caddr, cadddr

I have not come across these.
cdadr
cdaddr

Just found this
http://www.lispworks.com/documentation/HyperSpec/Body/f_car_c.htm

CAR, CDR, CAAR, CADR, CDAR, CDDR, CAAAR, CAADR, CADAR, CADDR, CDAAR, CDADR, CDDAR, CDDDR, CAAAAR, CAAADR, CAADAR, CAADDR, CADAAR, CADADR, CADDAR, CADDDR, CDAAAR, CDAADR, CDADAR, CDADDR, CDDAAR, CDDADR, CDDDAR, CDDDDR

I haven't seen anything like this in autodesk reference guide.
« Last Edit: August 01, 2011, 08:25:52 PM by jaydee »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get TAG name List question
« Reply #25 on: August 02, 2011, 04:05:43 AM »
You're correct. I can't remember where I read it first. But basically those C****R functions work in the following way:
  • cadr => (car (cdr list))
  • caddr => (car (cdr (cdr list)))
  • cdddr => (cdr (cdr (cdr list)))
  • cdaddr => (cdr (car (cdr (cdr list))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Get TAG name List question
« Reply #26 on: August 02, 2011, 07:58:15 AM »
That link provides a great explanation  :-)

Here is my short & simple take on it:

Dotted pairs  -  (x . y)

car:  returns first item
cdr:  returns the last item

Code: [Select]
_$ (car '(1 . 2))
1
_$ (cdr '(1 . 2))
2

All other c*r functions will error with dotted pairs since the above functions return an atom, not a list.

Standard Lists  -  (a b c ... x y z)

[ Note that a,b,c etc could also be lists themselves ]

There are only two functions you need to know:

car:  returns the first item of a list
cdr:  returns the list with the first item removed (note how this is different from dotted pairs)

Code: [Select]
_$ (car '(1 2))
1
_$ (cdr '(1 2))
(2)
_$ (car '(1 2 3 4 5))
1
_$ (cdr '(1 2 3 4 5))
(2 3 4 5)
_$ (car '((1 2) 3 4 5))
(1 2)
_$ (cdr '((1 2) 3 4 5))
(3 4 5)

These two functions can be combined up to four times:

Code: [Select]
(caar x)   =  (car (car x))
(cadr x)   =  (car (cdr x))
(cdar x)   =  (cdr (car x))
(cddr x)   =  (cdr (cdr x))

(caaar x)  =  (car (car (car x)))
(caadr x)  =  (car (car (cdr x)))
(cadar x)  =  (car (cdr (car x)))
(cdaar x)  =  (cdr (car (car x)))
(caddr x)  =  (car (cdr (cdr x)))
(cddar x)  =  (cdr (cdr (car x)))
(cdadr x)  =  (cdr (car (cdr x)))
(cdddr x)  =  (cdr (cdr (cdr x)))

(caaaar x) =  (car (car (car (car x)))
(caaadr x) =  (car (car (car (cdr x)))
(caadar x) =  (car (car (cdr (car x)))
(cadaar x) =  (car (cdr (car (car x)))
(caaddr x) =  (car (car (cdr (cdr x)))
(caddar x) =  (car (cdr (cdr (car x)))
(cadadr x) =  (car (cdr (car (cdr x)))
(cadddr x) =  (car (cdr (cdr (cdr x)))
(cdaaar x) =  (cdr (car (car (car x)))
(cdaadr x) =  (cdr (car (car (cdr x)))
(cdadar x) =  (cdr (car (cdr (car x)))
(cddaar x) =  (cdr (cdr (car (car x)))
(cdaddr x) =  (cdr (car (cdr (cdr x)))
(cdddar x) =  (cdr (cdr (cdr (car x)))
(cddadr x) =  (cdr (cdr (car (cdr x)))
(cddddr x) =  (cdr (cdr (cdr (cdr x)))