Author Topic: Modify 2nd element of Dotted Pair List  (Read 4311 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 493
Modify 2nd element of Dotted Pair List
« on: December 18, 2015, 05:37:48 AM »
I am having a dotted pair list. For example :-

Code: [Select]
(setq wallinfo (list sublist (cons 'len 240.0) (cons 'hgt 96.0)))
((LYR . "WALLS") (LEN . 240.0) (HGT . 96.0))

I want to modify Length from 240 to 280. How to do that ?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify 2nd element of Dotted Pair List
« Reply #1 on: December 18, 2015, 05:51:23 AM »
Try this :
Code - Auto/Visual Lisp: [Select]
  1. (setq wallinfo '(("LYR" . "WALLS") ("LEN" . 240.0) ("HGT" . 96.0)))
  2.  
  3. (KDUB:replace-assoc "LEN" 280.0 'wallinfo)

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;;;
  5. ;;; a general utility -- replace cons list from assoc list
  6. ;;; say tmp_list is= (("EXPERT" . 2) ("TEXTSIZE" . 3.5) ("BLIPMODE" . 0))
  7. ;;; (KDUB:replace-assoc "TEXTSIZE" 5.0 'tmp_list)
  8. ;;;     ==>(("EXPERT" . 2)("TEXTSIZE" . 5.0)("BLIPMODE" . 0))
  9. ;;; note : Removes first found reference only
  10. ;;; to replace the last matching cons in list :
  11. ;;; (setq tmp_list (reverse tmp_list)
  12. ;;;       tmp_list (reverse (KDUB:replace-assoc .... 'tmp_list)))
  13. ;;;
  14. (defun kdub:replace-assoc (assoctag assocval quoted_lst_sym)
  15.   (set quoted_lst_sym
  16.        (subst (cons assoctag assocval)
  17.               (assoc assoctag (eval quoted_lst_sym))
  18.               (eval quoted_lst_sym)
  19.        )
  20.   )
  21. )
  22.  

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

kpblc

  • Bull Frog
  • Posts: 396
Re: Modify 2nd element of Dotted Pair List
« Reply #2 on: December 18, 2015, 05:55:45 AM »
I'm using this function:
Code - Auto/Visual Lisp: [Select]
  1. (defun _kpblc-list-add-or-subst (lst key value)
  2.                                 ;|
  3. *    Change or add list by key
  4. *    Call parameters:
  5.   lst      list to be proceed
  6.   key      key
  7.   value    value to be set. nil - erase sublist
  8. *    Call samples:
  9. (setq lst '((1 . 2) ("test" . "value0")))
  10. (_kpblc-list-add-or-subt lst "test" 1) ; '((1 . 2) ("test" . 1))
  11. (_kpblc-list-add-or-subt lst "test2" 1) ; '((1 . 2) ("test" . "value0") ("test2" . 1))
  12. (_kpblc-list-add-or-subt lst "test" nil) ; '((1 . 2))
  13. |;
  14.   (if (not value)
  15.     (vl-remove-if (function (lambda (x) (= (car x) key))) lst)
  16.     (if (cdr (assoc key lst))
  17.       (subst (cons key value) (assoc key lst) lst)
  18.       (cons (cons key value)
  19.             (vl-remove-if
  20.               (function
  21.                 (lambda (x)
  22.                   (= (car x) key)
  23.                   ) ;_ end of lambda
  24.                 ) ;_ end of function
  25.               lst
  26.               ) ;_ end of vl-remove-if
  27.             ) ;_ end of cons
  28.       ) ;_ end of if
  29.     ) ;_ end of if
  30.   ) ;_ end of defun
Sorry for my English.

AIberto

  • Guest
Re: Modify 2nd element of Dotted Pair List
« Reply #3 on: December 18, 2015, 06:11:30 AM »
Very good example, Thanks Kerry & kpblc.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify 2nd element of Dotted Pair List
« Reply #4 on: December 18, 2015, 06:23:17 AM »
Very good example, Thanks Kerry & kpblc.

Thanks AIberto.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Modify 2nd element of Dotted Pair List
« Reply #5 on: December 18, 2015, 07:45:36 AM »
@ kpblc:
If you change line 16 to:
Code: [Select]
(if (assoc key lst)You can replace lines 19-26 with just:
Code: [Select]
lst
Note that line 15 will remove all subs with an identical key and that subst in line 17 will only substitute the first. So your function assumes a list with unique keys.
Edit: This is not exactly correct. Subst will replace all occurrences of an item in a list. But the function does assume unique keys:
Code: [Select]
(_kpblc-list-add-or-subst '((1 . 2) (1 . 3) (1 . 4)) 1 nil)
=> nil
(_kpblc-list-add-or-subst '((1 . 2) (1 . 3) (1 . 4)) 1 5)
=> ((1 . 5) (1 . 3) (1 . 4))
« Last Edit: December 18, 2015, 08:23:22 AM by roy_043 »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Modify 2nd element of Dotted Pair List
« Reply #6 on: December 18, 2015, 08:20:49 AM »
Try this :
Code - Auto/Visual Lisp: [Select]
  1. (setq wallinfo '(("LYR" . "WALLS") ("LEN" . 240.0) ("HGT" . 96.0)))
  2.  
  3. (KDUB:replace-assoc "LEN" 280.0 'wallinfo)

...
Let me ask, why not simply?
Code: [Select]
(defun kdub:replace-assoc2 (assoctag assocval lst_sym)
  (subst (cons assoctag assocval)
     (assoc assoctag lst_sym)
     lst_sym
  )
)
(setq wallinfo (KDUB:replace-assoc2 "LEN" 280.0 wallinfo))
=> (("LYR" . "WALLS") ("LEN" . 280.0) ("HGT" . 96.0))

efernal

  • Bull Frog
  • Posts: 206
Re: Modify 2nd element of Dotted Pair List
« Reply #7 on: December 18, 2015, 09:07:15 AM »
Code - Auto/Visual Lisp: [Select]
  1. (SETQ sublist (CONS 'lyr "WALLS"))
  2. (SETQ wallinfo (LIST sublist (CONS 'len 240.0) (CONS 'hgt 96.0)))
  3. (SETQ wallinfo (SUBST (CONS 'len 280.0) (ASSOC 'len wallinfo) wallinfo))
  4.  
  5. (LYR . "WALLS")
  6. ((LYR . "WALLS") (LEN . 240.0) (HGT . 96.0))
  7. ((LYR . "WALLS") (LEN . 280.0) (HGT . 96.0))
  8.  
e.fernal

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify 2nd element of Dotted Pair List
« Reply #8 on: December 18, 2015, 07:21:39 PM »
@Marc
Yes, I posted the version I use for experimenting ..

Still works in this situation
.. though this may suit the OP better
Code - Auto/Visual Lisp: [Select]
  1. (defun kdub:replace-assocVal (tag val lst)
  2.   (subst (cons tag val) (assoc tag lst) lst)
  3. )


@efernal
Yes, I ignored the OP's key naming was using SYM's for the key ... my bad!
If he really wants to define the assoc list like that then a different  function will need to be crafted as you have done.
Fortunately, this still works :
Code - Auto/Visual Lisp: [Select]
  1. (setq wallinfo (KDUB:replace-assocVal 'len  280.0 wallinfo)
  2. ;or
  3. (KDUB:replace-assoc 'len  280.0 'wallinfo)
  4.  

@mailmaverick
Did you mean to assign the key values as Symbols like that ??
« Last Edit: December 18, 2015, 07:37:40 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Modify 2nd element of Dotted Pair List
« Reply #9 on: December 19, 2015, 09:36:15 AM »
@mailmaverick
Did you mean to assign the key values as Symbols like that ??

In my list, name shall always be STRING such as LEN, HGT etc. and associated value shall be real number.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Modify 2nd element of Dotted Pair List
« Reply #10 on: December 19, 2015, 11:08:24 AM »
Therefore your list will look like:
(setq wallinfo (list sublist (cons "LEN" 240.0) (cons "HGT" 96.0)))
(("LYR" . "WALLS") ("LEN" . 240.0) ("HGT" . 96.0))

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Modify 2nd element of Dotted Pair List
« Reply #11 on: December 20, 2015, 10:57:08 AM »
Thanks to all.

My problem has been solved. !!!!