finding balanced occurences of two words... help needed
[1/2] from: princepawn::mailandnews::com at: 15-Sep-2000 4:53
REBOL [
Title: {Unbalanced set and reset commands alert function.
Prints a warning when a file contains an unbalanced number of
setsize and resetsize commands.
Right now, for some reason, it only seems to count the resetsize text
and not increment set-cmds...
}]
study-file: func [file [file!] /local set-cmds reset-cmds] [
set-cmds: 0
reset-cmds: 0
s-size: [thru "setsize" (set-cmds: set-cmds + 1)]
rs-size: [thru "resetsize" (reset-cmds: reset-cmds + 1)]
rules: [any [rs-size | s-size] ]
parse read file rules
if (set-cmds <> reset-cmds) [
print reform [ "file" file "had unbalanced set and reset cmds"
set-cmds "set-cmds" reset-cmds "reset-cmds"
]
]
]
write %dummyfile1 "setsize"
write %dummyfile2 "setsize setsize resetsize"
foreach file [ %dummyfile1 %dummyfile2 ] [ study-file file ]
terrence-brannon: [[princepawn--yahoo--com] perl-refugee myth-gamer]
free-email: http://www.MailAndNews.com
free-usenet: http://www.mailAndNews.com
; all the above is real REBOL code, believe it or not.
[2/2] from: rebol:techscribe at: 15-Sep-2000 2:18
Hi princepawn,
and not in bed yet ...
> s-size: [thru "setsize" (set-cmds: set-cmds + 1)]
> rs-size: [thru "resetsize" (reset-cmds: reset-cmds + 1)]
> rules: [any [rs-size | s-size] ]
>
> parse read file rules
your rules contains the first rs-size and then s-size.
The rs-size rule says [thru "resetsize"]. thru is a navigational command
that tells the parse to ignore everything up to "resetsize". Since this is
your first rule, parse will have skipped all occurrences of "setsize"
because it can satisfy this rule by navigating passed the "setsize" until
it arrives at "resetsize". Since it can fulfill the rs-size rule, and in
doing so has passed "thru" all occurrences of "setsize", the s-size rule is
never matched.
Instead of using navigational commands, try the following combination (or
something similar, I haven't tried it, because I really have to rest my
brain for a few hours):
s-size: ["setsize" (set-cmds: set-cmds + 1)]
rs-size: ["resetsize" (reset-cmds: reset-cmds + 1)]
rules [any [rs-size | s-size | skip] ]
What happens now is that parse tries to match rs-size on the first
character sequence it finds in file. If there is no match, it tries to
match s-size instead. If neither of them match, it'll perform skip, which
takes it to the next character and it will now try to match the string
resetsize
again against the charcter sequence beginning with the second
character in the file, and so on, until it has exhausted the file,
occassionally matching one or the other rule, or skipping a character, if
neither of the rules match the chacter sequence beginning at the current
character position.
Hope this (or something similar to this) helps,
;- Elan [ : - ) ]
author of REBOL: THE OFFICIAL GUIDE
REBOL Press: The Official Source for REBOL Books
http://www.REBOLpress.com
visit me at http://www.TechScribe.com