Home   Archive   Permalink



A little indirection problem with data conversion

I am trying to make a little general-purpose program to generate a lookup table from a csv file. In other words, the program has to work with words and values and types that are not known until the program actually runs. I have almost got it, but I am having a bit of trouble converting a value to a type when the type is determined at run time. Sort of hard to explain but I think I have isolated the issue in the script below. I want to convert a string of digits to an integer, but that data type of integer! is not determined until the program runs. I wonder if anyone might have any thoughts on the matter.
    
Sample script: (This is a complete program that will run.)
    
R E B O L []
    
EMPNAME: "MR WHITE"
EMPEXT: "104"
    
ATTRIBUTE-LIST: [EMPNAME string! EMPEXT integer!]
    
LOOKUPBLOCK: []
    
foreach [WRD TYP] ATTRIBUTE-LIST [
     append LOOKUPBLOCK WRD
     print ["Converting " mold get WRD " to " TYP]
     append LOOKUPBLOCK mold to TYP get WRD ;; This is the "problem line."
]
    
print mold LOOKUPBLOCK
print {What I want is: [EMPNAME "MR WHITE" EMPEXT 104]}
    
halt
    
Result when run: (Note that EMPEXT is not being converted to an integer.)
    
Converting "MR WHITE" to string!
Converting "104" to integer!
[EMPNAME "MR WHITE" EMPEXT "104"]
What I want is: [EMPNAME "MR WHITE" EMPEXT 104]
>>
    

posted by:   Steven White     29-Mar-2018/10:54:28-7:00



Couple of things here I notice are:
    
1) When you do FOREACH [WRD TYP] ATTRIBUTE-LIST, you are only assigning the words STRING! and INTEGER! to TYP, not the datatype values. TO TYP will therefore not work as you'd expect, rather: TO GET TYP is required.
    
2) You MOLD your value before appending to LOOKUPBLOCK, thus stringifying every value.
    
     EMPNAME: "MR WHITE"
     EMPEXT: "104"
    
     ATTRIBUTE-LIST: [EMPNAME string! EMPEXT integer!]
    
     LOOKUPBLOCK: collect [
         foreach [WRD TYP] ATTRIBUTE-LIST [
             keep WRD
             print ["Converting " mold get WRD " to " TYP]
             keep to get TYP get WRD
         ]
     ]
    
     probe LOOKUPBLOCK
     print {What I want is: [EMPNAME "MR WHITE" EMPEXT 104]}


posted by:   Chris     29-Mar-2018/12:52:23-7:00



Chris, thank you, that worked nicely.
    
The reason I "molded" everything was that I thought "mold" was the way to get things to look like what they are, in human-readable terms. In other words, molding a string would put quotes around it, and molding an integer would leave it as quote-less digits.    
    
And as for "collect," I do recall hearing about it somewhere, but I don't see it in the REBOL function dictionary so I didn't remember it, even if I would have known how to use it. So we make friends with a new skill today.

posted by:   Steven White     29-Mar-2018/14:56:38-7:00



MOLD will serialize any value. It's sort of like SAVE, though instead of saving somewhere, it just returns a string.
    
COLLECT is a relatively recent feature. Very handy for building up data blocks. Within its context you have KEEP (and KEEP/ONLY) that serve to accumulate values.
    
     collect [foreach value [2 4 6][keep pick system/locale/days value]]

posted by:   Chris     29-Mar-2018/15:31:42-7:00