Hi,
coming more or less from Python I just started with D. Not a real
programmer, just automating things and looking for a neat
compiled language.
Just to learn, I wrote a function to read CSV-like files (I know
D has its own routine). Since I'm still a bit overwhelmed by the
many complex language features, I'm curious what could I change
to make my code as D'ish as possible?
Thank you for any suggestion,
Thorstein
// Reads CSV-like files with only numeric values in each column
// new_ndv replaces ndv, which is the original no-data-value
double[][]* readNumMatCsv(char[] filePath, char[] ndv, char[]
new_ndv)
{ double[][]* p_numArray;
double[][] numArray;
char[] line;
string noCont = "File content not usable. Quit here.";
string noFile = "Could not read file. Quit here.";
string nonNum = "Found a non-numeric value in data matrix. Quit
here.";
Regex!char re = regex(r"(\n$)");
if(exists(filePath))
{ File f = File(filePath, "r");
if((line = f.readln().dup).length > 0)
{ while (!f.eof())
{ if((line = replaceAll(f.readln().dup.replace(ndv,
new_ndv), re, "")).length > 0)
{ foreach(i;split(line,","))
{ if(isNumeric(i) == false)
{ writeln(nonNum);
return p_numArray;
}
}
if(split(line,",").length > 0)
{ numArray ~= to!(double[])(split(line,","));
}
}
}
p_numArray = &numArray;
} else { writeln(noCont); }
} else { writeln(noFile); }
return p_numArray;
}