Please note that, some lines have multiple "*", I'd want only the 1st word after "*" to be considered as "Answer" and be flipped to sit in the beginning of the line.
Briefly, I have a list of questions and answers that are written in the following format: Question*Answer and sometimes Question*Answer*Answer when a question requires more than an answer... that I need to change to this format: Answer|Question.
this script replies to the message command .formatfile from a user with the global userfile flag(s) m, n, and/or o
rename the original file to old.file
put that file in the eggdrop scripts/ dir
message the bot with .formatfile
the new file will be at scripts/new.file
I guess, we just need this script to be upgraded to add "?" to the end of each question line.
Note that, this tcl script doesn't work for "KAOS questions" type where the answer requires multiple words. It chose the first word after "*" as an answer which is not the case for KAOS type questions.
We might need to have a new tcl that would work for KAOS type questions in the future.
Thank you again, and I hope we work out to upgrade this TCL script to do the addition of "?" to the end of each question line.
### Trivia Question File Format Converter ***
# formatfile.tcl v0.1 by SpiKe^^ (1Dec2021) *
# This script will convert trivia files from Q*A to A|Q #
# Also convert Q*A1*A2*A3 to A1|Q (uses first answer) #
# Also adds a ? to the end of questions, if has none #
# Also removes any kaos and scramble questions #
# load this script to the eggdrop bot.
# rename the source file to old.file & put in scripts/ dir.
# private message the bot with .formatfile
bind msg mno .formatfile format:file
proc format:file {nick uhost hand arg} {
set oldfilename "scripts/old.file"
set newfilename "scripts/new.file"
if {![file exists $oldfilename]} {
putserv "PRIVMSG $nick :Error: File not found: $oldfilename"
return 0
}
set openold [open $oldfilename r]
set opennew [open $newfilename w]
set cnt 0
while {![eof $openold]} {
lassign [split [gets $openold] "*"] ques answ
set ques [string trim $ques]
set answ [string trim $answ]
if {$ques eq "" || $answ eq ""} { continue }
if {[string match -nocase "kaos:*" $ques]} { continue }
if {[regexp -nocase {^(scramble|uword)$} $ques]} { continue }
if {![string match {*[.?]} $ques]} { append ques "?" }
incr cnt
puts $opennew "${answ}|$ques"
}
close $openold
close $opennew
if {$cnt == 0} { file delete $newfilename
putserv "PRIVMSG $nick :Error: No valid questions found in $oldfilename"
} else {
putserv "PRIVMSG $nick :Saved $cnt reformatted questions to $newfilename"
}
return 0
}
putlog "formatfile.tcl ver 0.1 by SpiKe^^ Loaded."
### Trivia Question File Format Converter 2 ###
# formatfile2.tcl v0.3 by SpiKe^^ (7Dec2021) #
### Public command: .format2 :will convert trivia files from...
#
# 1. Question: What year did Disneyland open?
# Answer: 1955.
#
# ...to something more like:
# 1955|What year did Disneyland open?
### You can also specify a Question Category for the current file.
# Public command: .format2 Disney Trivia :will convert to...
#
# 1955|Disney Trivia: What year did Disneyland open?
# Script will also add a ? to the end of questions, if has none #
### load this script to the eggdrop bot.
### rename the source file to old.file & put in scripts/ dir.
### private message the bot with .format2 |or| .format2 Category-Name
bind msg mno .format2 format:file2
proc format:file2 {nick uhost hand arg} {
set oldfilename "scripts/old.file"
set newfilename "scripts/new.file"
if {![file exists $oldfilename]} {
putserv "PRIVMSG $nick :Error: File not found: $oldfilename"
return 0
}
set arg [string trim $arg]
if {$arg ne ""} {
if {![string match "*:" $arg]} { append arg ":" }
append arg " "
}
set openold [open $oldfilename r]
set opennew [open $newfilename w]
set cnt 0
set ques "" ; set answ ""
while {![eof $openold]} {
set line [string trim [gets $openold]]
if {$line eq ""} { continue }
if {$ques eq ""} {
if {![string match -nocase "answer: *" $line]} { set ques $line }
continue
}
if {[lsearch -nocase [lrange [split $line] 0 1] "question:"] > -1} {
set ques $line ; continue
}
set answ $line
set x [lsearch -nocase [lrange [split $ques] 0 1] "question:"]
if {$x > -1} {
regexp -nocase {question:(.*?)$} $ques - ques
set ques [string trim $ques]
}
if {[string tolower [lindex [split $answ] 0]] eq "answer:"} {
regexp -nocase {answer:(.*?)$} $answ - answ
set answ [string trim $answ ". "]
}
if {$ques ne "" && $answ ne ""} { incr cnt
if {![string match {*[.?]} $ques]} { append ques "?" }
puts $opennew "${answ}|${arg}$ques"
}
set ques "" ; set answ ""
}
close $openold
close $opennew
if {$cnt == 0} { file delete $newfilename
putserv "PRIVMSG $nick :Error: No valid questions found in $oldfilename"
} else {
putserv "PRIVMSG $nick :Saved $cnt reformatted questions to $newfilename"
}
return 0
}
putlog "formatfile2.tcl ver 0.3 by SpiKe^^ Loaded."