Author Topic: list handling  (Read 2163 times)

0 Members and 1 Guest are viewing this topic.

curmudgeon

  • Newt
  • Posts: 194
list handling
« on: March 28, 2009, 12:52:41 PM »
I have text entities ( room numbers ) and polygons ( room outlines ). I create an association list
( ("rm_nm_txt" <rm_ename>)  ("rm_nm_txt" <rm_ename>)...)

I was using vl-sort to sort this list in ascending order by room number. Now, I need to reach back into that list and grab the room name by the cdr, if I am communicating at all.

I think I would like to use (setq it  (ssget "_CP" ptlst) ) to get the polyline I want, and then (ssname 0 it) to get the ename, and search the list for the corresponding room number. (member ) is not working for me, so I am thinking I may need either a different subr or a different list type.

Any suggestions?

Never express yourself more clearly than you are able to think.

SomeCallMeDave

  • Guest
Re: list handling
« Reply #1 on: March 28, 2009, 01:36:22 PM »
One of the gurus will have a more elegant way,  but this seems to work, (if I understand the problem correctly)


Code: [Select]
(setq aa (list '("room #1" "e1") '("room #2" "e2") '("room #3" "e3"))) ;;where "e1"  represents your ename

(vl-remove-if-not '(lambda (x) (= (cadr x) "e1")) aa)

will return
(("room #1" "e1"))

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: list handling
« Reply #2 on: March 28, 2009, 01:48:08 PM »
Hi,

(assoc "e2" (mapcar 'reverse aa)) ; returns ("e2" "room #2")
Speaking English as a French Frog

curmudgeon

  • Newt
  • Posts: 194
Re: list handling
« Reply #3 on: March 28, 2009, 01:53:32 PM »
thanks Dave.

I had gotten to the point where I was pretty sure I would need lambda.
and putting it in (caar (vl-remove-if-not '(lambda (x) (= (cadr x) "3C9EE")) lst)) gets my value.
[ I changed the format of the list to save handles instead of enames. ]

 8-)

roy

and now I see the
(assoc "e2" (mapcar 'reverse aa)) ; returns ("e2" "room #2")

I WILL STUDY BOTH.
THANK YOU.

Never express yourself more clearly than you are able to think.

mkweaver

  • Bull Frog
  • Posts: 352
Re: list handling
« Reply #4 on: March 28, 2009, 01:59:06 PM »


Given this list of room numbers
Code: [Select]
(setq mylistByRoom
       (list
       '("room1" . "ename1")
       '("room2" . "ename2")
       '("room3" . "ename3")
       '("room4" . "ename4")
       '("room5" . "ename5")
       '("room6" . "ename6")
       )
      )
One method to get the pair by ename is this:
Code: [Select]
(car (vl-member-if (function (lambda(pair)(= (cdr pair) "ename3"))) mylistbyroom))which returns:
Code: [Select]
("room3" . "ename3")

Another approach would be to make a copy of the list with the ename first, thus
Code: [Select]
(setq
  mylistByEname(mapcar
(function
   (lambda(pair)
     (cons (cdr pair) (car pair))
     )
   )
mylistbyroom
)
  )

making mylistbyename =
Code: [Select]
(("ename1" . "room1")
  ("ename2" . "room2")
  ("ename3" . "room3")
  ("ename4" . "room4")
  ("ename5" . "room5")
  ("ename6" . "room6")
)
Which let's you use assoc to get the item you want:
Code: [Select]
(assoc "ename3" mylistbyename)which returns:
Code: [Select]
("ename3" . "room3")
The list need not be sorted for assoc to work.

Mike


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: list handling
« Reply #5 on: March 28, 2009, 03:07:48 PM »
Nice solution Gile.

Here is another for non dotted pair list:
Code: [Select]
(setq aa '(("room #1" "e1") ("room #2" "e2") ("room #3" "e3")))
Code: [Select]
_$ ;;  2 item list search on the 2nd item
(defun revassoc (itm lst)
  (vl-some '(lambda (x) (if (= (cadr x) itm) x)) lst)
)

_$ (revassoc "e2" aa)
("room #2" "e2")


For dotted pair use this:
Code: [Select]
;; dotted pair list
(defun revassoc (itm lst)
  (vl-some '(lambda (x) (if (= (cdr x) itm) x)) lst)
)
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.