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
basic regexp problem

Posted by maxell (maxell), 15 August 2005
Code:
set name "Noname"
set file [open list.txt a]
           set result [regexp -all $name $file ]
           puts "$result"
           if {$result > "0"} {
           puts "test" }

it seem the regexp didn't open the file? the result always 0 , have read the manual but still confused
the function of the just simple 1. read from string and compare with var $name, and if the define $name found on file it will return with word "test"
sample list.txt
Code:
[16.08.05/01:24] - Name: Noname id: 0827EC5 Level: 1
[16.08.05/01:24] - Name: Noname id: 0827EC5 Level: 1
[16.08.05/01:24] - Name: Noname id: 0827EC5 Level: 1
[16.08.05/01:24] - Name: Noname id: 0827EC5 Level: 1
[16.08.05/01:24] - Name: Noname id: 0827EC5 Level: 1

as u can see there is 5 dupe, what i want is the script count the dupe(and make the announce puts "no of dupe : $dupe", and if possible delete the 4 field and leave 1 alone, pls help, i have try to search this at google,manual still confuse,


Posted by maxell (maxell), 16 August 2005
play a bit at some sample script, and get this :
Code:
set name "Noname"
set file [open list.txt r]
set get2 [gets $file]
set result [regexp -all $name $get2 ]
           puts "$result"
           if {$result > "0"} {
           puts "test" }

what i got, yes it read but only first line of file , it didn't read until eof of file, and i have read eof at tcl man but still didn't know how to implement that so make the script read until eof, also pls help me how to make the script also read the dupe text found (perline) -- like "puts
"found number of name dupe : $"




Posted by admin (Graham Ellis), 16 August 2005
The open command only opens the file ready for reading ... it doesn't actually read anything at all.

fgets reads a single line of data.  That's why you're only getting a report about the first line.   You need to run your fgets in a loop that keeps going while there's still data available if you want to process every line of the file.

See examples in the Tcl Input/Output module listed under [url=http://www.wellho.net/resources/modules.html]

Posted by maxell (maxell), 16 August 2005
hmm cann't found tcl/input output
Code:
Modules on Tcl/Tk

T201 What is Tcl?
T202 Tcl Fundamentals
T203 Conditionals and Loops
T205 String Handling in Tcl
T206 Lists
T207 Procedures and Variable Scope
T208 Arrays
T209 File and Directory Handling
T211 What is Expect? Why use it?
T212 Expect Processes
T213 Libraries, Packages and Namespaces
T214 Other Facilities in Tcl
T216 Introduction to Tk
T217 First Widgets, Geometry and Events
T218 Geometry Managers
T219 Drawing on Canvases
T220 Frames and Other Topics
T221 Menus
T222 Resources
T223 Textish Widgets
T224 Events in Tk
T241 Tcl on the Web
T242 More on Expect
T243 Using Tcl from C
T244 Socket Programming
T245 [incr-Tcl]
T246 Tix Megawidgets
T247 Advanced Regular Expressions
T248 A Review of Tcl and Tk Basics
T249 The tcltest Package

btw how to count the found result and make it as var? and if possible delete the dupe and just leave 1, pls help, show the code

Posted by admin (Graham Ellis), 16 August 2005
Sorry - try "file and directory handling" ... I was posting through my mobile phone and couldn't check the exact wording!

Posted by maxell (maxell), 17 August 2005
how to regex this data ?
[space]name[space] ?
have try with /s[name]/s but didn't work

Posted by admin (Graham Ellis), 17 August 2005
Try

{\sname\s}

Posted by maxell (maxell), 18 August 2005
{\sname\s}
ok work,but why didn't work {\s$var\s} ? how to regex a [space]var[space] , sorry if i asked twice,i thought before that was same ,but it's diff,pls help

Posted by admin (Graham Ellis), 18 August 2005
It didn't work with the variable name because the curly braces defer the interprettaion on the variable that starts with the $ character.

You'll need something like

"\\s$name\\s"

Where the $ variable will be interpretted and the \ is protected by a second \ from the Tcl shell ...

Posted by maxell (maxell), 19 August 2005
thanks for that, works good. 1 last quest, how to define the match result? sample:
Code:
set name "Noname"
set f [open test.log r]
#file process 1
while { [eof $f] != 1 } {
           gets $f line
           if [regexp "\\s$name\\s" $line] {
            incr result
           }
         }
           close $f
           if {$result == 1 } {
         puts "Found on file 1 : $result"
         } else {
         set f2 [open test2.log r]
           #how to define the var $result to other name from this below process ?
           #file process 2
         while { [eof $f2] != 1 } {
           gets $f2 line
           if [regexp "\\s$name\\s" $line] {
               incr result
               }
         }
         set result2 "$result"
       
         if {$result2 >= 1 } { #this should be read from var $result on file process 2? but it seem it always read from file process 1
         puts "Found on FIle 2 : $result2"
         } elseif { $result == 0 } { #i want this want read the var from regex result var on file process 1
         puts "Thanks for join"
     
        }
     }
     
       }    


[regexp "\\s$name\\s" $line] --> the result should return as $result right? how to define custom var name ?

Posted by admin (Graham Ellis), 19 August 2005
To capture the whole match string, put an extra variable name (without the $ since you're setting in) on the end of the regex command.

To capture just an an interesting bit of the match string, put round brackets around the part(s) of the regular expression to which you want to capture the matches, and add extra variable names (in addition to the one that catches the whole match) on the end of the command.

To capture / check whether there was a match at all, check the return value from regex - it will be true if there was a match and false if there wasn't.

Example

set gotit [regexp {([[:graph:]]+)@([[:graph:]])}  $in x y z

will match an email address (nonspaces@nonspaces) and capture:
gotit as a flag as to whether or not the match worked. if it did:
x is the email address as a whole
y is the user name
z is the domain name

Posted by maxell (maxell), 19 August 2005
hmm, u seem missunderstood my question .
pls reread again my code

Posted by admin (Graham Ellis), 19 August 2005
I have read your code and think I still don't understand your question.

Posted by Dimon (Dimon), 7 December 2005
Hi Maxell!
If I understood your question correctly, you can construct your command line using $var in 'regexp' command like a string and run it later with 'eval' command:

Code:
set name "Noname"
set f [open test.log r]
#file process 1
while { [eof $f] != 1 } {
           gets $f line
           set cmdline "regexp \{$name} \$line res"
           if {[eval $cmdline]>0} {
               incr result
               }
      }
...........


It must works.



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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho