GetStateNames

The engine.
Post Reply
jdtoellner
Posts: 71
Joined: Mon Aug 01, 2016 9:45 pm

GetStateNames

Post by jdtoellner »

I can use GetStateNames to retrieve strings from a CSV file:

Code: Select all

string stringVar = GetStateNames(columnIndex[idx])[GetInt(columnIndex[idx], rec)].c_str())
How do I handle a blank cell in the CSV file?

(If I use this code it throws an exception.)
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: GetStateNames

Post by shooltz[BayesFusion] »

Can you post your CSV file here? Alternatively, send me a private message using this forum with file attached.
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: GetStateNames

Post by shooltz[BayesFusion] »

Before you can use the value returned by DSL_dataset::GetInt with the vector returned by DSL_dataset::GetStateNames, you need to check if the value at the specific position in your dataset was missing or not. To do that, call DSL_dataset::IsMissing, which returns true if value is missing.

The vector returned by GetStateNames does not have a special entry for the blank cells from CSV file. Your program crashes (in release mode) or asserts (in debug mode), because GetInt returns -1 for the blanks, and -1 can't be used as an index when calling std::vector's operator[].

Your code should look like this:

Code: Select all

// earlier in the program, certainly before main loop over records
string missingString = "N/A"; // application specific, may be empty string, etc.
// ...
int varIdx = columnIndex[idx];
const string stringVar& = IsMissing(varIdx, rec) ? missingString : GetStateNames(varIdx)[GetInt(varIdx, rec)];
The -1 is the default used by parser for discrete columns; if for some reason you want to use different value for blanks then:
a) create an object of DSL_datasetParseParams class and set its missingInt member to that value.
b) pass its address as 2nd argument when calling DSL_dataset::ReadFile.
Post Reply