Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read from a file in TCK

30 views
Skip to first unread message

krishna

unread,
Sep 20, 2006, 9:13:26 AM9/20/06
to
Can Anyone please help me.

I want to read the contents of a file. I m a begineer in TCK and
couldnt gather much information about file handling. If anyone of you
know please send me a sample script showing how to read from a file and
also please send me some documentation if its available with you

Thanks
Krishna

suchenwi

unread,
Sep 20, 2006, 9:38:04 AM9/20/06
to

krishna schrieb:

> I want to read the contents of a file. I m a begineer in TCK and
> couldnt gather much information about file handling. If anyone of you
> know please send me a sample script showing how to read from a file and
> also please send me some documentation if its available with you

I expect you mean Tcl (Tool Command Language)? That's what this group
is for- To read a file in one go is simple:
set fp [open $filename]
set data [read $fp]
close $fp]

To read it line by line (if it's gigabytes...)
set fp [open $filename]
while {[gets $fp line] >= 0} {
#do something with $line
}
close $fp

Helmut Giese

unread,
Sep 20, 2006, 9:55:54 AM9/20/06
to
On 20 Sep 2006 06:38:04 -0700, "suchenwi"
<richard.suchenw...@siemens.com> wrote:

>
>krishna schrieb:
>
>> I want to read the contents of a file. I m a begineer in TCK and
>> couldnt gather much information about file handling. If anyone of you
>> know please send me a sample script showing how to read from a file and
>> also please send me some documentation if its available with you
>
>I expect you mean Tcl (Tool Command Language)? That's what this group
>is for- To read a file in one go is simple:
> set fp [open $filename]
> set data [read $fp]
> close $fp]

For the 'beginner in TCK': This line has a small typo and should read
close $fp
Welcome in Tcl land
Helmut Giese

Michael Schlenker

unread,
Sep 20, 2006, 9:48:34 AM9/20/06
to
Take a look at the tutorial:
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html

Michael

krishna

unread,
Sep 21, 2006, 6:03:50 AM9/21/06
to
Thanks for your help. I have another doubt as how to perform this
operation.

I have a file with contents
------------------------------------

-file SYS -parameter NO 1 -parameter YO 2 -parameter CO 3
/netlist/name

i want to read the integers and also assign a variable

ie set a variable to /netlist/name


can someone give me how to handle these ?

Thanks,
krishna

Michael Schlenker

unread,
Sep 21, 2006, 6:16:00 AM9/21/06
to

Read the tutorial, either the section about regexp.
http://www.tcl.tk/man/tcl8.5/tutorial/Tcl20.html

or use scan:
http://wiki.tcl.tk/scan

or use string functions to get at the parts the are interesting to you.

Michael


Ralf Fassel

unread,
Sep 21, 2006, 6:17:47 AM9/21/06
to
* "krishna" <kitc...@gmail.com>

| -file SYS -parameter NO 1 -parameter YO 2 -parameter CO 3
| /netlist/name
|
| i want to read the integers and also assign a variable
|
| ie set a variable to /netlist/name
|
| can someone give me how to handle these ?

- you open the file with the 'open' command
- you read the file line by line with the 'gets' command
- you split the line into single words either with 'split' (may yield
empty fields if line contains consecutive spaces) or with 'regexp'
(check wiki and docs for syntax)
- you assign variables with the 'set' command

For all of those commands, the TCL pages
http://www.tcl.tk/man/tcl8.4/TclCmd/contents.htm
have the documentation online,
http://www.tcl.tk/man/tcl8.5/tutorial/Tcl24.html
describes the commands useful for reading files and the wiki
http://wiki.tcl.tk
may contain further info.

If you need help with specific code, please post that code.

HTH
R'

Cameron Laird

unread,
Sep 21, 2006, 7:01:09 AM9/21/06
to
In article <1158833030....@d34g2000cwd.googlegroups.com>,
.
.
.
set fp [open $file_with_contents]
set contents [read $fp]
close $fp
for {first_variable second_variable} [split $contents \n] {}

krishna

unread,
Sep 22, 2006, 1:00:48 AM9/22/06
to

Can I split with respect to special Characters ";"

The contents of the file is

a ; b
c ; d


i Want to extract it as

a
c
in a variable

and
b
d
in another variable .

Is it possible?

krishna

unread,
Sep 22, 2006, 2:35:34 AM9/22/06
to
I was able to perform the previous query I posted.

I have another doubt if this is possible in TCL.
i have a file with data
-root RTI -parameter NUM 1 -parameter NO 3 -parameter TB 0

now i need to get the output as

RTI_NUM1_NO3_TB0

i would like to know how to set my search.

Thanks,
Krishna

Uwe Klein

unread,
Sep 22, 2006, 3:35:50 AM9/22/06
to
set file [ open ...
set data [ read $file ]
set lines [ split $data \n ]

set res ""
set spacer ""

foreach line $lines {
foreach tok $line {
switch -glob -- $tok \
-root {
puts $res
set res ""
# nothing
} -* {
set spacer _
} default {
append res $spacer$tok
set spacer ""
}
}
}

This gives one empty line at the top.

uwe

Donal K. Fellows

unread,
Sep 22, 2006, 4:35:21 AM9/22/06
to
krishna wrote:
> i have a file with data
> -root RTI -parameter NUM 1 -parameter NO 3 -parameter TB 0
> now i need to get the output as
> RTI_NUM1_NO3_TB0

An interesting little problem. Try this:

set f [open ...]
# Extract the words from the file's contents
set data [regexp -all -inline {\S+} [read $f]]
close $f

# out is an accumulator variable
set out {}

# Iterate over the words; note that we do not use [foreach] here
# because we want to skip several words at a time, and the number of
# words to skip is not constant.
for {set i 0} {$i<[llength $data]} {incr i} {
switch -- [lindex $data $i] {
-root {
# Start of an item to print (?) so print out anything old
# that's accumulated first
if {[string length $out]} {puts $out}

# Now start accumulating the next thing to print
set out [lindex $data [incr i]]
}

-parameter {
# Add the data extracted from the -parameter options. Note
# that Tcl, unlike C/C++, has a well-defined substitution
# order so this code is correct.
append out _[lindex $data [incr i]][lindex $data [incr i]]
}

default {
puts "# WARNING: extra word [lindex $data $i] found!"
}
}
}

# Everything's processed, but we probably have something in the
# accumulator so print it out now so we don't lose the last line.
if {[string length $out]} {puts $out}

As far as I can see from testing with your example, that does the right
thing, but I hope that -root always takes one value and -parameter
always takes two; if not, you're going to have to take that code and
extend it further.

Donal.

0 new messages