Best way to slice a long list in sublists

Started by HPW, October 24, 2017, 12:47:37 PM

Previous topic - Next topic

HPW

Hello,



I have a long flat list of params parsed from a csv file.

For example it has for eample 49 items and I know that they are a mulitple of 7 elements.



What is the most elegant way to slice the list into a list with 7 sublists with each 7 items?

In a loop with (slice 0 7) and so on?



Regards

Hans-Peter
Hans-Peter

HPW

#1
My current code:

(setq counter 0)
(dotimes (xx (/(length slotlst)paramnumber))
(setq newlst (append newlst (list(slice slotlst counter paramnumber))))
(setq counter (+ counter paramnumber))
)
Hans-Peter

fdb

#2
(explode slotlist 7)

There are a *lot* of usefull functions standard in newlisp ;-)



And if there wasn't such a function i would write something like:


(define (my-exp llst number)
  (if (empty? llst)
      '()
      (cons (0 number llst) (my-exp (number llst) number))))


Who doesn't like recursion and implicit indexing ! ;-)



Mvg

Ferry

HPW

#3
Hello ferry,



Thanks a lot for the tip. I wasn't aware of that nice command.

(explode slotlist paramnumber)

Works like a charm.


Quote
Who doesn't like recursion and implicit indexing ! ;-)

Jup, don't teach a old horse new tricks.

Working mainly in Autolisp let me often forget the nice things from newlisp. ;-)



Regards

Hans-Peter
Hans-Peter