Execute a Markov algorithm: Difference between revisions

Content added Content deleted
m (tidy up description of task a little)
(→‎Tcl: Added implementation)
Line 29: Line 29:
})
})
end</lang>
end</lang>

=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<lang tcl>package require Tcl 8.5
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"}
lassign $argv ruleFile inputFile outputFile

# Read the file of rules
set rules {}
set f [open $ruleFile]
foreach line [split [read $f] \n[close $f]] {
if {[string match "#*" $line] || $line eq ""} continue
if {[regexp {^(.+)\s+->\s+(\.?)(.*)$} $line -> from final to]} {
lappend rules $from $to [string equal "." $to] [string length $from]
} else {
error "Syntax error: \"$line\""
}
}

# Apply the rules
set f [open $inputFile]
set out [open $outputFile w]
foreach line [split [read $f] \n[close $f]] {
set any 1
while {$any} {
set any 0
foreach {from to stop fl} $rules {
# If we match the 'from' pattern...
if {[set idx [string first $from $line]] < 0} {
continue
}

# 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
if {$stop} {
set any 0
break
} else {
set any 1
}
}
}

# Output the processed line
puts $out $line
}
close $out</lang>