Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
how to avoid too much substitution

Posted by sirius_black (sirius_black), 5 December 2003
Hi !!
I'm made a Tcl script that read another Tcl script and wrote the results in a third Tcl script (a copy).

My problem is :
The 'reader' script gets a line like 'puts $x' from the file A
And when it wrote it in the other file (B) I find 'puts "" '
and no 'puts $x'
The script changed '$x' by his value ($x is not defined in the 'reader')
I don't want the script to do a substitution.  
What can I do ?

I have changed the subject on this post to describe the question being asked rather than to reflect his frustration with Tcl at the time the post was made - Graham

Posted by admin (Graham Ellis), 5 December 2003
Here's a program that copies a file called "tclcopy" to STDOUT:

Code:
set fh [open tclcopy]
while {[gets $fh inline] > 0} {
       puts $inline
       }


It works fine and doesn't alter the text being copied.

Please modify my program to show the problem that you're having ... I suspect the the very act of doing so will lead you to a solution, but if not post up the actual code and I'll have a further look at it

Posted by sirius_black (sirius_black), 11 December 2003
I realised the cause of my problem wasn't Tcl but the cat unix command.
Here is the content of the file I try to read (the content is not very important, the only problem is the presence of the '$' character.

--file.tcl--
#!/usr/bin/expect -f
set x 3
puts $x
set a "Hello\nWorld"
puts $a
-----------

And the main script :
--read.tcl--
#!/usr/bin/expect -f
set timeout 1
set f [open "file.tcl" "r"]
set texte ""

while { ![eof $f] } {
     set ligne [gets $f]
     append texte "$ligne\n"
}
puts $texte ;# Only to be sure it works
close $f

spawn sh
send "cat > truc.txt << EOF\n"
expect ">"
send -- $texte
expect ">"
send "EOF\n"
expect
send "exit\n"
-----------

The problem was in the cat command. If you type
[sirius]$ cat > truc.txt << EOF
> $x
> second line
> EOF
[sirius]$ cat truc.txt

second line
[sirius]$

The cat command don't like the '$' (it don't want to read it). If we do :
[sirius]$ cat > truc.txt << EOF
> \$x
> second line
> EOF
[sirius]$ cat truc.txt
$x
second line
[sirius]$

It works !! So I think I must replace the '$' by '\$'.
After a long time (grr) I find a solution.

--read.tcl--
#!/usr/bin/expect -f
set timeout 1

set f [open "file.tcl" "r"]
set texte ""

while { ![eof $f] } {
   set ligne [gets $f]
   set ligne [string map {\\ \\\\ \$ \\\$ \` \\\`} $ligne]
   append texte "$ligne\n"
}
puts $texte
close $f
spawn sh
send "cat > truc.txt << EOF\n"
expect ">"
send -- $texte
expect ">"
send "EOF\n"
expect
send "exit\n"
------------

It seems to work but if you have a better idea... (for exemple tabulations are interpreted by the shell).
I don't want to see the operation (cat) on the screen because there is a lot of null bytes. What can I do ?



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho