Re: newbie: foreach
- From: Bryan Oakley <oakley@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 19 Aug 2005 17:21:20 GMT
Niv (KP) wrote:
Thanks for all the help, I've cracked it as follows; (but I'm sure the 3 library lists could be declared as a single list somehow, back to the textbooks).
It's as simple as this:
set libs [list apa act1 act2 act3]
foreach lib $libs {...}
foreach lib $libs {...}
foreach lib $libs {...}There's not much point in doing that, unless there's a chance that you'll add more libraries in the future and you're concerned that you'll forget to update one of the foreach statements.
I think you've made the code harder to understand by using dynamic variable names (eg: set $lib ... if {[set $lib]} ...). I'd recommend arrays, which are perfect for this situation. If yes_no always returns 1 or 0, you can also use a shorter test in the if statements to make the code (arguably) a bit more readable
foreach lib {apa act1 act2 act3} {
# get an answer for each library in the list
set include($lib) [yes_no $lib] # TESTING: verify when the variable has been set
puts "$lib has been set to $include($lib)"
}
foreach lib {apa act1 act2 act3} {
if {$include($lib)} {
puts $hdsnew "$lib = $downstream_lib_path/$lib"
}
}Again, if what you have works there's no real need in changing it. I just don't want you walking away thinking tcl is more difficult than it really is.
.
- References:
- newbie: foreach
- From: Kev P.
- Re: newbie: foreach
- From: Bryan Oakley
- Re: newbie: foreach
- From: Niv (KP)
- Re: newbie: foreach
- From: Bryan Oakley
- Re: newbie: foreach
- From: Niv (KP)
- newbie: foreach
- Prev by Date: Re: text widget must follow end of text only if associated scrollbar is at bottom
- Next by Date: Re: Question on printing a canvas
- Previous by thread: Re: newbie: foreach
- Next by thread: Re: newbie: foreach
- Index(es):
Relevant Pages
|