set thefile "bla.txt"
bind dcc - count dcc:count
proc dcc:count {hand idx arg} {
set b1 "\{"
set b2 "\}"
set file [open "$::thefile" r]
while {![eof $file]} {
set lines [split [read $file] "\n"]
set t1 0
set t2 0
foreach line $lines {
if {[string match "*$b1*" $line]} {
incr t1
}
if {[string match "*$b2*" $line]} {
incr t2
}
}
}
catch { close $file }
putdcc $idx "$t1 of $b1 and $t2 of $b2"
}
bla.txt file:
if { bla } { moo }
Duno for shure what is wrong there and especialy why I'm always getting as results 2 { and 1 } when should be 2 { and 2 }.
Once the game is over, the king and the pawn go back in the same box.
First of all..the way you're "looping" through the file makes no sense... 'read' without a second parameter makes tcl read the entire file all at once.
Second.. you only check for one { and } in each line, so the code you quoted should result in 1 { and 1 }.
About what "looping" are you talking about? As far as I can see I'm reading all the lines of the file the split them in to pices with the foreach thing. At least this should happen. I have also tryed to split again the result to make the match more eficient but screwed it somehwere.
Once the game is over, the king and the pawn go back in the same box.
I'm talking about the while loop...it'll only be executed once, so you might aswell do if {![eof]} {..}
In your foreach you loop through the list of lines created with [split [read $file] "\n"], so the ONE line of code you used for testing should still result in 1 and 1.
Ahh.. duno why sometimes I choose the hard way on doing things. Silly me forgot about the 'regexp'.. pff. Thank you.
PS: As far as I remember don't think there is a need of two \ in front of the { and no need (humm..) of then in front of the }. Not shure 100% about this anyway. I chould be wrong or something.
Once the game is over, the king and the pawn go back in the same box.
There is some magic here, that you should understand.
In Tcl, you may need to escape certain characters. EG, you can use \003, \{, \[
However, what if you want to place the backslash (\) into your text, and not have it interpret it as escaping the next character. You use \\.
Tcl isn't the only language that uses escape sequences. Infact, Regular Expressions are a language in themselves, and they also have escape sequences.
So if you need to send a escape { to regexp, you can't send \{, as Tcl will capture it, and sort the \ before it makes it to regexp. Thus, using \\{ in Tcl, sends \{ to the regexp.
{ when followed by a character other than a digit, matches the left-brace character `{'; when followed by a digit, it is the beginning of a bound.
So double escaping it is only needed when you're looking for a { followed by a number.
For my personal knowlege. I want to know if I've missed one or something like this. Dosen't matter for what I've use it, stdragon gave me the best solution and all I would like to know now it's the thing I've asked a few moments ago.
Once the game is over, the king and the pawn go back in the same box.
I asked what you're using it for since you might have an interest in NOT matching escaped braces in your file.
As for your question...what do you mean?
I'll answer what i think your question was about...if you wanted to match "{0" the { would have to be double escaped.