Home > Archive > Tcl > July 2005 > Working with lists and listboxes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Working with lists and listboxes
|
|
| Silas Justiniano 2005-07-29, 9:05 am |
| Hi all!
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?
Thank you! Bye!
| |
| Jonathan Bromley 2005-07-29, 9:05 am |
| 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.
| |
| Silas Justiniano 2005-07-29, 5:05 pm |
| Thank you very much. I think would be better to use an array, because
I'm really working with Authors and AuthorCode, like:
{14 Shakespeare}
{22 Goethe}
{7 {Machado de Assis}}
Thx! Bye!
|
|
|
|
|