Best way to slice a long list in sublists

Q&A's, tips, howto's

Best way to slice a long list in sublists

Postby HPW » Tue Oct 24, 2017 7:47 pm

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
 
Posts: 1352
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany

Re: Best way to slice a long list in sublists

Postby HPW » Tue Oct 24, 2017 8:33 pm

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))
   )                     
Hans-Peter
HPW
 
Posts: 1352
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany

Re: Best way to slice a long list in sublists

Postby fdb » Tue Oct 24, 2017 10:22 pm

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
fdb
 
Posts: 34
Joined: Sat Nov 09, 2013 8:49 pm

Re: Best way to slice a long list in sublists

Postby HPW » Wed Oct 25, 2017 7:14 am

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
Hans-Peter
HPW
 
Posts: 1352
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests