Re: empty list




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]

.



Relevant Pages

  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • Re: empty list
    ... The most important readers of the source code you write, are people, ... variable to be an empty list to which I am going to append items, ... Everything is composed from a string. ... to obfuscate their code in wacky ways because, don't you know, lists ...
    (comp.lang.tcl)
  • ANN: MeObjects Library for Delphi
    ... object type small and powerful. ... the Object instance can use the ClassType method return the ... Especially for lists of pointers to dynamically allocated memory. ... {Summary Adds Ansi String and correspondent object to a list. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: problem evaluating if { ! [file exists $DIRVAL]} inside script
    ... I wrote the following code in a small script and verified it worked. ... array set mydirlist {[list $string]} ... set currpath [concat $currpath1$mydirlist] ... Even though it is documented to accept strings, it treats the strings as lists and often doesn't do what you intend. ...
    (comp.lang.tcl)
  • Filling a grid with Sharepoint data
    ... /// Retrieves a Lists web service proxy with default settings. ... string strComputerName = System.Net.Dns.GetHostName.ToLower; ... XmlDocument xmlDoc, ... XmlElement elementMethod, ...
    (microsoft.public.dotnet.framework.windowsforms)