Re: empty list




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!

.



Relevant Pages

  • empty list
    ... I have a script with a function called "get_list" which fills a list ... proc get_list {prompt} { ... set module [string trim $module] ... set module ...
    (comp.lang.tcl)
  • Re: behavioral change of "read" builtin for sh on 8-CURRENT
    ... This script waits for terminal input for each of the above variables (INT, ... On 8-CURRENT if I hit Enter with no input at the prompt the system seems to ... recognize the newline as input and continues to sit there until I hit Enter ... Enter network interface: ...
    (freebsd-questions)
  • behavioral change of "read" builtin for sh on 8-CURRENT
    ... This script waits for terminal input for each of the above variables (INT, ... On 8-CURRENT if I hit Enter with no input at the prompt the system seems to ... recognize the newline as input and continues to sit there until I hit Enter ... Enter network interface: ...
    (freebsd-current)
  • behavioral change of "read" builtin for sh on 8-CURRENT
    ... This script waits for terminal input for each of the above variables (INT, ... On 8-CURRENT if I hit Enter with no input at the prompt the system seems to ... recognize the newline as input and continues to sit there until I hit Enter ... Enter network interface: ...
    (freebsd-questions)
  • Re: behavioral change of "read" builtin for sh on 8-CURRENT
    ... This script waits for terminal input for each of the above variables (INT, ... On 8-CURRENT if I hit Enter with no input at the prompt the system seems to ... recognize the newline as input and continues to sit there until I hit Enter ... Enter network interface: ...
    (freebsd-current)