Can I tell if a word exists?

Discussion in 'Rebol' started by swhite, Nov 12, 2010.

  1. swhite

    swhite Member

    Let's say I want make a configuration file, or maybe a database with one record per file, and I have some coding in a file list this:

    REBOL []

    NAME: "Steven"
    ADDRESS: "123 Main St"
    PHONE: "123-456-7890"

    If I "do" that file, I will get words for NAME, ADDRESS, PHONE and their values, and I can refer to them in the code.

    But, what if, for one file, PHONE does not exist so there is no line for PHONE. In the code, if I refer to PHONE I will get an error. Is there any way to check if PHONE exists at all before I try to refer to it?

    My default plan would be to initialize all possible words:

    NAME: ""
    ADDRESS: ""
    PHONE: ""

    at the beginning of the script or loop, then "do" the file, and if the line is absent in the file, the corresponding word in the script will have its initial value. But I am wondering, more now out of curiosity then immediate need, if I can tell whether or not a word actually even exists.

    Thank you.
  2. Graham

    Graham Developer Staff Member

    to check if a word exists, use 'value?

    eg. value? 'name
  3. MaxV

    MaxV Member

    I think that the best and simplest way to write data is this way:
    Code:
    a: [ NAME "Steven" ADDRESS "123 Main St" PHONE "123-456-7890"]
    save %temp.txt a
    
    Then you have to read it with the load function and then
    you can use select:
    Code:
    temp: load  %temp.txt
    select temp 'NAME
    
    This way you may write data in any order, but you'll always find them.

    Morever you can:
    Code:
    if ((select temp 'ADDRESS) = none) [ alert "ADRESS not found"]
    

Share This Page