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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: 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