Home   Archive   Permalink



CSV and the "set" function

R E B O L [
     Title: "Question about 'set"
]
    
;; [--------------------------------------------------------------------------]
;; [ This question is in the form of a script so it can be copied into a     ]
;; [ file and executed, to show the question.                                 ]
;; [                                                                         ]
;; [ I have read Nick A's procedures for working with CSV files, and         ]
;; [ believe I understand them, and have a slight variation that I am         ]
;; [ trying to make work. It does work, down to one final detail.            ]
;; [                                                                         ]
;; [ I have a CSV file with the first line containing field names.            ]
;; [ If I parse the first line, I get a block of strings, each string         ]
;; [ being a column name. Rather than look up a a column name in the         ]
;; [ list of column names and use the index of the column name to index     ]
;; [ into the data, I want to try a variation.                                ]
;; [                                                                         ]
;; [ First, I use the to-word function to convert each column name into     ]
;; [ a word, and store those words in a block.                                ]
;; [                                                                         ]
;; [ Next, when I want to process a data record, I parse it into strings,     ]
;; [ and then, for each word in the heading block, I use the set function     ]
;; [ to set that word to the corresponding value in the data record.         ]
;; [ The result of that is that I can use the column names as words in        ]
;; [ the source code of a program.                                            ]
;; [                                                                         ]
;; [ I am not proposing, by the way, that this is a preferred way of         ]
;; [ doing things, it is just where my investigations took me.                ]
;; [                                                                         ]
;; [ So this all works, except for one touch that I thought might be nice.    ]
;; [                                                                         ]
;; [ The values in a data record, after that record is parsed on its         ]
;; [ commas, are all strings, even if they really are not. It would be     ]
;; [ nice if, when I set a word to a value that is a date, it could         ]
;; [ have a "type" of "date" instead of "string." I am wondering if         ]
;; [ there is a function that does that. I have tried some combinations     ]
;; [ of "do" and "load" without success.                                     ]
;; [                                                                         ]
;; [ Thank you.                                                             ]
;; [--------------------------------------------------------------------------]
    
;; -- Column headings. These were in the first line of the CSV file
;; -- looking like this: name,birthdate,email.
;; -- They were parsed into strings as ["name" "birthdate" "email"]
;; -- and then converted to words with to-word.    
DATA-NAMES: [name birthdate email]
    
;; -- Data values from a typical record. These were originally in a desired
;; -- form, looking like this: "Steven",01-JAN-2000,steve@aol.com.
;; -- After parsing, they became a block of strings.
DATA-VALUES: ["Steven" "01-JAN-2000" "steve@aol.com"]
    
;; -- This counter is used for indexing into the data as we look through
;; -- the column headings.
VALUE-COUNTER: 0
    
;; -- Procedure for processing a data record.
;; -- For each column heading word, pick the corresponding data value
;; -- and set the heading word to that value.
foreach DATA-NAME DATA-NAMES [
     VALUE-COUNTER: VALUE-COUNTER + 1
     set DATA-NAME pick DATA-VALUES VALUE-COUNTER
]
    
;; -- And it works. We can refer to the data values by name.
print ["name = " name ", type = " type? name]
print ["birthdate = " birthdate ", type = " type? birthdate]
print ["email = " email ", type = " type? email]
    
;; -- BUT, all data items are strings. The program could convert them to
;; -- other data types, but this procedure is going to be in a
;; -- general-purpose module that handles any CSV file, so I thought it
;; -- would be a nice touch to have the right data types set when a
;; -- calling program asks for each CSV record.
    
;; -- I am not sure if the above is clear enough, so here is another
;; -- phrasing. If I wanted to read each record of the CSV file and
;; -- send an email to each address, I currently would have to use
;; -- to to-email function on the "email" word to convert it to email
;; -- before sending. But, if the general-purpose module for processing
;; -- any arbitrary CSV file could recognize that email data type and
;; -- set the type of the "email" word, then my coding could omit that
;; -- to-email conversion step.
    
halt


posted by:   Steven White     25-Mar-2014/10:41:11-7:00