Author Topic: Using list in function  (Read 1345 times)

0 Members and 1 Guest are viewing this topic.

civil.eng

  • Newt
  • Posts: 66
Using list in function
« on: May 29, 2021, 04:06:56 AM »
Hi everyone,
I'm trying to write a function to populate list items in each popuplist :

Code: [Select]
(defun setpop ()
(setq poplist (list '("popup1" "alist")
      '("popup2" "plist")
      '("popup3" "plist")
      '("popup4" "slist")
)
  )

  (foreach pop poplist
    (start_list (car pop))
    (mapcar 'add_list (cadr pop))
    (end_list)
  )
)



But it gives this error :
; error: bad argument type: listp "alist"

For example "alist" items are :
(setq alist '("Orchard" "Wall" "Pool" "Building"))

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Using list in function
« Reply #1 on: May 29, 2021, 05:04:41 AM »
Code - Auto/Visual Lisp: [Select]
  1.     (start_list <key_of_popup_list>)
  2.     (mapcar 'add_list (mapcar 'cadr poplist))
  3.     (end_list)
  4.  

civil.eng

  • Newt
  • Posts: 66
Re: Using list in function
« Reply #2 on: May 29, 2021, 05:28:11 AM »
Thank you Tharwat, But I don't want to add all cadr item of list to popup list, I want to populate items of realted list in loop , like:

<popup1 > items of alist

<popup2 > items of plist
 
...

...

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Using list in function
« Reply #3 on: May 29, 2021, 05:30:28 AM »
Simply replace cadr with car

civil.eng

  • Newt
  • Posts: 66
Re: Using list in function
« Reply #4 on: May 29, 2021, 05:42:51 AM »
Tharwat, there are several popup lists and lists , and the loop that I have written must populate each list to each popup.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Using list in function
« Reply #5 on: May 29, 2021, 05:47:30 AM »
Code - Auto/Visual Lisp: [Select]
  1. (foreach key '("key1" "key2" "key3") ;; add all keys of your popup_lists
  2.   (start_list key)
  3.   (mapcar 'add_list (mapcar 'car poplist))
  4. )
  5.  

civil.eng

  • Newt
  • Posts: 66
Re: Using list in function
« Reply #6 on: May 29, 2021, 05:53:36 AM »
Wrong again, that should be like :

(foreach key '("popup1" "popup2" "popup3") ;; add all keys of your popup_lists
  (start_list key)
  (mapcar 'add_list (cadr poplist))
  (end_list)
)

(cadr poplist) are items of a list like "alist"

(setq alist '("Orchard" "Wall" "Pool" "Building"))
(setq plist '("region1" "region2" "region3"))
(setq slist '("riverNG" "riverTH" "riverCR"))

popup1 > alist
popup2,popup3 > plist
popup3 > slist
« Last Edit: May 29, 2021, 06:01:47 AM by civil.eng »

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Using list in function
« Reply #7 on: May 29, 2021, 06:00:45 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun setpop (/ poplist)
  2.   (setq poplist (list '("popup1" "alist")
  3.                       '("popup2" "plist")
  4.                       '("popup3" "plist")
  5.                       '("popup4" "slist")
  6.                 )
  7.   )
  8.   (foreach pop (mapcar 'car poplist)
  9.     (start_list pop)
  10.     (mapcar 'add_list (cdr (assoc pop poplist)))
  11.     (end_list)
  12.   )
  13. )
  14.  

civil.eng

  • Newt
  • Posts: 66
Re: Using list in function
« Reply #8 on: May 29, 2021, 06:10:08 AM »
Yes this is right, but it shows only item of "(cdr (assoc pop poplist)" , it must show items of each list like "alist"

(setq alist '("Orchard" "Wall" "Pool" "Building"))
(setq plist '("region1" "region2" "region3"))
(setq slist '("riverNG" "riverTH" "riverCR"))

popup1 > alist
popup2,popup3 > plist
popup3 > slist

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Using list in function
« Reply #9 on: May 29, 2021, 06:22:37 AM »
You should have learned a lot for now from all these different scenarios.  :grinwink:
Be sure to have the variables 'alist' 'plist' ...etc assigned with list of items to avoid errors and failure.

Code - Auto/Visual Lisp: [Select]
  1. (defun setpop (/ poplist)
  2.   (setq poplist (list (list "popup1" alist)
  3.                       (list "popup2" plist)
  4.                       (list "popup3" plist)
  5.                       (list "popup4" slist)
  6.                 )
  7.   )
  8.   (foreach pop (mapcar 'car poplist)
  9.     (start_list pop)
  10.     (mapcar 'add_list (cadr (assoc pop poplist)))
  11.     (end_list)
  12.   )
  13. )

civil.eng

  • Newt
  • Posts: 66
Re: Using list in function
« Reply #10 on: May 29, 2021, 06:30:15 AM »
Bravo, you did great

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Using list in function
« Reply #11 on: May 29, 2021, 05:10:52 PM »
The current code is processing the list in the following way:
  • Obtain a list containing the first elements of the original list
  • Iterate over the list of first elements
  • Use the first element to obtain each item from the original list
  • Add the contents of each item to the popup list tile

Instead, you can iterate over the list in a single pass in the following way:
Code - Auto/Visual Lisp: [Select]
  1. (defun setpop ( )
  2.     (foreach lst
  3.         (list
  4.             (list "popup1" alist)
  5.             (list "popup2" plist)
  6.             (list "popup3" plist)
  7.             (list "popup4" slist)
  8.         )
  9.         (start_list  (car  lst))
  10.         (foreach itm (cadr lst) (add_list itm))
  11.         (end_list)
  12.     )
  13. )

But it's actually likely to be more efficient to simply do this:
Code - Auto/Visual Lisp: [Select]
  1. (defun setpop ( )
  2.     (dclst "popup1" alist)
  3.     (dclst "popup2" plist)
  4.     (dclst "popup3" plist)
  5.     (dclst "popup4" slist)
  6. )
  7. (defun dclst ( key lst )
  8.     (start_list key)
  9.     (foreach itm lst (add_list itm))
  10.     (end_list)
  11. )
« Last Edit: May 29, 2021, 05:13:59 PM by Lee Mac »

civil.eng

  • Newt
  • Posts: 66
Re: Using list in function
« Reply #12 on: May 30, 2021, 04:40:10 PM »
Woooov, you are fabulous. thank you lee mac