Re: Weird Error Msg



At 6 Feb 2007 05:37:55 -0800 "SuNnY" <esunny@xxxxxxxxx> wrote:


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.

Nope. It is just a line of text. It is NOT a list. It *might* look
something like a Tcl list ('words' with spaces between them). There is
no certainty that it is a properly formed Tcl list (in terms of
balanced braces, brackets, and quote marks) and you should never expect
it to be (unless the file in question was written by a Tcl program that
wrote proper Tcl lists, one per line). You can make it into a list
using, for example, split:

set TestStr [split [gets $Rdfile]]

But unless you have some reason to do that, there is not really any
point in doing it. If you just want to do string matching against lines
of text, some variation of regexp, string match, or string first will do
what you want to do.

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



--
Robert Heller -- 978-544-6933
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
heller@xxxxxxxxxxxx -- Contract Programming: C/C++, Tcl/Tk

.



Relevant Pages

  • Re: Weird Error Msg
    ... foreach file $txtfile { ... set TestStr ... want to match a glob-style pattern, a regular expression, or a fixed string. ... lists, and the foreach expects it's second argument to be a list. ...
    (comp.lang.tcl)
  • Re: Weird Error Msg
    ... set TestStr ... The problem is the foreach: you simply cannot expect the above code to ... lists, and the foreach expects it's second argument to be a list. ... some other string. ...
    (comp.lang.tcl)
  • Re: Weird Error Msg
    ... foreach file $txtfile { ... set TestStr ... want to match a glob-style pattern, a regular expression, or a fixed string. ... Your files do not contain lists, and the foreach expects it's second argument to be a list. ...
    (comp.lang.tcl)
  • Re: list or not a list?
    ... sets the tcl variable to a string. ... Tcl list commands, given a plain string, attempt to do their best to ... The set of strings which are truly Tcl lists is a subset of the set of ...
    (comp.lang.tcl)
  • Re: TCL lists
    ... Don't construct Tcl lists by ... You don't start with a Tcl list, you start with a string. ... Parsing functions that are documented to create lists will take ... will also create empty list elements for those. ...
    (comp.lang.tcl)