新手问题在这里...... 我想将“what”函数的输出写入文本文件。
所以这就是我所做的:
我创建了一个名为“text”的变量,并为其分配了“what”的输出
text:[what]
现在我想将“text”变量的内容写入txt文件......
感谢任何帮助。提前谢谢!
答案 0 :(得分:4)
将语句输出写入文件的最简单方法是使用
echo %file.log
what
以echo none
结束此
>> help echo
USAGE:
ECHO target
DESCRIPTION:
Copies console output to a file.
ECHO is a function value.
ARGUMENTS:
target -- (Type: file none logic)
(SPECIAL ATTRIBUTES)
catch
答案 1 :(得分:3)
不幸的是,what
函数没有返回任何值:
在控制台中尝试以下操作:
print ["Value of `what` is: " what]
所以write %filename.txt [what]
无效。
相反,您可以做的是查看what
source what
返回:
what: func [
"Prints a list of globally-defined functions."
/local vals args here total
][
total: copy []
vals: second system/words
foreach word first system/words [
if any-function? first vals [
args: first first vals
if here: find args /local [args: copy/part args here]
append total reduce [word mold args]
]
vals: next vals
]
foreach [word args] sort/skip total 2 [print [word args]]
exit
]
看到这个函数只打印(它没有返回它找到的值)我们可以修改脚本来做你想做的事情:
new-what: func [
"Returns a list of globally-defined functions."
/local vals args here total collected
][
collected: copy []
total: copy []
vals: second system/words
foreach word first system/words [
if any-function? first vals [
args: first first vals
if here: find args /local [args: copy/part args here]
append total reduce [word mold args]
]
vals: next vals
]
foreach [word args] sort/skip total 2 [append collected reduce [word tab args newline]]
write %filename.txt collected
exit
]
这个函数有点hackish(设置了文件名,但它会返回你想要的)。您可以扩展函数以接受文件名或执行您想要的任何操作。 tab
和newline
可以使文件输出更漂亮。
需要注意的重要事项:
source
查找功能write %filename value
会一次向文件写出一个值。如果你open
一个文件,你可以写更多次。答案 2 :(得分:0)