Author Topic: Items in list  (Read 1546 times)

0 Members and 1 Guest are viewing this topic.

debgun

  • Guest
Items in list
« on: February 08, 2010, 02:48:59 PM »
Is there a way to figure out how many items are in a list?  I'm trying to use a counter that will loop through code until the "end of list".  Here is what I've got so far.

Code: [Select]
(setq colnum 5)
(For colnum to "end of list"
((eq (nth colnum Header) (vla-get-tagstring att))
(vla-put-textstring att (nth colnum data)))
(setq colnum (1+ colnum))

Thanks!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Items in list
« Reply #1 on: February 08, 2010, 02:50:17 PM »
Quote
Command: (length '(1 2 3))
3

You can also use ' foreach '.

Code: [Select]
(foreach n list
    (... do something to n )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Items in list
« Reply #2 on: February 08, 2010, 03:00:55 PM »
Don't understand exactly what you are doing but like Tim suggested use the foreach
Code: [Select]
(setq HeaderList '("A" "B" "C"))
(setq colnum 5)
(Foreach Header HeaderList
  (if (eq Header (vla-get-tagstring att))
    (vla-put-textstring att (nth colnum data)))
  )
  (setq colnum (1+ colnum))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Items in list
« Reply #3 on: February 08, 2010, 03:05:57 PM »
Using the while
Using an index
Code: [Select]
(setq HeaderList '("A" "B" "C"))
(setq colnum 5) ; count up from 5, must be > 5 in the list
(while (setq Header (nth colnum HeaderList))
  (if (eq Header (vla-get-tagstring att))
    (vla-put-textstring att (nth colnum data)))
  )
  (setq colnum (1+ colnum))
)

Using an offset index
Code: [Select]
(setq HeaderList '("A" "B" "C"))
(setq colnum 5) ; count up from 0
(while (setq Header (nth (- 5 colnum) HeaderList))
  (if (eq Header (vla-get-tagstring att))
    (vla-put-textstring att (nth colnum data)))
  )
  (setq colnum (1+ colnum))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: Items in list
« Reply #4 on: February 08, 2010, 03:24:42 PM »
One more, 'mapcar' with counter:

Code: [Select]
(setq i -1)
(mapcar
  (function
    (lambda (x) (setq i (1+ i))
      (if (eq x (vla-get-TagString att))
        (vla-put-TextString att (nth i data))))) HeaderList)

debgun

  • Guest
Re: Items in list
« Reply #5 on: February 08, 2010, 03:34:21 PM »
Thank you for all your help!