Jump to content

Execute a Markov algorithm: Difference between revisions

→‎{{header|Tcl}}: Make the code more compact
(→‎{{header|J}}: minor imrovement (due to nsg & vc))
(→‎{{header|Tcl}}: Make the code more compact)
Line 427:
if {[string match "#*" $line] || $line eq ""} continue
if {[regexp {^(.+)\s+->\s+(\.?)(.*)$} $line -> from final to]} {
lappend rules $from $to [string equalcompare "." $tofinal] [string length $from]
} else {
error "Syntax error: \"$line\""
Line 440:
while {$any} {
set any 0
foreach {from to stopmore fl} $rules {
# If we match the 'from' pattern...
if {[set idx [string first $from $line]] <>= 0} {
# Change for the 'to' replacement
continue
set line [string replace $line $idx [expr {$idx+$fl-1}] $to]
}
 
# Change for the 'to' replacement
set line [string replace $line $idx [expr {$idx+$fl-1}] $to]
 
# Stop if we terminate, otherwise note that we've more work to do
set any 0$more
if {$stop} {
break; # Restart search for rules to apply
set any 0
} else {
set any 1
}
break
}
#DEBUG# puts $line
}
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.