Author Topic: Merge together the items of several lists  (Read 3249 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Merge together the items of several lists
« on: September 09, 2016, 01:28:26 PM »
Hi guys,
I see here that you like challenges, so heres one:
How I do I merge the items of the following lists:
Code: [Select]
(setq LstA (list "A0" "A1" "A2" "A3" "A4" "A5"))
(setq LstB (list "B0" "B1" "B2" "B3" "B4" "B5"))
(setq LstC (list "C0" "C1" "C2" "C3" "C4" "C5"))
(setq LstD (list "D0" "D1" "D2" "D3" "D4" "D5"))
Like this:
Code: [Select]
(setq LstReturn
(
("A0" "B0" "C0" "D0")
("A1" "B1" "C1" "D1")
("A2" "B2" "C2" "D2")
("A3" "B3" "C3" "D3")
("A4" "B4" "C4" "D4")
)
)
An association list is created upon merge'ing each "nth" item from each list.

I'm trying to write a subfunction like this, to achieve the above result:
(setq LstReturn (MergeNLists (setq NLst (list LstA LstB LstC LstD))))

Where the e NLst argument could take any number if lists to merge their items together.
Code: [Select]
(MergeNlists Nlst)I'm still unsure how to properly name the subfunction, to describe exactly what is achieved.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12919
  • London, England
Re: Merge together the items of several lists
« Reply #1 on: September 09, 2016, 01:41:06 PM »
Code - Auto/Visual Lisp: [Select]
  1. (mapcar 'list LstA LstB LstC LstD)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Merge together the items of several lists
« Reply #2 on: September 09, 2016, 01:51:36 PM »
Hi Lee,
Thats easy task - the main problem is if unknown length of lists is supplied, thats why I think about going with this "list" of lists argument (Nlist), which length may varies:
(MergeNlists Nlist), where (setq Nlist (lst1 lst2 lst3 ... lstN)).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12919
  • London, England
Re: Merge together the items of several lists
« Reply #3 on: September 09, 2016, 01:57:27 PM »
Code - Auto/Visual Lisp: [Select]
  1. (apply 'mapcar (cons 'list nLst))

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Merge together the items of several lists
« Reply #4 on: September 09, 2016, 02:12:19 PM »
Code - Auto/Visual Lisp: [Select]
  1. (apply 'mapcar (cons 'list nLst))
I might repeat myself, but I'll write it again:
 You are a truly List Manipulator!  :yes:

Just a hint about my initial idea/thoughts:
1. get Selectionset by specific entitytype
2. store in different lists different specific properties of the objects
3. construct this big-a#s association list, for further manipulations

Thank you for solving step #3! (1 and 2 were easy)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12919
  • London, England
Re: Merge together the items of several lists
« Reply #5 on: September 09, 2016, 07:18:47 PM »
 :-)