PDA

View Full Version : I need a hand with some TCL proc...


djdeluxe76
05-24-2005, 08:31 AM
Hello and thanks for coming here...

First off, this is my first attempt at TCL scripting. I have been reading
thru the ActiveTCL manuals alot lately. Now I am trying to build a proc
that will do the following:

open a channel "in"
read in some logfiles in a dir "logdir"
close channel "in"
foreach of these files read them and lappend to a variable "log_data"
open a new channel "out" and puts the collected (read) $log_data
then close channel "out"
return with the logfile argument of this proc that was used as file for the open "out"]

So in the end I have a file "logfile" created by "out" that contains all
the data read by "in". And I have the ability to store the return code by
the proc in a new variable so I can work with the location of "logfile".

Here's what I have so far:

proc savePrelog {dir logfile} {
set prelog_data {}
foreach file [glob -nocomplain -directory $dir *{underdogs}*.log] {

set line [gets $in]
set dataIN [read $in]

close $in

lappend prelog_data $dataIN

set out [open $logfile a]
set dataOUT $prelog_data

puts $out $dataOUT

close $out

}
return $logfile
}

but this way I have the problem that it does not stop once it has
read all logfiles. The saved file just grows and grows...
There must be a way to do this properly.

Thanks for any replies :)

Cheers
DJ

Harm
05-24-2005, 01:04 PM
You're right for the second file, you need to open the data stream / file before reading anything from it. Also "gets" will only read one line from the file (starting where the current file pointer is). You can use "read" instead ; it'll read the whole file. I have also added a "-types f" switch to the "glob" command to make sure all files are files and not directories (or anything else).

I guess "underdog" appears in all the names of the logfiles you want to merge.

proc savePrelog {dir logfile} {
set data ""
set logfiles [glob -nocomplain -directory $dir -types f -- "*underdogs*.log"]
foreach logfile $logfiles {
set in [open $logfile r]
set data [read $in]
close $in
set out [open $logfile a]
puts $out $data
close $out
}
return $logfile
}

I hope this helps.
Good luck :)

djdeluxe76
05-24-2005, 03:09 PM
Gee, thanks Harm, that actually helps a lot!
Working with it now... :)

Yea, you assume correctly, "underdogs" appears in all file names.

I'm all excited to make it work, let's go back coding :D

Cheers
DJ