Re: Weird Error Msg
- From: "SuNnY" <esunny@xxxxxxxxx>
- Date: 6 Feb 2007 05:37:55 -0800
On Feb 6, 12:51 pm, Bryan Oakley <oak...@xxxxxxxxxxxxxxxxxxxx> wrote:
SuNnY wrote:
On Feb 6, 1:57 am, Bryan Oakley <oak...@xxxxxxxxxxxxxxxxxxxx> wrote:
SuNnY wrote:
here is what i have done so far, it is not complete, not at all but i
am working my way out.
cd "C:\\"
set RootDir [pwd]
puts "Root: [pwd]"
set txtfile [glob -nocomplain *.tcl]
set DirList [glob -directory $RootDir *]
puts "Dir List : $DirList"
foreach Dir $DirList {
...
set txtfile [glob -nocomplain *.tcl]
foreach file $txtfile {
puts "Searching file $file Textdata ....."
set Rdfile [open $file r]
while { [eof $Rdfile]!=1 } {
set TestStr [gets $Rdfile]
foreach Str $TestStr {
The above statement is your problem. You are reading lines of data from
a text file and assuming each line is a valid tcl list. It almost
certainly is not.
If you are trying to parse a tcl file and print out commands or
variables or something, that is not something you can do with a simple
loop. You'll need to mimic the behavior of the tcl interpreter with
respect to quotes, curly braces, backslashes and newlines.
If all you're doing is looking for a string within a line in the file,
there is no need to do the "foreach Str $TestStr", you can just use
something like "string match $pattern $TestStr" or "regexp $pattern
$TestStr" or "string first $pattern $TestStr", depending on whether you
want to match a glob-style pattern, a regular expression, or a fixed string.
I am trying to read a particular string from a particular type of
file.
there are many files of this type.
and i am looking for this string in these particular files only.
So i need to scan complete directory tree to search for this file,
then to check if this particular string is present or not. if yes then
to display string with count (count=number of times this string
appears in all the files)
That is why i need to read the contents of the directory and the
files.
Now i am trying it using glob -type.
Scanning the directories and looking for files of a particular type is
not your problem. I'll repeat: the problem is that you are reading each
line of a file and trying to treat that line like a Tcl list, which it
is not.
The problem is last of the following four lines:
set Rdfile [open $file r]
while { [eof $Rdfile]!=1 } {
set TestStr [gets $Rdfile]
foreach Str $TestStr {
The problem is the foreach: you simply cannot expect the above code to
work in any sort of general purpose way. Your files do not contain
lists, and the foreach expects it's second argument to be a list.
Hi!
Hmmmm, what does "TestStr" in "set TestStr [gets $Rdfile]" contain.
Isnt it a list of strings of that file.
I have captured each string of TestStr in Str and compared it with
some other string.
for eg: This works perfect:
set TestData [open "C:/Tcl/bin/TestDataTaskW3.txt" r] ;# Opening
file containing test data ( a .txt file)
set contents [read $TestData] ;# Reading contents of the file
puts "Test Data:- \n$contents" ;# Printing test data on the
screen
set IpStr [lindex $argv 0] ;# Storing User Input (string1) in a
variable
#puts "\nString to be Matched: $IpStr\n" ;# Printing User Input
seek $TestData 0 start ;# Seeking file pointer to the
start(begining of the file)
if { [regexp {^A.*B$} $IpStr] } { ;# A regexp to check if input
string matches the required condition
while { [eof $TestData] !=1} { ;# A while loop to check the end
of file
set TestStr [gets $TestData] ;# Reads test data line by
line and stores in a variable
foreach StrChk $TestStr { ;# picking up each group of
letters (string2) from the file and storing them to another variable
if {[string match $StrChk $IpStr]} { ;# matching Input
string to any string from file
puts "From file $StrChk : MATCH: $IpStr " ;# Output-
Matched string from the file is printed on the screen with a message
} else {
puts "From file $StrChk : No Match : $IpStr"
}
} ;# end of foreach
} ;# end of while
} else {
puts "\nWrong Input \($IpStr\): No Match"
}
close $TestData ;# Closing .txt file
set EndTime [clock clicks -milliseconds ] ;# Registering time hen
computation ends.
puts "\nTime taken [expr {$EndTime} - {$StrTime}] milliseconds" ;#
displaying time taken in milliseconds for complete process
It is a code where user inputs a string, which is checked for validity
(valid string ^A.*B$, strat A and end B with anything inbetween).
i use
set TestStr [gets $TestData] to read line by line from TestData
(which is a file i want to read)
foreach StrChk $TestStr { to capture each string of the line
in a variable StrChk
if {[string match $StrChk $IpStr]} { Finally do the matching.
this is working fine for me, i mean i got my desired result.
thnx
.
- Follow-Ups:
- Re: Weird Error Msg
- From: slebetman@xxxxxxxxx
- Re: Weird Error Msg
- From: Robert Heller
- Re: Weird Error Msg
- From: Bryan Oakley
- Re: Weird Error Msg
- References:
- Weird Error Msg
- From: SuNnY
- Re: Weird Error Msg
- From: Bryan Oakley
- Re: Weird Error Msg
- From: SuNnY
- Re: Weird Error Msg
- From: Bryan Oakley
- Re: Weird Error Msg
- From: SuNnY
- Re: Weird Error Msg
- From: Bryan Oakley
- Re: Weird Error Msg
- From: SuNnY
- Re: Weird Error Msg
- From: Bryan Oakley
- Weird Error Msg
- Prev by Date: NameSpaces - general question.
- Next by Date: Daylight savings change
- Previous by thread: Re: Weird Error Msg
- Next by thread: Re: Weird Error Msg
- Index(es):
Relevant Pages
|