Page 1 of 1

Best way to slice a long list in sublists

PostPosted: Tue Oct 24, 2017 7:47 pm
by 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

Re: Best way to slice a long list in sublists

PostPosted: Tue Oct 24, 2017 8:33 pm
by HPW
My current code:
Code: Select all
   (setq counter 0)
   (dotimes (xx (/(length slotlst)paramnumber))
      (setq newlst (append newlst (list(slice slotlst counter paramnumber))))
      (setq counter (+ counter paramnumber))
   )                     

Re: Best way to slice a long list in sublists

PostPosted: Tue Oct 24, 2017 10:22 pm
by fdb
Code: Select all
(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:

Code: Select all
(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

Re: Best way to slice a long list in sublists

PostPosted: Wed Oct 25, 2017 7:14 am
by HPW
Hello ferry,

Thanks a lot for the tip. I wasn't aware of that nice command.
Code: Select all
 (explode slotlist paramnumber)

Works like a charm.

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