Let's say I have defined a list as follows:Hello,
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most >cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted? I can't >split the list by whitespace, because single elements that contain >whitespace will also be split.
Let's say I have defined a list as follows:
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned.
In most cases, this is desirable, but in my case, I want them to be
returned exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted?
Choosechee <choosechee@pm.me> schrieb:
Let's say I have defined a list as follows:Hello,
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most
cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted? I can't
split the list by whitespace, because single elements that contain
whitespace will also be split.
by using 'llength'?
Like (all on one line)
for {set i 0} {$i < [llength $myList]} {incr i} {puts "$i: [lindex
$myList $i]"}
HTH
Helmut
First\nElement
Second Element
First\nElement
Second Element
First
Element
Helmut Giese wrote:
Choosechee <choosechee@pm.me> schrieb:
Let's say I have defined a list as follows:Hello,
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most
cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted? I can't >>> split the list by whitespace, because single elements that contain
whitespace will also be split.
by using 'llength'?
Like (all on one line)
for {set i 0} {$i < [llength $myList]} {incr i} {puts "$i: [lindex
$myList $i]"}
HTH
Helmut
Hello,
Maybe building an explicit list is safer :
set myList [ list {First\nElement} {Second Element} ]
foreach element $myList { chan puts $element }
First\nElement
Second Element
or if you collect data in different variables :
set a {First\nElement}
set b {Second Element}
set myList [list $a $b]
or
set myList [list $a ]
lappend myList $b
foreach e $myList { chan puts $e }
First\nElement
Second Element
note that this will fail :
set myList $a
foreach e $myList { chan puts $e }
First
Element
Regards
Pascal
Choosechee <choosechee@pm.me> wrote:
Let's say I have defined a list as follows:
`set myList {"First\nElement" {Second Element}}`
You have not defined a list. You have defined a string. It is a
string that happens to be convertable to a list, but it is not a list.
Curly braces {} do not mean "a list" (although this is a common misconception).
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
You did not print a list, you printed a string (it has not undergone conversion to a list yet).
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned.
Yes, now you forced Tcl to convert the string to a list, and the
conversion from string to list also interpreted the backslashes,
because of the double quotes you used in the string.
In most cases, this is desirable, but in my case, I want them to be
returned exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted?
By properly defining a list at the outset (this is the preferred
method):
% set myList [list {First\nElement} {Second Element}]
{First\nElement} {Second Element}
% foreach {element} $myList {puts $element}
First\nElement
Second Element
Or, if you insist on attempting to manually use the "list serialization syntax" for strings (note that the documentation says to not do this)
by defining the string such that the substitutions do not occur:
% set myList {{First\nElement} {Second Element}}
{First\nElement} {Second Element}
% foreach {element} $myList {puts $element}
First\nElement
Second Element
Choosechee <choosechee@pm.me> schrieb:
Let's say I have defined a list as follows:Hello,
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most
cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted? I can't
split the list by whitespace, because single elements that contain
whitespace will also be split.
by using 'llength'?
Like (all on one line)
for {set i 0} {$i < [llength $myList]} {incr i} {puts "$i: [lindex
$myList $i]"}
HTH
Helmut
Let's say I have defined a list as follows:No, the elements have NOT undergone substitution. "\n" when passed to puts it prints a newline. "\n" is just the literal string containing one character, a newline, Nothing is being "substituted" (well, except by the input parser processing string escapes: "... \x ..." ).
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted? I can't split the list by whitespace, because single elements that containYou are going to have to write a version of "puts" that escapes "special" characters (like newline).
whitespace will also be split.
On 3/18/26 15:32, Rich wrote:
Choosechee <choosechee@pm.me> wrote:
Let's say I have defined a list as follows:
`set myList {"First\nElement" {Second Element}}`
You have not defined a list. You have defined a string. It is a
string that happens to be convertable to a list, but it is not a list.
Curly braces {} do not mean "a list" (although this is a common
misconception).
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
You did not print a list, you printed a string (it has not undergone
conversion to a list yet).
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned.
Yes, now you forced Tcl to convert the string to a list, and the
conversion from string to list also interpreted the backslashes,
because of the double quotes you used in the string.
In most cases, this is desirable, but in my case, I want them to be
returned exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted?
By properly defining a list at the outset (this is the preferred
method):
% set myList [list {First\nElement} {Second Element}]
{First\nElement} {Second Element}
% foreach {element} $myList {puts $element}
First\nElement
Second Element
Or, if you insist on attempting to manually use the "list serialization
syntax" for strings (note that the documentation says to not do this)
by defining the string such that the substitutions do not occur:
% set myList {{First\nElement} {Second Element}}
{First\nElement} {Second Element}
% foreach {element} $myList {puts $element}
First\nElement
Second Element
I am actually reading the list from a file. Is this a bad idea?
Should I just parse it from scratch?
At Wed, 18 Mar 2026 14:50:35 -0500 Choosechee <choosechee@pm.me> wrote:
Let's say I have defined a list as follows:
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most
cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
No, the elements have NOT undergone substitution.
Choosechee <choosechee@pm.me> wrote:
On 3/18/26 15:32, Rich wrote:
Choosechee <choosechee@pm.me> wrote:
Let's say I have defined a list as follows:
`set myList {"First\nElement" {Second Element}}`
You have not defined a list. You have defined a string. It is a
string that happens to be convertable to a list, but it is not a list.
Curly braces {} do not mean "a list" (although this is a common
misconception).
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
You did not print a list, you printed a string (it has not undergone
conversion to a list yet).
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned.
Yes, now you forced Tcl to convert the string to a list, and the
conversion from string to list also interpreted the backslashes,
because of the double quotes you used in the string.
In most cases, this is desirable, but in my case, I want them to be
returned exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted?
By properly defining a list at the outset (this is the preferred
method):
% set myList [list {First\nElement} {Second Element}]
{First\nElement} {Second Element}
% foreach {element} $myList {puts $element}
First\nElement
Second Element
Or, if you insist on attempting to manually use the "list serialization
syntax" for strings (note that the documentation says to not do this)
by defining the string such that the substitutions do not occur:
% set myList {{First\nElement} {Second Element}}
{First\nElement} {Second Element}
% foreach {element} $myList {puts $element}
First\nElement
Second Element
I am actually reading the list from a file. Is this a bad idea?
We can't read your mind. It is best to tell us these facts as part of the initial posting.
Whether this is a bad idea depends upon whether you can depend upon the string from the file being properly formatted as a list. In a response
to pmcc you said it was also being created by someone else, which means
if you use Tcl's default conversion, you are dependent upon someone
else always correctly formatting the list encoding. If they get a
single character wrong in just the right place, you'll take an error in
your code.
I.e.:
% set is_it_a_list {a "b c}
a "b c
% lindex $is_it_a_list 1
unmatched open quote in list
Should I just parse it from scratch?
If you can't be sure it is always properly encoded as a string that
Tcl can convert automatically, then yes, it is better to do the parse yourself from scratch.
Let's say I have defined a list as follows:Okay, well, I created some code to brace every element before I use the
`set myList {"First\nElement" {Second Element}}`
If I print this list:
`chan puts $myList`
I get:
`"First\nElement" {Second Element}`
However, if I were to iterate over the list and print the elements:
`foreach {element} $myList {chan puts $element}`
I get:
```
First
Element
Second Element
```
The elements have undergone substitution before being returned. In most cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted? I can't split the list by whitespace, because single elements that contain whitespace will also be split.
On 3/18/26 21:50, Rich wrote:
Choosechee <choosechee@pm.me> wrote:
I am actually reading the list from a file. Is this a bad idea?
We can't read your mind. It is best to tell us these facts as part
of the initial posting.
Whether this is a bad idea depends upon whether you can depend upon
the string from the file being properly formatted as a list. In a
response to pmcc you said it was also being created by someone else,
which means if you use Tcl's default conversion, you are dependent
upon someone else always correctly formatting the list encoding. If
they get a single character wrong in just the right place, you'll
take an error in your code.
I.e.:
% set is_it_a_list {a "b c}
a "b c
% lindex $is_it_a_list 1
unmatched open quote in list
Should I just parse it from scratch?
If you can't be sure it is always properly encoded as a string that
Tcl can convert automatically, then yes, it is better to do the
parse yourself from scratch.
Sorry, I do need to learn to be more specific when asking questions
like this. Anyways, I'm okay with having a fail state, so couldn't I
just check with `string is list $myList` and throw my own error if it returns 0?
Are you sure?:
% set myList {"First\nElement" {Second Element}}
"First\nElement" {Second Element}
% llength $myList
2
% lindex $myList 0
First
Element
It looks like the list parser, when encountering a quoted string in the thing it is parsing to a list, has invoked rule 4 and performed
"backslash substitution" when creating list element zero.
Let's say I have defined a list as follows:
[...]
The elements have undergone substitution before being returned. In most cases, this is desirable, but in my case, I want them to be returned
exactly as written, like:
```
"First\nElement"
{Second Element}
```
How can I get an element of a list without it being substituted?
Choosechee <choosechee@pm.me> wrote:
Let's say I have defined a list as follows:
`set myList {"First\nElement" {Second Element}}`
You have not defined a list. You have defined a string. It is a
string that happens to be convertable to a list, but it is not a list. Curly braces {} do not mean "a list" (although this is a common misconception).
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,105 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 492342:47:59 |
| Calls: | 14,158 |
| Calls today: | 2 |
| Files: | 186,284 |
| D/L today: |
1,625 files (612M bytes) |
| Messages: | 2,502,643 |