This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

brackets

Old posts that have not been replied to for several years.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

brackets

Post by caesar »

Code: Select all

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.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

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 }.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

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.

What exactly are you trying to do?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I would like to read all the { and } from a file and tell the result.
Once the game is over, the king and the pawn go back in the same box.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Code: Select all

set fp [open sheep/baa.txt r]
set data [read $fp]
close $fp
set openbraces [regexp -all "\\{" $data]
set closebraces [regexp -all "\\}" $data]
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

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.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Yes, you are right, as usualy. Thank you for explaining this.
Once the game is over, the king and the pawn go back in the same box.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I don't want to bitching around, but I've test the thing you've said about the \'s and actualy the regexp worked with one \ like this: \{.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

{ 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.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Please give me an example of the matches you've mentioned earlier. I'm asking this to understand things better not to bitch around. Thank you.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Who? What?

btw: what is the purpose of counting the braces?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

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.
Tclsh test:
% regexp \\{0 \{0
1
% regexp \{0 \{0
couldn't compile regular expression pattern: quantifier operand invalid
Locked