4

Newbie question here... I'd like to write the output of the "what" function to a text file.

So here is what I have done:

I've created a variable called "text" and assigned the output of "what" to it

text: [what]

Now I want to write the content of the "text" variable to a txt file...

Any help is appreciated. Thanks in advance!

sqlab
  • 6,412
  • 1
  • 14
  • 29
Hugo
  • 135
  • 1
  • 1
  • 7

3 Answers3

4

the easiest way to write the output of statements to a file is to use

echo %file.log
what

with echo none you end this

>> 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
sqlab
  • 6,412
  • 1
  • 14
  • 29
3

Unfortunately there is not really a value returned from the what function:

Try the following in the console:

 print ["Value of `what` is: " what]

So write %filename.txt [what] will not work.

Instead, what you could do is to look at the source of what

source what

which returns:

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
]

See that this function only prints (it doesn't return the values it finds) We can modify the script to do what you want:

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
]

This function is a little hackish (filename is set, but it will return what you want). You can extend the function to accept a filename or do whatever you want. The tab and newline are there to make the file output prettier.

Important things to notice from this:

  1. Print returns unset
  2. Use source to find out what functions do
  3. write %filename value will write out a value to a file all at once. If you open a file, you can write more times.
kealist
  • 1,669
  • 12
  • 26
0

Fairly elementary: use write if you just want to save some text, read to recover it; use save if you want to store some data and use load to recover it.

>> write %file.txt "Some Text"
>> read %file.txt
== "Some Text"

>> text: [what]
>> save/all %file.r text
>> load %file.r
== [what]

You can get more information on each word at the prompt: help save or view online: load, save, read and write.

rgchris
  • 3,698
  • 19
  • 19