Hi,
I am trying to:
- Load data from (tab delimited) file with various data types (columns), e.g. location, country, latitude, longitude
- Determine minimum value for selected columns using a function (e.g. latitude)
I have created:
- class Record - to create an array with a data element for each data type (column)
- class ImportData - to load the data from file
Now I am trying to address the data element (e.g. records[i].latitude) dynamically when calling the function .getColumnMin passing a parameter (e.g. .getColumnMin(latitude)). Unfortunately this returns an error ("fld cannot be resolved or is not a field"). Respective lines of code highlighted in yellow below.
Can anyone help?
Code (shortened):
class Record {
[...]
String location;
float latitude;
[...]
public Record(String[] pieces) {
[...]
location = pieces[4];
latitude = float(pieces[5]);
[...]
}
}
------------------------------------
Record[] records;
String[] rows;
class ImportData {
int rowCount;
String[] columnNames, rowNames;
ImportData(String filename) {
rows = loadStrings(filename);
[...]
records = new Record[rows.length];
for (int i = 1; i < rows.length; i++) { // ignore first row with column names
if (trim(rows[i]).length() == 0) {
continue; // skip empty rows
}
if (rows[i].startsWith("#")) {
continue; // skip comment lines
}
String[] pieces = split(rows[i], TAB); // Load data into array
scrubQuotes(pieces);
if (pieces.length == 19) {
records[rowCount] = new Record(pieces);
rowCount++;
}
}
if (rowCount != records.length) {
records = (Record[]) subset(records, 0, rowCount);
}
}
[...]
float getColumnMin(String fld) {
float m = Float.MAX_VALUE;
for (int row = 0; row < rowCount; row++) {
if (records[row].fld < m) { // addressing the data object directly using
if (records[row].latitude < m) works perfectly fine
m = records[row].fld;
}
}
return m;
}
------------------------------------
ImportData data;
[...]
void setup() {
data = new ImportData("data.txt");
latitudeMin = data.getColumnMin("latitude");
println(latitudeMin);
[...]
}
1