Save a value to a block instead of the word label that represents it
Asked Answered
P

4

5

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.

Position answered 14/2, 2013 at 18:25 Comment(0)
C
3

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.

Cherenkov answered 15/2, 2013 at 0:29 Comment(0)
F
3

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 .

Foothold answered 29/5, 2013 at 12:57 Comment(0)
F
3

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.

Foothold answered 29/5, 2013 at 13:5 Comment(0)
H
1

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 ]
Hubie answered 14/2, 2013 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.