Author Topic: Manipulating lists  (Read 1999 times)

0 Members and 1 Guest are viewing this topic.

Denzuki

  • Guest
Manipulating lists
« on: September 11, 2015, 02:40:39 PM »
I am trying to create an alphanumerical list from 3 previous lists also alphanumerical.

i.e. list 1 - ad1, ad2, ad3.....
list 2 - gd01, gd02, gd03....
list 3-  b1, b2, b3......

 I want to create a string from the list above that would read ad1-gd01-b1; but I also need to create a list that would include ad1-gd02-b1; ad1-gd03-b1; etc.

Below is part of the code:
  (setq a 0)
  (setq b 0)
  (setq c 0)
  (vl-load-com)
  (setq alpha (vlax-make-safearray vlax-vbString '(0 . 4)))
  (vlax-safearray-fill alpha '("ad1" "ad2" "ad3" "ad4" "ad5"))
  (setq beta (vlax-make-safearray vlax-vbString '(0 . 4)))
  (vlax-safearray-fill beta '("gd01" "gd02" "gd03" "gd04" "gd05"))
  (setq gamma (vlax-make-safearray vlax-vbString '(0 . 4)))
  (vlax-safearray-fill gamma '("b1" "b2" "b3" "b4" "b5"))
  (setq alphalist (vlax-safearray->list alpha))
  (setq betalist (vlax-safearray->list beta))
  (setq gammalist (vlax-safearray->list gamma))

    (while (< b 3)   
    (setq newstr (strcat (nth a alphalist) "-" (nth b betalist) "-" (nth c gammalist)" "))
    (setq newlist (list newstr))
    (princ newlist);review
    (setq b (+ b 1))
    )

This is probably very simple and I'm too close to it. Any help would be appreciated.

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: Manipulating lists
« Reply #1 on: September 11, 2015, 03:50:41 PM »
Code: [Select]
(defun mklst ( / alst blst clst x itm lst )
  (setq alst '("ad1" "ad2" "ad3" "ad4" "ad5")
        blst '("gd01" "gd02" "gd03" "gd04" "gd05")
        clst '("b1" "b2" "b3" "b4" "b5")
  )
  (setq x -1)
  (while (< x 3)
    (setq x (1+ x))
    (setq itm (strcat (nth x alst) "-" (nth x blst) "-" (nth x clst)))
    (setq lst (cons itm lst))
  )
  (reverse lst)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Manipulating lists
« Reply #2 on: September 11, 2015, 04:16:33 PM »
I think this:
Code: [Select]
(while (< x 3)should be:
Code: [Select]
(while (< x 4)

Tharwat

  • Swamp Rat
  • Posts: 712
  • Hypersensitive
Re: Manipulating lists
« Reply #3 on: September 11, 2015, 04:17:48 PM »
This ?

Code - Auto/Visual Lisp: [Select]
  1. (setq lst-1 '("ad1" "ad2" "ad3")
  2.       lst-2 '("gd01" "gd02" "gd03")
  3.       lst-3 '("b1" "b2" "b3")
  4. )
  5. (mapcar '(lambda (a b c)
  6.            (setq l1 (cons (strcat a "-" b "-" c) l1)
  7.                  l2 (cons (strcat (car lst-1) "-" b "-" (car lst-3)) l2)
  8.            )
  9.          )
  10.         lst-1
  11.         lst-2
  12.         lst-3
  13. )
  14. (mapcar 'reverse (list l1 l2))

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: Manipulating lists
« Reply #4 on: September 11, 2015, 04:28:24 PM »
I think this:
Code: [Select]
(while (< x 3)should be:
Code: [Select]
(while (< x 4)

Yes, you mean :
Code: [Select]
(while (< (setq x (1+ x)) 3)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Denzuki

  • Guest
Re: Manipulating lists
« Reply #5 on: September 11, 2015, 05:22:05 PM »
Thank you all for your quick responses.

The "3" in (while (< x 3) in this case could be any number, depending on what was needed. I shortened the number of list items for simplicity sake.

Tharwat

  • Swamp Rat
  • Posts: 712
  • Hypersensitive
Re: Manipulating lists
« Reply #6 on: September 11, 2015, 05:44:49 PM »
Did not you try my codes ?

Denzuki

  • Guest
Re: Manipulating lists
« Reply #7 on: September 11, 2015, 06:24:20 PM »
Thank you ribarm and Tharwat for your responses.
They both works very well.

Tharwat

  • Swamp Rat
  • Posts: 712
  • Hypersensitive
Re: Manipulating lists
« Reply #8 on: September 11, 2015, 06:26:03 PM »
Thank you ribarm and Tharwat for your responses.
They both works very well.

Good to hear , you're welcome.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Manipulating lists
« Reply #9 on: September 12, 2015, 05:51:55 AM »
Yes, you mean :
Code: [Select]
(while (< (setq x (1+ x)) 3)
No. There are 5 elements in each list. The while loop in your original code will handle these values of x: -1, 0, 1, and 2. The resulting list will therefore have 4 items. With your modification the resulting list will only have 3 items.

Of course Tharwat's suggestion is the most efficient. But I am unsure if Denzuki is familiar with mapcar since he seems to be coming from a VBA background.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Manipulating lists
« Reply #10 on: September 12, 2015, 06:54:29 AM »
Why not use the output from mapcar directly? e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( a b c )
  2.     (list
  3.         (mapcar '(lambda ( a b c ) (strcat a "-" b "-" c)) a b c)
  4.         (mapcar '(lambda ( b ) (strcat (car a) "-" b "-" (car c))) b)
  5.     )
  6. )
Code - Auto/Visual Lisp: [Select]
  1. (setq l1 '("ad1" "ad2" "ad3" "ad4" "ad5")
  2.       l2 '("gd01" "gd02" "gd03" "gd04" "gd05")
  3.       l3 '("b1" "b2" "b3" "b4" "b5")
  4. )
  5. _$ (foo l1 l2 l3)
  6. (("ad1-gd01-b1" "ad2-gd02-b2" "ad3-gd03-b3" "ad4-gd04-b4" "ad5-gd05-b5") ("ad1-gd01-b1" "ad1-gd02-b1" "ad1-gd03-b1" "ad1-gd04-b1" "ad1-gd05-b1"))