Re: empty list
- From: "bs" <brett.schwarz@xxxxxxxxx>
- Date: 6 Apr 2006 05:52:32 -0700
slebetman@xxxxxxxxx wrote:
Niv (KP) wrote:
Niv (KP) wrote:
Hi peeps,
I have a script with a function called "get_list" which fills a list
with names,
and ends when a blank or fullstop are entered.
However, sometimes the list needs to be empty, but the whole script
falls over
when I try this.
How do I handle a null list please?
First thoughts are to force the list with an entry, like "empty_list"
then the first pass
changes the first entry & subsequent passes use the lappend.
Then, when I use the list, if it is only "empty_list" I escape the
process(s) that uses the list.
But I'm sure there's a better way.
Function below:
#----------------------------------------------------------------------------------
proc get_list {prompt} {
# set mylist empty_list
while 1 {
puts -nonewline $prompt
flush stdout
gets stdin module
set module [string trim $module]
set module [string toupper $module]
if {$module == "." || $module == ""} break
# Validate the module name is between 3 & 10 chars long
# reject if it is outside these limits.
user_pause {1}
lappend mylist $module
}
return $mylist
}
#----------------------------------------------------------------------------------
Thanks for help in the past, Kev P.
OK, fixed it myself by just adding the following:
set mylist [list]
how easy was that!
It's easier than you think. In Tcl, an empty list is the same as an
empty string.
% set mylist ""
% llength $mylist
0
The beauty of the everything-is-a-string concept is that nothing is
opaque (well.. except arrays). Everything is composed from a string.
In Tcl:
character = any 8 or 16 bit value (16 bit for some unicode symbols)
word = a group of characters
string = a group of words
list = a specially formatted string*
Note that the special formatting of a list is nothing more than
separation by whitespace and obeying quoting rules. There is absolutely
nothing magical about a list:
% set foo "this is a string {as well as} a list"
% lindex $foo 4
as well as
% lindex $foo 4 1
well
If a string is absolutely under your control, don't be afraid to treat
it as a list. When you accept a string from the outside world then you
need to be careful as not all strings are valid lists.
Conceptually, that might be true, but you might not want to rely on
that. Internally, a list is actually inplemented as an C array I
believe. So, if you have a string and do list operations on it, it will
shimmer. It's probably worthwhile to convert the string to a list
first, either by using split, or even better, something like:
set listfromstr [regexp -all -inline {\S+} $str]
.
- Follow-Ups:
- Re: empty list
- From: slebetman@xxxxxxxxx
- Re: empty list
- From: suchenwi
- Re: empty list
- References:
- empty list
- From: Niv (KP)
- Re: empty list
- From: Niv (KP)
- Re: empty list
- From: slebetman@xxxxxxxxx
- empty list
- Prev by Date: Re: What happened to spectcl?
- Next by Date: Re: Tcl_CreateTrace problem
- Previous by thread: Re: empty list
- Next by thread: Re: empty list
- Index(es):
Relevant Pages
|