Hm. I'm seeing several things going on here, not all of which are related to your question...
For starters, let's look at your code comment "# only take the first column & store in a list". However, you're not storing the information in a
list, you're storing it in a Tcl
array, which is quite a different data structure. You might find it instructive to read my comments from thread287-250855 "Array uniq, without list translation," to learn more about the distinctions between a list and an array in Tcl. (
marsd should find this a familiar thread.

)
Continuing to review your code, I notice that you're trying to store the filenames in an integer-indexed array. If you read the thread I referenced above, you'll realize that this is a situation where a true Tcl list is far more efficient, and so if at all possible, you'll want to convert your code to using a true list instead of an array.
Another comment, I notice that you used the following to increment a variable:
Code:
set nbdir [expr $nbdir+1]
As
marsd showed in his code, it's much better to use Tcl's
incr command to increment the value of a variable (as long as both the orginal value and the increment are integers). The "gotcha" to look out for with
incr is that it takes the
name of a variable as an argument (no $ in front of the variable name). So the following is equivalent to the line above:
(The code fragment provided by
marsd also takes advantage of the fact that the return value of
incr is the new value that was assigned to the variable.)
As for the core question you asked, you're encountering a classic problem of trying to treat an arbitrary string as a list. In Tcl, a
list is a whitespace sequence of
elements, and an
element can be any string value. So what do you do if an element contains whitespace characters? You quote it, using the exact same quoting rules as when quoting a Tcl command string. So:
Code:
France Germany United Kingdom
is a 4 element list, but:
Code:
France Germany {United Kingdom}
is a 3 element list, where the third element is the string "United Kingdom". Notice that the quoting characters themselves are not considered part of the element value:
[tt]%
set countries {France Germany {United Kingdom}}
France Germany {United Kingdom}
%
lindex $countries 2
United Kingdom[/tt]
However,
all of the Tcl quoting rules apply to lists. So look what happens in the following case:
[tt]%
[ignore]set dir [file nativename [pwd]][/ignore]
D:\tcltk
%
lindex $dir 0
D: cltk[/tt]
In this case, the "\" in front of the "t" was treated as a Tcl escape character, which caused the "\t" sequence to be translated into a literal Tab character when parsed as a list. You've also got problems if the string isn't a well-formed Tcl list, for example, if it contains unbalanced quoting characters:
[tt]%
set test "This {is a test"
This {is a test
%
lindex $test 0
unmatched open brace in list[/tt]
Using your current approach, you'd also run into problems if you have spaces in your file names. So, if at all possible, you should really come up with another way of extracting the data from your file. But it's difficult to suggest alternatives without knowing what your data format looks like.
Once you've got a value that you'd like to add to a list, there are a few Tcl commands you could use to add the value as an element of a list. You might find
lappend useful, which adds one or more elements to the end of a list already stored in a variable. The first argument is the variable
name (once again, you won't want a "$" in front of the variable name), and each subsequent argument is added as a distinct element at the end of the list. A nice feature of
lappend is that it automatically creates the variable for you if it doesn't already exist. So, here's a quick example:
[tt]%
lappend vals France Germany "United Kingdom"
France Germany {United Kingdom}
%
lappend vals "bad {value"
France Germany {United Kingdom} bad\ \{value
%
lindex $vals 3
bad {value[/tt] - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax