Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there any way to pass a list to a proceedure?

Status
Not open for further replies.

rubberscissors

Technical User
Oct 11, 2001
21
US
I'm calling a proceedure that needs to be passed a series of variables, and I want one of the variables passed to be a list.

Basically, I have a while loop and for each iteration of the loop at one point it checks a series of radiobuttons and makes a list based on each one which has been selected. So, the list varies in length and content, but each iteration also calls a proceedure which will make use of the elements on the list...I want to pass the list to the proceedure at each iteration of the loop...is there a way to do this? Each time I try, it complains there is 'no such variable'...which I think is because it's not a variable but a list. Any thoughts?
 

set R_list {
"elementone"
"element two"
"elementthree"
}
set e 0
proc bogus {var var_list} {
global e
while {[button_button] > 0} {
foreach elem [lsort $var_list] {
puts "$elem is part of $var_list."
if {[string compare $elem $var] == 0} {
upvar #0 $elem mat[incr e]
}
}
}
}

bogus elementone $R_list

This is a bs example but it details that you can process
the output of a fictional procedure that reads data from
buttons > 0 ; sorts the list data and then compares them to declared string var.
If matching the list element var is declared in the global scope. In theory.
I have never had any luck with upvar #0, but the info on upvar is worth reading to understand the stack levels.

Maybe one of the big wigs can help you more.
 
Well, the simple answer is that there's no problem passing a list as an argument to a procedure. For all practical purposes, a list is simply a white-space separated sequence of elements stored as a string. (It's actually handled a bit more efficiently in the Tcl internals, but that's the way you should regard lists from a scripting point of view.) So, anywhere you could pass a string, you could also pass a list.

Here's a simple example:

Code:
proc lreverse { list } {
    set newlist {}
    set i [llength $list]
    for { incr i -1 } { $i >= 0 } { incr i -1 } {
        lappend newlist [lindex $list $i]
    }
    return $newlist
}

This defines lreverse as a procedure that accepts one argument, which happens to be a list. The list provided as an argument is stored in the variable list. (Note that I didn't have to choose the name "list"; it could have been any variable name at all.) Here's a couple examples of calling this procedure interactively:

[tt]% lreverse {one two three four}
four three two one
% set states [ignore][list California "New York" Indiana][/ignore]
California {New York} Indiana
% lreverse $states
Indiana {New York} California[/tt]

In both cases, I'm passing a single argument to lreverse, but the argument happens to be a list. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top