Author Topic: how can i combine each item in multiple lists to return at the same time  (Read 1860 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
i hope i can explain this right...
i dont have any code, im just messing around learning things with some down time.

i have 2 lists (lista and listb)
i want to return each item in each list at the same time.
here is my thought
Code: [Select]
(foreach x lista
  (setq var1 x)
)
(foreach z listb
(setq var2 z)
)
(princ (strcat var1 " is list a" var2 "is list b"))

how can i combine each item in 2 lists to return at the same time
am i in the right church, wrong pew?

ribarm

  • Gator
  • Posts: 3251
  • Marko Ribar, architect
Re: how can i combine each item in multiple lists to return at the same time
« Reply #1 on: September 16, 2014, 12:35:41 PM »
Code: [Select]
(if
  (and
    (vl-every '(lambda ( x ) (eq (type x) 'STR)) lista)
    (vl-every '(lambda ( x ) (eq (type x) 'STR)) listb)
  )
  (mapcar '(lambda ( a b ) (strcat a " is list a; " b " is list b)) lista listb)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: how can i combine each item in multiple lists to return at the same time
« Reply #2 on: September 16, 2014, 02:18:09 PM »
mapcar is key; using the prin* functions allows for any data type:

Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda ( a b ) (print a) (princ "is list a ") (prin1 b) (princ " is list b")) lista listb)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: how can i combine each item in multiple lists to return at the same time
« Reply #3 on: September 17, 2014, 12:43:01 AM »
Marko , you can append the two lists in one to check for their types .


Code - Auto/Visual Lisp: [Select]
  1. (vl-every '(lambda ( x ) (eq (type x) 'STR)) (append lista listb))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how can i combine each item in multiple lists to return at the same time
« Reply #4 on: September 17, 2014, 01:40:44 AM »
Just a little question? Are those 2 lists always the same length? If not what should happen to the items found in one but not the other?

Mapcar simply ignores them (only iterates over the shortest list), so if you want something else then perhaps you might need to go about this a different way. E.g. this would print nil in place of the "missing" items from the shortest list.
Code - Auto/Visual Lisp: [Select]
  1. (setq tempa lista tempb listb)
  2. (while (or tempa tempb)
  3.   (print (car tempa))
  4.   (princ " is list a ")
  5.   (prin1 (car tempb))
  6.   (princ " is list b")
  7.   (setq tempa (cdr tempa) tempb (cdr tempb))
  8. )
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: how can i combine each item in multiple lists to return at the same time
« Reply #5 on: September 17, 2014, 08:13:15 AM »
Just a little question? Are those 2 lists always the same length? If not what should happen to the items found in one but not the other?


Good question Irné  :)

andrew_nao

  • Guest
Re: how can i combine each item in multiple lists to return at the same time
« Reply #6 on: September 17, 2014, 01:46:04 PM »
Just a little question? Are those 2 lists always the same length? If not what should happen to the items found in one but not the other?

Mapcar simply ignores them (only iterates over the shortest list), so if you want something else then perhaps you might need to go about this a different way. E.g. this would print nil in place of the "missing" items from the shortest list.
Code - Auto/Visual Lisp: [Select]
  1. (setq tempa lista tempb listb)
  2. (while (or tempa tempb)
  3.   (print (car tempa))
  4.   (princ " is list a ")
  5.   (prin1 (car tempb))
  6.   (princ " is list b")
  7.   (setq tempa (cdr tempa) tempb (cdr tempb))
  8. )

i didnt even think about that. i dunno if they would be its nothing important im only messing around with code learning from the errors and thinking of scenarios and how to apply different functions and such