Re: Working with lists and listboxes
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Working with lists and listboxes

From: Jonathan Bromley <jonathan.bromley@doulos.com>
Date: Fri Jul 29 2005 - 16:05:41 CEST

On 29 Jul 2005 05:57:29 -0700, "Silas Justiniano"
<silasju@gmail.com> wrote:

>I have a [listbox -listvariable mylist], but there is a problem. mylist
>has the following structure:
>
>{car owner}
>{Porsche Jack}
>{Ferrari Christopher}
>{Mustang Silas}
>
>I just want to show the owner in listvariable. I have just one
>solution:
>
>set $owner {}
>foreach rows $mylist {
> lappend owner [lindex $rows 1]
>}
>
>Do you know a faster way to do this? Is it necessary to put $mylist
>into a loop?

What do you want to do with "mylist"? Would it instead be
possible to have two separate, parallel lists? If so, you could
make one of those lists be the -listvariable of the list box.
Then instead of stepping through "mylist" you can step through
the two lists in parallel, using the excellent multiple-index
feature of [foreach]:

  set carList {Porsche Ferrari Mustang}
  set ownerList {Jack Christopher Silas}

  foreach car $carList owner $ownerList {
    # scan both lists in parallel
    puts "$owner owns $car"
  }

Alternatively, perhaps you could create an array of owners indexed
by cars, and then your list of cars could be obtained using
[array names owners].

The preferred solution will be determined by what you need to do with
the data. If the data is essentially fixed, then the array trick
may be easiest. If the data is dynamically altering, then it's
probably better to use the parallel-list scheme so that your
listbox automatically updates whenever the "car list" is changed.
[trace variable] can be your friend too.

-- 
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL, Verilog, SystemC, Perl, Tcl/Tk, Verification, Project Services
Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, BH24 1AW, UK
Tel: +44 (0)1425 471223          mail:jonathan.bromley@doulos.com
Fax: +44 (0)1425 471573                Web: http://www.doulos.com
The contents of this message may contain personal views which 
are not the views of Doulos Ltd., unless specifically stated.
Received on Thu Sep 29 14:27:56 2005