TCL special character \x1 - tcl

I have a problem with TCL string
set WORD 128
set CELL_NAME "MCELL_$WORD\x1"
# real: MCELL_128.. (.. is 2 special characters that I can't paste here)
# expected: "MCELL_128x1"
How can I format the string as expected?

set CELL_NAME "MCELL_${WORD}x1"
gives you the expected output.
Other possibilities:
set CELL_NAME "MCELL_[set WORD]x1"
set CELL_NAME [format "MCELL_%dx1" $WORD]
Documentation:
format,
set,
Summary of Tcl language syntax, particularily item [8].

Related

How to insert ASCII control characters into a TCL string?

I need to create a TCL script that contains ASCII control characters. This is the full list of these characters from the ASCII table but I am only interested in putting in the "start of text" value 2 and "end of text" value 3.
You can enter a hex code in a string by writing \xnn where nn is the code, e.g.
set start_of_text "\x02"
set end_of_text "\x03"
See the documentation at https://www.tcl-lang.org/man/tcl8.6/TclCmd/Tcl.htm#M27
You can also use format with the %c code (which might be more useful if you don't know the relevant number until run-time because it's in a variable or whatever):
set ascii(STX) [format %c 2]
set ascii(ETX) [format %c 3]
If I'm going to be wrapping text in a control sequence (often for things like applying a colouring) then I'll make a procedure to do the job:
proc wrapped {string} {
# These use Unicode escapes
return "\u0002$string\u0003"
}
puts [wrapped "this is some test text"]

Length in bytes of text with (CR) (LF)

I’ve got from sqlite3 value that could be written in hex like "0x0D 0x0A". Yes, it’s (CR) and (LF). I want to know a length of data i’ve got. But command "string length" returns 1, not 2. "string bytelength" returns 1 too. How can I get correct length of data in bytes?
It’s a simple example. In real program I’ve got different text data from sqlite with unknown encoding. All I need is to get length of data in bytes. But every (CR)(LF) in text are counting as 1 byte.
Examples of getting data from sqlite and file:
sqlite dbcmd messages.db
set t [dbcmd message from messages limit 1,1]
string length $t
set f [open test.txt r]
set t [read $f]
string length $t
(Windows 7, ActiveTcl 8.6.4, tclkit 8.6.6)
By default, Tcl transforms CR-LF sequences in files being read into simple LF characters. This is usually useful, as it simplifies ordinary text processing in scripts greatly. However, if want the exact values then you can use fconfigure to put the channel into an alternate processing mode. For example, changing the channel's -translation setting to lf (from auto) will make all carriage-returns be preserved (and line-feeds too).
set f [open test.txt r]
fconfigure $f -translation lf
set t [read $f]
string length $t
There are other settings that could — in general — affect what you get, particularly the -eofchar and -encoding options. The -eofchar is usually EOF (i.e., the character associated with Ctrl+Z) and the -encoding is a system-specific value that depends on things like what your platform is and what your locale is. If you want to really work with binary data, i.e., get just the bytes, you can set the -translation option to binary, which sets everything up right for handling binary data. There's a shorthand for that common option in the open command:
set f [open test.txt rb]; # ««« “b” flag in open mode
set t [read $f]
string length $t
If you do get the bytes and want to get characters from them at some point, the encoding convertfrom command is the tool you'll need. Remember, characters and bytes are not the same thing. That had to be given up in order to allow people to use more characters than there are values expressible in a byte.

How I add bracket in string variable in TCL file

I have written one TCL script but I have one problem when making a string variable as below:
set a 100
set b "this is variable[$a]"
I want b to be assign with b = "this is variable[100]" but I got the error:
invalid command name 100
Please help me to fix it :-(.
You just need to escape it:
set a 100
set b "this is variable\[$a\]"
Other possibilities (but escaping the brackets is better):
set b [format {this is variable[%d]} $a]
set b [subst -nocom -noback {this is variable[$a]}]
Documentation:
set,
format,
subst

Regarding expect_out(buffer) in Expect

Can anyone explain the difference between
expect_out(buffer)
expect_out(0,string)
Generally I prefer to use expect_out(buffer) .
What is the second one and when can we use that?
Can any one explain please?
You might want to take a look at the manpage:
I will quote the relevant parts:
Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable expect_out(buffer). Up to 9 regexp substring matches are saved in the variables expect_out(1,string) through expect_out(9,string). If the -indices flag is used before a pattern, the starting and ending indices (in a form suitable for lrange) of the 10 strings are stored in the variables expect_out(X,start) and expect_out(X,end) where X is a digit, corresponds to the substring position in the buffer. 0 refers to strings which matched the entire pattern and is generated for glob patterns as well as regexp patterns. For example, if a process has produced output of "abcdefgh\n", the result of:
expect "cd"
is as if the following statements had executed:
set expect_out(0,string) cd
set expect_out(buffer) abcd
and "efgh\n" is left in the output buffer. If a process produced the output "abbbcabkkkka\n", the result of:
expect -indices -re "b(b*).*(k+)"
is as if the following statements had executed:
set expect_out(0,start) 1
set expect_out(0,end) 10
set expect_out(0,string) bbbcabkkkk
set expect_out(1,start) 2
set expect_out(1,end) 3
set expect_out(1,string) bb
set expect_out(2,start) 10
set expect_out(2,end) 10
set expect_out(2,string) k
set expect_out(buffer) abbbcabkkkk
You can see how expect_out(0,string) and expect_out(buffer) contain different strings.

Use comment symbol in variable value TCL

I am looking to use the # symbol which is the symbol to indicate that everything following is going to be a comment in a variable value. So, I would like to write the following:
set Dev1_Number 1#
set Dev2_Number 2#
But the program only recognizes 1 and 2 as values that can be placed the memory location of the variable. Is there anyway to get around that?
Tcl comments only occur when the comment character is the first character of a command word
(http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm#M29). You'll see code with end-of-line comments preceded by a semicolon
set foo bar ;# this is a comment
set foo bar # this is an error!
That's not the case in your example. In your example, the hash is merely data.
Your comments indicate your editor is making an incorrect assumptions about Tcl syntax. What editor are you using?
If you are concerned, you can "force" the hash to be part of the value by using quotes
set Dev1_Number "1#"
set Dev1_Number {1#}
Use backslash character, It escapes the original meaning of that character.
set a 3\#
puts "a=$a"
output: a=3