Author Topic: Comparing List Variables  (Read 4387 times)

0 Members and 1 Guest are viewing this topic.

dustinthiesse

  • Guest
Comparing List Variables
« on: February 04, 2009, 03:54:14 PM »
I'm sure there's a simple way to do this...

What I've got is a list of filenames like:  (setq dwgs (list "aaa" "bbb" "ccc"))....etc....
What I am trying to do is if the current drawing exists within the list, remove it and then do some stuff...

Code: [Select]
(if (/= dwgs
         (setq dwgs (vl-remove-if
                          '(lambda (x)
                             (eq x (getvar 'dwgname))
                           )
                         dwgs
                         )
         )
    )
  (write-line(strcat "(load\""lisprun"\") " lisprun) scrfile)
)

It seems my main issue is the "/=" operator always returns T when comparing two lists.
I need this statement to return T ONLY when the current dwg is a member of the 'dwgs' variable.
Is there a function that will acheive this?

I know, i could probably do this by saying:

Code: [Select]
(if (member (getvar 'dwgname) dwgs)
  (progn
    (write-line(strcat "(load\""lisprun"\") " lisprun) scrfile)
    (setq dwgs (vl-remove (getvar 'dwgname) dwgs))
  )
)
But I don't want to!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Comparing List Variables
« Reply #1 on: February 04, 2009, 04:03:44 PM »
If you want to use ' vl-remove... ', then just check the length of the list before and after.  If it's less, than the list was there, if equal than not.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

dustinthiesse

  • Guest
Re: Comparing List Variables
« Reply #2 on: February 04, 2009, 04:04:42 PM »
Good idea Tim.

I figured it out tho.
I needed to use (not(equal....))
instead of (/=.....)

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Comparing List Variables
« Reply #3 on: February 04, 2009, 04:08:34 PM »
Code: [Select]
(setq dwgs '( "aaa" "bbb" "ccc"))
(setq count1 (vl-list-length dwgs))

(setq dwgs (vl-remove (strcase "ccc")  (mapcar 'strcase dwgs)))
(setq count2 (vl-list-length dwgs))

(if (/= count1 count2)
  (alert "Current drawing name removed from the list")
  (alert "Current drawing name not found on the list")
)
Keep smile...

ronjonp

  • Needs a day job
  • Posts: 7533
Re: Comparing List Variables
« Reply #4 on: February 04, 2009, 04:12:14 PM »
Another way:

Code: [Select]
(if (and (vl-position (getvar 'dwgname) dwgs)
         (setq dwgs (vl-remove-if '(lambda (x) (eq x (getvar 'dwgname))) dwgs))
    )
  (write-line (strcat "(load\"" lisprun "\") " lisprun)
              scrfile
  )
)

I think this would work too (not tested)

Code: [Select]
(and (vl-position (getvar 'dwgname) dwgs)
     (setq dwgs (vl-remove-if '(lambda (x) (eq x (getvar 'dwgname))) dwgs))
     (write-line (strcat "(load\"" lisprun "\") " lisprun)
                 scrfile
     )
)
« Last Edit: February 04, 2009, 04:18:26 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: Comparing List Variables
« Reply #5 on: February 04, 2009, 06:01:54 PM »


Your homework for the night is to construct similar Auto Lisp versions; one which uses an iterative process. Then one that uses a recursive process.  ...Class dismissed.


:)~
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dustinthiesse

  • Guest
Re: Comparing List Variables
« Reply #6 on: February 05, 2009, 08:18:20 AM »


Your homework for the night is to construct similar Auto Lisp versions; one which uses an iterative process. Then one that uses a recursive process.  ...Class dismissed.

Sounds like a fun challenge for the next time I have a little downtime (or need a distraction...ADD).
I'll have to put off checking this thread so I don't see the answers though  :-D

dustinthiesse

  • Guest
Re: Comparing List Variables
« Reply #7 on: February 09, 2009, 08:45:54 AM »
I'm sure there's a more elegant way of doing this, but here's what I've got for the iterative version.
I've not been schooled in programming so the recursive version is a little above me at the moment.
I'd be interested to see one for educational purposes though.

Code: [Select]
;;;removes all instances of itm from lst
;;;returns newlst with itm removed
(defun remove-if(lst itm / newlst)

  (foreach n lst
    (if(/= n itm)
      (setq newlst(cons n newlst))
    )
  )
  (reverse newlst)
)

;;;returns T if itm exists in lst
;;;returns nil if itm does not exist
;
;;;basically is a (member) function
(defun remove-if-p(lst itm)

  (if(equal lst (remove-if lst itm))
    nil
    T
  )
)

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Comparing List Variables
« Reply #8 on: February 09, 2009, 10:05:20 AM »
Hi,

Here's a recursive way for remove (vl-remove)
Code: [Select]
(defun remove (item lst)
  (if lst
    (if (equal item (car lst))
      (remove item (cdr lst))
      (cons (car lst) (remove item (cdr lst)))
    )
  )
)

For remove-if, an argument should be a predicate function:
Code: [Select]
(defun remove-if (fun lst)
  (if lst
    (if (apply fun (list (car lst)))
      (remove-if fun (cdr lst))
      (cons (car lst) (remove-if fun (cdr lst)))
    )
  )
)

To search an item in a list, you'd rather stop the process when the first occurence is found
A recursive way
Code: [Select]
(defun countains (item lst)
  (if lst
    (or (equal item (car lst))
      (countains1 item (cdr lst))
    )
  )
)
An iterative way
Code: [Select]
(defun countains (item lst / found)
  (while (and (not found) lst)
    (if (equal item (car lst))
      (setq found T)
      (setq lst (cdr lst))
     
    )
  )
  found
)
« Last Edit: February 09, 2009, 10:34:03 AM by gile »
Speaking English as a French Frog

dustinthiesse

  • Guest
Re: Comparing List Variables
« Reply #9 on: February 09, 2009, 10:14:58 AM »
Thank you very much for the examples!  :-D

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Comparing List Variables
« Reply #10 on: February 09, 2009, 10:21:53 AM »
Another iterative way

Code: [Select]
(defun countains (item lst)
  (and
    (while (and lst (not (equal item (car lst))))
      (setq lst (cdr lst))
    )
    lst
  )
)
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: Comparing List Variables
« Reply #11 on: February 09, 2009, 12:53:19 PM »
My most favorite iterative version:
Code: [Select]
;;; Author: Matt Stachoni
(defun list:Remove (e l)
  (apply 
    'append
    (subst nil (list e) (mapcar 'list l))
    )
  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Comparing List Variables
« Reply #12 on: February 09, 2009, 01:02:00 PM »
Truth be told, that one looks like something Tony wrote around 1692. PS: It's not iterative.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dustinthiesse

  • Guest
Re: Comparing List Variables
« Reply #13 on: February 09, 2009, 01:04:55 PM »
PS: It's not iterative.

I still like it.  Good stuff.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: Comparing List Variables
« Reply #14 on: February 09, 2009, 01:13:29 PM »
Truth be told, that one looks like something Tony wrote around 1692. PS: It's not iterative.
yeah i guess your right. ...meh.

It does? Well i got it off a post from Matt so i don't really know where it came from before that so sorry Tony, if its your's.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org