Author Topic: Comparing elements in a list  (Read 1473 times)

0 Members and 1 Guest are viewing this topic.

StarGazer

  • Guest
Comparing elements in a list
« on: June 20, 2005, 04:38:41 PM »
Greetings Everyone,
 I'm working on a routine where I need to create a list of "items" (heatpumps) from a text file that is an extract of attruibtes in a drawing. What I want it to do is grab the first item in the just dump it into a list as the first element. Then reading the next line in I want to compare it to the list to see if it's the same, if it is then just skip it. If not append it to the list. Then go to the next line and compare that one to the list. Now that list can be just one or several elements that need to be compared. some of my drawings have over two hundred Heatpumps in them. I'm getting stuck with the comparing and stepping thru the list. any help would be great.

Thanks, Thomas

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Comparing elements in a list
« Reply #1 on: June 20, 2005, 05:00:39 PM »
If you posted some of your code we could help you better but just to compare the first element in a list,
something like this:

Code: [Select]
(setq mylist (list "trane" 2.5))

(setq target "trane")
(if (= (strcase target)  (strcase (car mylist)))


You may not need to force to upper case but most of the time you must to be sure of a match.
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Comparing elements in a list
« Reply #2 on: June 20, 2005, 05:15:21 PM »
Re-reading your post, perhaps something like this:
Code: [Select]
(setq next_attr "trane"
      newlist '())


(if (member (strcase next_attr)   mylist)
  (setq newlist (cons (strcase (car mylist)) newlist))
)

In this example the newlist is forced to upper case.

If you want to save the case of the attribute you could do this:
Code: [Select]
(if (member (strcase next_attr)   mylist)
  (setq newlist1 (cons (strcase (car mylist)) newlist1)
        newlist (cons (strcase (car mylist)) newlist))
)

 
 Newlist1 is used for the test & discarded when you are done while newlist has the
 names with the case preserved. There should be a better way to do that but i can't
 think of one at the moment.
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.

whdjr

  • Guest
Comparing elements in a list
« Reply #3 on: June 20, 2005, 05:21:53 PM »
Here's a good read for you.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Comparing elements in a list
« Reply #4 on: June 20, 2005, 05:51:40 PM »
There are tons of ways. In fact; hold on a sec. (I'll whip up a demo for ya.)

Here is a recursive one off the top of my head.

You did alot of the work in your post believe it or not. (The code is the easy part, the hard part is getting down what you need in words.)

Code: [Select]
(defun test (lst-c lst-m / inlist)
 
  (defun inlist (? lst)
    (and (member ? lst)) )
 
  (cond
    ((inlist (car lst-c) lst-m)
     (alert "Found a match")
     (test (cdr lst-c) lst-m))
    ( T (cond
          ((<= (length lst-c) 1)
           "End of list reached"
           )
          ( T (test (cdr lst-c) lst-m))))) )


BTW, This was just for play. I did this in five min. Its not a very efficient work of code I just wanted to show that it can be doen quite easily, you just need a lil push to get going and you'll get it whiped up in no time.  Post a more specific example and we'll get cha going ma-good-man.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org