Forum OpenACS Q&A: foreach..lindex...behavior??

Collapse
Posted by arief zj on
Hi there..
I wrote this code:

1  set db [ns_db gethandle]
2  set sql_account "select accd_stat from table_a"
3  set accd_lists [database_to_tcl_list_list $db $sql_account]
4  set bbb ""
5
6  foreach accd $account_lists {
7    #set status1 "unstart"
8    set status1 [lindex $accd 0]
9    if { $status1 == "unstart" } {
10       set ccc "yess"
11   } else {
12       set ccc "damn"
13   }
14 
15   append bbb "$status1 >> $ccc
" 16 }
and they produce this output : unstart >> damn question is: Why the if statement failed although the variable hold the same content as the specified condition ??? and if I commented line8 and then uncomment line7, they works .... any ideas??? -Silly me-
Collapse
Posted by Esti Alvarez on
I think you want to use database_to_tcl_list instead of database_to_tcl_list_list
Collapse
Posted by arief zj on
but i think, database_to_tcl_list is not applicable whenever I expand
the sql query like:


select accd_id, accd_name, accd_date, accd_stat from table_a


bcoz it will fetch the accd_id only right?? 
any idea ???
Collapse
Posted by Prashant Khandelwal on
I belive this should work for you (line #8)

set status1 [lindex [lindex $accd 0] 0]

Collapse
Posted by Pascal Scheffers on
try replacing { $status1 == "unstart" } with { [string equal $status1 "unstart"] }. It's more typing, but that way you are SURE that tcl is doing a string compare. Comparing empty variables with something else produces weird results. Don't know why, prolly something to do Tcl being a loosely typed language.
Collapse
Posted by James Thornton on
Unless you are using Tcl 8.2.3 or higher, you probably won't have the string equal command. In that case upgrade to Tcl 8.3.4, or use [string compare string1 string2] == 0 instead.