Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two Different List Output

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I have ...

set line [read $fid]

puts $line\n
set line [lreplace $line 0 0] ;# remove the first element
puts $line\n


The result of the 1st "puts", it displays the data correctly with the original new line returns (i.e. \n).

However, the result of the 2nd "puts" (after the lreplace cmd), it displays the data in one continuous stream without interpreting the new line returns.

What happened to the orignal line returns ???? (Note: it has nothing to do with the lreplace cmd - I could have replace any index of the original list)
 
Just for jollies, try replacing

set line [lreplace $line 0 0]

with

set line [lrange $line 1 end]

and see if that changes the behavior.

Bob Rashkin
rrashkin@csc.com
 
Replacing set line [lreplace $line 0 0]
with set line [lrange $line 1 end] has the same effect as I originally stated - that is the output is now in one continous stream.

So the question still remains:
Why does the initial output of a list created from the cmd set line [read $fid] preserves the trailing end-of-line values, but if you "play" with the list using one of the list functions, the trailing end-of-line values are no longer preserved?
 
The best I can account for it is that you're really taking a string with embedded spaces (and line feeds) and treating it as if it were a list. After a list operation (like lreplace or lrange), it really is a list. Absent any more explicit instructions for a split delimiter, Tcl uses the \n as well as the " ".

...or something like that.

Bob Rashkin
rrashkin@csc.com
 
Bong is right: when Tcl enconters a string where a list is expected, silently transforms the string to a list using a
Code:
split
command with the standard white-spaces delimiters (space, tab, nl). And when Tcl enconters a list where a string is expected, silently transforms the list to a string using a
Code:
join
command with the space as separator.

So when you read a file you obtain a string, then you use it as a list, splitting it at all nl, then when you write the list to the console, displaying the lines spaced with a space char.

Some hints:
If you expect lines from your file, use
Code:
gets
that return you one line at a time.
To write your lines to the console, use
Code:
join
with a \n char as separator.

More info at:

This transform from string to list (and from list to string) is one of the more used implicit mechanism and, misunderstood, can be an annoyance.

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top