Best way to slice a long list in sublists

Q&A's, tips, howto's
Post Reply
HPW
Posts: 1377
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Best way to slice a long list in sublists

Post 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
Hans-Peter

HPW
Posts: 1377
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Re: Best way to slice a long list in sublists

Post 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))
	)							
Hans-Peter

fdb
Posts: 50
Joined: Sat Nov 09, 2013 8:49 pm

Re: Best way to slice a long list in sublists

Post 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

HPW
Posts: 1377
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Re: Best way to slice a long list in sublists

Post 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
Hans-Peter

Post Reply