I am trying to save a few string values into a block so that I can save that block to a text file. I am getting these values from a form using VID.

One way to do this would be to simply save strings to the file. But I would prefer being able to retrieve the data as a block.

This is what I intend to do:

view layout [
    contact-name: field
    save-button: btn "Save" [
        saved-data-block: copy []
        append saved-data-block [[contact-name: contact-name/text]] ;problem here
        save my-file saved-data-block
    ]
]

For an input like Rebol User in the name field, the content stored in the file should be something like [contact-name: "Rebol User"], but the content is [contact-name: contact-name/text]

I understand that the problem is that the block is not being evaluated as code at the time I am appending it to saved-data-block. What can I do to save the string value to the text file in a block-like manner? Should I be doing something else to achieve this? Any comments/queries are welcome.

有帮助吗?

解决方案

If you aren't required to specifically use reduce, compose can be a better way of calling out the parts you want to be "left alone". Only things in parentheses will be evaluated, everything else untouched. So for instance:

append/only saved-data-block compose [contact-name: (get-face contact-name)]

Compose is often the clearest way to express boilerplate with little parts noted that you want to be evaluated. Of course, it's good to understand reduce too.

其他提示

And here is another idea:

append/only saved-data-block repend [contact-name:] get-face contact-name 

This time contact-name: isn't reduced, so it stays a set-word!, and the value from the form element is appended.

Doesn't look nice with the double append, but saves on key-strokes.

Grahams answer actually misses a tick :-)

append/only saved-data-block reduce [ to-set-word 'contact-name get-face contact-name ]

Without the ' reduce reduces contact-name to its value, an object! before it is handed over to to-set-word.

So, you have to change contact-name to a lit-word! 'contact-name .

reduce reduces this to a word!, which will then be handed to to-set-word .

Reduce will also reset the name of the field which is also "contact-name".

So, this would be better

append/only saved-data-block reduce [ to-set-word contact-name get-face contact-name ]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top