Testing my understanding of the set function
I know how to accomplish the final result of what I am trying to, but I am wondering if there is a better way. I plan to have a data file that will be one line of text containing values understandable to REBOL. I plan to bring that file into memory with the 'load' function which will result in a block of values. I know that I can modify the values by referring to BLOCK-NAME/1, BLOCK-NAME/2, etc., and then can rewrite the block to the data file with the 'save' function. BUT, I an wondering if there is a better way, specifically, by referring to BLOCK-NAME/1, BLOCK-NAME/2, etc. with more meaningful names to indicate what those values really are. The demo script below shows my attempt, which does not work. I am wondering if this can be done at all. Thank you. R E B O L [ ] ;; -- This file contains one line of text, containing: ;; -- 'john' 'quincy' 'adams' DATAFILE-NAME: %set-test.txt ;; -- We want to load the data into memory with one line of code. DATAFILE: load DATAFILE-NAME ;; -- We want these three words to refer to the three values in the file. DATANAMES: [FIRSTNAME MIDDLENAME LASTNAME] ;; -- Set the above words to refer to the three values in the file. set DATANAMES DATAFILE ;; -- Test the 'set' to see that it worked. print 'Starting values:' print [DATAFILE/1 ' = ' FIRSTNAME] print [DATAFILE/2 ' = ' MIDDLENAME] print [DATAFILE/3 ' = ' LASTNAME] ;; -- Try to change the middle name by referring to the word. ;MIDDLENAME: 'steven' MIDDLENAME: copy 'steven' ;; -- Test to show that it did not do as we hoped. print [DATAFILE/2 ' = ' MIDDLENAME] ;; -- We hope to be able to save the new values with one line of code. ;; -- We would like the saved file to contain: ;; -- 'john' 'steven' 'adams' save DATAFILE-NAME DATAFILE print 'Check saved data.' print 'The data file remains unchanged.' halt
posted by: Steven White 1-Jun-2015/11:29:42-7:00
Hi Steven, after you used 'set, you have to references to the same string from within the file. (in my case, the file contains ["peter" "paul" "mary"]) datafile/1 / "peter" - \ firstname when you are setting firstname afterwards, you are removing the second link to the same value, with a new link to a new value >> firstname: "john" "john" ---- firstname "peter" --- datafile/1 What you really want to do is change the value, that is linked >> change clear firstname "john" datafile/1 / "john" -- \ firstname
posted by: Ingo 1-Jun-2015/16:38-7:00
data: [John Baines Adams] names: [first middle last] forall data [ ; to set a block of names set names/1 data/1 ; to a block of data names: next names ; perhaps for ease of reference ] ; names: head names ; middle: 'Quincy ; to edit the middle name forall data [ ; data/1: get names/1 ; names: next names ; ] ; gets the edits to the data block names: head names data: [John Quincy Adams] ; data ready to save
posted by: Lyndon 3-Jun-2015/22:25:47-7:00
|