Re: newbie: foreach



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.

.



Relevant Pages

  • Re: netbeans 5.0 + extra modules doesnt work
    ... Disturbed> making a java mp3 player. ... without any luck (again the missing lib was ... If it is a NB problem (can't find the libraries, ... for which you are looking does not appear in one of those lists, ...
    (comp.lang.java.programmer)
  • Re: Getting started on the HP49g+
    ... either the original HP extable lib ... extable2 lib from http://www.hpcalc.org/details.php?id=3940. ... ask again if you have any problem installing libraries. ... compile the string with ASM (listed in the development lib menu, ...
    (comp.sys.hp48)
  • Re: How to "import" an interface into a module w/o `include?
    ... libs but not one file per lib. ... do you mean that you have used 'vlog' to compile your source files ... into ModelSim libraries of those names? ... could possibly know it's an interface). ...
    (comp.lang.verilog)
  • Re: RosAsm is a broken pile of crap
    ... armed with a macro assembler that's ... Even turbo pascal supported libraries (in the form of .tpu ... > Either the Lib is open source and free reuse. ... To use it in RosAsm, you'd have to convert it all to assembly. ...
    (alt.lang.asm)
  • Re: Freebsd-7.4 + std gcc 4.2.1 fails to honour -march=i586
    ... all shared libraries too. ... matched set for the right CPU & release etc. ... But no reason we coudn't have an infrastructure of lib ... It would prevent gcc -march=586 silently failing, ...
    (freebsd-hackers)