Searching for a non-constant regular expression - tcl

I want to find a pattern in a file, but the pattern can have several forms.
Here is the code :
while {[gets $thefile line] >= 0} {
for {set nb_table 1} {$nb_table<$count_table} {incr nb_table} {
if { [regexp {pattern_$nb_table} $line] } {
puts "I found one !"
}
}
}
the var $count_table is known, catched before on a on a other procedure.
If i do a puts of pattern_$nb_table in the for loop i got the name of all tables and that's good, but I never have I found one! printed out (sure i want to be another process but it is not the subject). Why I never go in the if? My file contains the pattern : pattern_1 pattern_2 .....

The problem is that the variable is not being substituted into the regular expression (the {…} disable all immediate substitutions). This is a situation where you'd use (putting the variable name in braces just for clarity, and putting the pattern in double quotes for highlighting only):
if {[regexp "pattern_${nb_table}" $line]} { ... }
Except that if I was looking for a string that simple, I'd try to use string first or string match:
if {[string first "pattern_${nb_table}" $line] >= 0} { ... }
if {[string match "*pattern_${nb_table}*" $line]} { ... }
Both of these are faster than regular expression matching, provided you're doing something simple. If the rest of the real pattern is a regular expression, only regexp will do. Of course.

Related

How to find multiple sub string patterns in a string in TCL

I am trying to find multiple string patterns in a string in TCL. I cannot get the correct and optimized way to do that.
I have tried some code and it is not working
I have to find -h ,-he,-hel ,-help in the string -help
set args "-help"
set res1 [string first "-h" $args]
set res2 [ string first -he $args]
set res3 [string first -hel $args]
set res4 [string first "-help" $args"]
if { $res1 == -1 || $res2 || $res3 || $res4 } {
puts "\n string not found"
} else {
puts "\n string found"
}
how to use regexp here I am not sure , so need some inputs.
The expected output is
This is a case where using regexp is easier. (Asking if a string is a prefix of -help is a separate problem.) The trick here is to use ? and (…) (or rather (?:…) which is the non-capturing version) in the RE and you must use the -- option because the RE begins with a -:
if {[regexp -- {-h(?:e(?:lp?)?)?} $string]} {
puts "Found the string"
} else {
puts "Did not find the string"
}
If you want to know what string you actually found, add in a variable to pick up the overall match:
if {[regexp -- {-h(?:e(?:lp?)?)?} $string matched]} {
puts "Found the string '$matched'"
} else {
puts "Did not find the string"
}
If you instead want the indices where it matched, you need an extra option:
if {[regexp -indices -- {-h(?:e(?:lp?)?)?} $string match]} {
puts "Found the string at $match"
} else {
puts "Did not find the string"
}
If you were instead interested in whether the string was a prefix of -help, you instead should do:
if {[string equal -length [string length $string] $string "-help"]} {
puts "Found the string"
} else {
puts "Did not find the string"
}
Many uses of this sort of thing are actually doing command line parsing. In that case, the tcl::prefix command is very useful. For example, tcl::prefix match finds the entry in a list of options that a string is a unique prefix of and generates an error message when things are ambiguous or simply don't match; the result can be switched on easily:
set MY_OPTIONS {
-help
-someOtherOpt
}
switch [tcl::prefix match $MY_OPTIONS $string] {
-help {
puts "I have -help"
}
-someOtherOpt {
puts "I have -someOtherOpt"
}
}

How to delete a part of the text file if a pattern is found matching using tcl?

How can I remove a part of the text file if the pattern I am searching is matched?
eg:
pg_pin (VSS) {
direction : inout;
pg_type : primary_ground;
related_bias_pin : "VBN";
voltage_name : "VSS";
}
leakage_power () {
value : 0;
when : "A1&A2&X";
**related_pg_pin** : VBN;
}
My pattern is related_pg_pin. If this pattern is found i want to remove that particular section(starting from leakage power () { till the closing bracket}).
proc getSection f {
set section ""
set inSection false
while {[gets $f line] >= 0} {
if {$inSection} {
append section $line\n
# find the end of the section (a single right brace, #x7d)
if {[string match \x7d [string trim $line]]} {
return $section
}
} else {
# find the beginning of the section, with a left brace (#x7b) at the end
if {[string match *\x7b [string trim $line]]} {
append section $line\n
set inSection true
}
}
}
return
}
set f [open data.txt]
set g [open output.txt w]
set section [getSection $f]
while {$section ne {}} {
if {![regexp related_pg_pin $section]} {
puts $g $section
}
set section [getSection $f]
}
close $f
close $g
Starting with the last paragraph of the code, we open a file for reading (through the channel $f) and then get a section. (The procedure to get a section is a little bit convoluted, so it goes into a command procedure to be out of the way.) As long as non-empty sections keep coming, we check if the pattern occurs: if not, we print the section to the output file through the channel $g. Then we get the next section and go to the next iteration.
To get a section, first assume we haven't yet seen any part of a section. Then we keep reading lines until the end of the file is found. If a line ending with a left brace is found, we add it to the section and take a note that we are now in a section. From then on, we add every line to the section. If a line consisting of a single right brace is found, we quit the procedure and deliver the section to the caller.
Documentation:
! (operator),
>= (operator),
append,
close,
gets,
if,
ne (operator),
open,
proc,
puts,
regexp,
return,
set,
string,
while,
Syntax of Tcl regular expressions
Syntax of Tcl string matching:
* matches a sequence of zero or more characters
? matches a single character
[chars] matches a single character in the set given by chars (^ does not negate; a range can be given as a-z)
\x matches the character x, even if that character is special (one of *?[]\)
Here's a "clever" way to do it:
proc unknown args {
set body [lindex $args end]
if {[string first "related_pg_pin" $body] == -1} {puts $args}
}
source file.txt
Your data file appears to be Tcl-syntax-compatible, so execute it like a Tcl file, and for unknown commands, check to see if the last argument of the "command" contains the string you want to avoid.
This is clearly insanely risky, but it's fun.

Tcl script to do Indentation

I want to write a tcl script to align my tcl script with proper indentation. For Example if i have a code like :
proc calc { } {
set a 5
set b 10
if {a < b} {
puts "b Greater"
}
}
I need to change like:
proc calc { } {
set a 5
set b 10
if {a < b} {
puts "b Greater"
}
}
Could u guys help on this.
Writing an indenter that handles your example is trivial. A full indenter that can handle most Tcl scripts is going to be very big and quite complicated. An indenter that can handle any Tcl script will have to incorporate a full Tcl interpreter.
This is because Tcl source code is very dynamic: for one thing you can't always just look at the code and know which parts are executing code and which parts are data. Another thing is user-defined control structures, which might change how the code is to be viewed. The example below works by counting braces, but it makes no attempt to distinguish between quoting braces that should increase indentation and quoted braces that should not.
This example is a very simple indenter. It is severely limited and should not be used for serious implementations.
proc indent code {
set res {}
set ind 0
foreach line [split [string trim $code] \n] {
set line [string trim $line]
# find out if the line starts with a closing brace
set clb [string match \}* $line]
# indent the line, with one less level if it starts with a closing brace
append res [string repeat { } [expr {$ind - $clb}]]$line\n
# look through the line to find out the indentation level of the next line
foreach c [split $line {}] {
if {$c eq "\{"} {incr ind}
if {$c eq "\}"} {incr ind -1}
}
}
return $res
}
This will convert your first code example to your second one. Add even a single brace as data somewhere in the code to be indented, though, and the indentation will be off.
Documentation: append, expr, foreach, if, incr, proc, return, set, split, string

How search for list's each element existence in file

How can I organize a cycle using TCL for searching list's each element existence in file or in another list, and if it doesn't exists there return unmatched element.
If the number of things that you are checking for is significantly smaller than the number of lines/tokens in the file, it is probably best to use the power of associative arrays to do the check as this can be done with linear scans (associative arrays are fast).
proc checkForAllPresent {tokens tokenList} {
foreach token $tokens {
set t($token) "dummy value"
}
foreach token $tokenList {
unset -nocomplain t($token)
}
# If the array is empty, all were found
return [expr {[array size t] == 0}]
}
Then, all we need to do is a little standard stanza to get the lines/tokens from the file and run them through the checker. Assuming we're dealing with lines:
proc getFileLines {filename} {
set f [open $filename]
set data [read $f]
close $f
return [split $data "\n"]
}
set shortList [getFileLines file1.txt]
set longList [getFileLines file2.txt]
if {[checkForAllPresent $shortList $longList]} {
puts "All were there"
} else {
puts "Some were absent"
}
It's probably better to return the list of absent lines (with return [array names t]) instead of whether everything is absent (with the general check of “is everything there” being done with llength) as that gives more useful information. (With more work, you can produce even more information about what is present, but that's a bit more code and makes things less clear.)
(When searching, be aware that leading and trailing whitespace on lines matters. This is all exact matching here. Or use string trim.)
Working with words instead of lines is really just as easy. You just end up with slightly different code to extract the tokens from the read-in contents of the files.
return [regexp -all -inline {\w+} $data]
Everything else is the same.

Is there a way to use wildcards in '==' test in tcl?

This might not be possible but is there a way to pass a regular expression in tcl.
I have this function that i cannot change which you pass in a string if finds something and compares them to see if they are equal.
proc check {a } {
// find b in the database
if {$a == $b} {
puts "equals"
} {
puts "Not equals"
}
}
The problem is that the function uses '=='. this only matches if they are exact, but i need to have wild cards in 'a' so that 'a' and 'b' are equal if 'a' and 'b' start with the same words.
I have this function that i cannot change
Why? In tcl, you could easily redefine it with
proc check {a } {
# find b in the database
if {[string match -nocase $a $b]} {
puts "equals"
} {
puts "Not equals"
}
}
Or you could redefine if, although not recommended.
You could even search and replace the if line at runtime with
proc check [info args check] [string map {
{if {$a == $b}} {if {[string match -nocase $a $b]}}
} [info body check]]
So: Why can't you change the function?
The behavior of the == operator is fixed; it always does an equality test. Indeed it does an equality test that prefers to do numeric equality and only falls back to string equality if it has no other way.
Therefore, to change the behavior of check you have to get really tricky.
I'd look at using execution traces to intercept something inside of check so that you can then put a read trace on the local a variable so that when you read it you get actually whether its value matches something according to complex rules. (b can probably just hold a 1 for boolean truth.) The code to do this is sufficiently mind-bendingly complex that I'd really try to avoid doing it!
Much easier, if you can, is to redefine proc so that you can put a prefix on the body of check so you can apply the trace there. Or even massage the test itself.
# Rename the command to something that nothing else knows about (tricky!)
rename proc my_real_proc
my_real_proc proc {name arguments body} {
# Replace a simple equality with a basic no-case pattern match
if {$name eq "check"} {
set body [string map {{$a == $b} {[string match -nocase $b $a]}} $body]
}
# Delegate to the original definition of [proc] for everything else
uplevel my_real_proc $name $arguments $body
}
So long as you run that code before the definition of check, you'll change the code (without “changing the code”) and get the sort of capabilities you want.