comp.lang.tcl
[Top] [All Lists]

Re: How to turn a very long list into smaller lists

Subject: Re: How to turn a very long list into smaller lists
From: Jonathan Bromley
Date: Fri, 30 May 2008 15:56:24 +0100
Newsgroups: comp.lang.tcl

On Fri, 30 May 2008 07:30:40 -0700 (PDT), Mel wrote:

>I have a list that is very long and I need to break it into smaller
>lists of say 50 elements (the original list is a muliple of 50.
>
>as for now I am just using lrange function which is extremy slow on a
>long list of say 100000 items.

On my not-very-fast Windows PC, this proc breaks up a 
list of 1,000,000 elements into a list of 20,000 fifty-element
lists, in about an eighth of a second.  Is that too slow for
you?  I was quite impressed...

proc split_list {L {n 50}} {
  incr n 0; # thanks to RS for this cool "is it an int" check!
  set result {}
  set limit [expr {[llength $L] - $n}]
  for {set p 0} {$p <= $limit} {incr p $n} {
    lappend result [lrange $L $p [expr {$p+$n-1}]]
  }
  return $result
}

What are you doing that is "extremely slow" ?  I suspect
you are doing something that causes the list to be 
shimmered back and forth between list and string 
representation internally.
-- 
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@xxxxxxxxxxxxx
http://www.MYCOMPANY.com

The contents of this message may contain personal views which 
are not the views of Doulos Ltd., unless specifically stated.

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy