Home   Archive   Permalink

Discover if a word exists

I feel like I have asked this before. I did look in the archives but did not see anything.
If I have a text file with some REBOL-readable items in it, like
%filename.txt contains:
and I load that file with
do load %filename.txt
Then the word DATA-NAME-1 will exist in my program and I can refer to its value.
BUT, if, in the text file, I mis-spell a word, like
DATE-NAME-1: "TEST VALUE" (note DATE instead of DATA)
then after I load the file an try to refer to DATA-NAME-1, I will get an error that DATA-NAME-1 has no value. The file will load, but because I spelled the above word in the file as DATE and tried to refer to DATA, I get that run-time error.    
I would like to catch this situation by checking, in my program, after I "load" the file, whether or not the word DATA-NAME-1 exists. If it does not exist (because it was spelled wrong in the file I loaded), then I could provide my own error message.
Is there a way I can check to see if a word in my program exists?
Thank you.

posted by:   Steven White     8-Feb-2018/13:34:05-8:00

value? 'data-name-1

posted by:   ange     8-Feb-2018/14:26:56-8:00

Now my question:
what "unset?" is for ?

posted by:   Giuseppe Chillemi     27-Mar-2018/2:25:27-7:00

value? takes a word! data-type as it's argument, unset? appears to take any data type, so the example given in the online dictionary for unset? (http://www.rebol.com/docs/words/wunsetq.html):
if unset? do [print "test"] [print "Nothing was returned"]
results in an error when used with the value? function:
if value? do [print "test"] [print "Nothing was returned"]
I've typically used value? to test if a word exists.

posted by:   Nick     29-Mar-2018/9:49:16-7:00

So now what about the reverse?
Let's say I have two files:
File 1 contains
DATA-NAME-1: "File 1 value 1"
DATA-NAME-2: "File 1 value 2"
File 2 contains:
DATA-NAME-1: "File 2 value 2"
If I "load" file 1, then DATA-NAME-1 will come into existence and DATA-NAME-2 will come into existence. I can refer to them, and I can use the "value?" function to find out if they exist.
If I subsequently "load" file 2, DATA-NAME-1 will get the new value from file 2, but I suppose DATA-NAME-2 will still exist with the value from file 1. How can I "obliterate" the words DATA-NAME-1 and DATA-NAME-2 BEFORE I try to load file 2, and then discover that DATA-NAME-2 does not exist in file 2 by loading file 2.

posted by:   Steven White     11-Apr-2018/12:01:36-7:00

foreach wrd [DATA-NAME-1 DATA-NAME-2] [unset wrd]
Is it necessary to store the word definition in the file? Why not store values only in the data file, and assign word labels in your code?

posted by:   Nick     12-Apr-2018/23:04:59-7:00

Thank you for that tip. I believe I can apply it.
The core reason why I do anything in REBOL the way I do is that I don't know any better. In another area, a small program that will go "on-line" in 2018 to manage a small summer tennis ladder, I have done as you suggest, put only the data in a file and assign words when I load it. The idea being explored above is for another project, a little photo labeling system to use when I start mining the large box of pictures from my late mother in search of little gems to pass along to my relatives.    
One part of this system will be the concept of an "index card" for each picture. If a picture is called photo1.jpg, then it will have an optional index card called photo1.txt. The index card will have the structured text, as suggested above.
FILENAME: %photo1.jpg
DATETAKEN: "Summer 1960"
...etc. Something like this but not as extensive: http://dublincore.org/documents/dces/
What I am thinking could happen is that I will define the contents of an index card and start making them, and then at some point I will decide that I need some other attribute, like this:
EVENT: "Steven graduation"
So I will add that item to all future index cards, but I won't want to go back and add to all exiting index cards something like:
EVENT: none
...although I suppose it would not be a hard project to go back and add that line to all index cards that don't have it...
Anyway, if I get to a state where some index cards have items that others do not have, then when I load a card WITHOUT some item I don't want to pick up a value from some other previously-read card WITH that item.
The format of the index card, with the "words" built in, makes the card readable by a person without any special computer program. This would be valuable if I want to email a picture, with its index card, to a relative.
And finally, the reason I am doing this at all is that I want to see if I can, and also that I don't want my "metadata" locked up in some format such that I can't get to it if I switch computers or if the maker of some other photo-indexing product goes out of business. I have a bit of NIH syndrome (Not Invented Here).

posted by:   Steven White     13-Apr-2018/9:41:35-7:00

There are a few different ways to manage an unknown, or varying, number of data fields. Basically, storing each record in a block gives you more flexibility. If you store the data 'flat', you're tied to dealing with a predefined 'column and row' structure:
[record1-value1 record1-value2 record2-value1 record2-value2] ; etc.
If you want to alter/expand the above data structure beyond 2 values per 'record', you need to add an empty value after every 2 two values in the existing data (either manually or with a script).
If you enclose each record in its own block, then you can add more values to each record's data structure as needed, without altering any code which deals with the first to values. And, you can always simply check the length of each block to see how many values are already contained in existing nested sub-blocks:
    [record1-value1 record1-value2]
    [record2-value1 record2-value2]
If you search for "flatten" at http://business-programming.com/business_programming.html , you'll find some generic code, examples, and explanations to help convert back and forth between 'flat' and nested data structures.

posted by:   Nick     13-Apr-2018/10:24:21-7:00