Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write contents of a variable to a text file in Rebol 2?

Tags:

rebol

rebol2

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!

like image 947
Hugo Avatar asked Mar 17 '23 23:03

Hugo


2 Answers

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
like image 89
sqlab Avatar answered Mar 20 '23 12:03

sqlab


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.
like image 30
kealist Avatar answered Mar 20 '23 11:03

kealist