Saving arrayObject to csv

I have arrayObject which has many variables and I want to save it's data to csv.

Here is my beginning of the object:

class fixture {
  //Variables---------------------------------------------------------------------------------
  int dimmer; //dimmer value
  int red, green, blue; //color values
  int pan, tilt, panFine, tiltFine; //rotation values
  int x_location, y_location, z_location; //location in visualisation
  int rotationX, rotationZ;
  int parameter;
  int colorWheel, goboWheel, goboRotation, prism, focus, shutter, strobe, responseSpeed, autoPrograms, specialFunctions; //special values for moving heads etc.
  int haze, fan, fog; //Pyro values

  String fixtureType;
  int fixtureTypeId;
  int channelStart;

  int parentAnsa;

And here is the code which I use nowadays to save data. I think it works, but it is really bad code.

void save1Darray(int[] array, String arrayName) {
  for(int i = 0; i < array.length; i++) {
    TableRow newRow = table.addRow();             
    newRow.setInt("id", table.lastRowIndex());  
    newRow.setString("variable_name", arrayName);
    newRow.setString("variable_dimensions", "1"); 
    newRow.setString("value", str(array[i]));   
    newRow.setString("1D", str(i));               
    newRow.setString("2D", "-");
  }
}


for (int i = 0; i < fixtures.length; i++) { tempFixtureObjectArray[i] = fixtures[i].dimmer; }
  save1Darray(tempFixtureObjectArray, "fixtures.dimmer");

  for (int i = 0; i < fixtures.length; i++) { tempFixtureObjectArray[i] = fixtures[i].red; }
  save1Darray(tempFixtureObjectArray, "fixtures.red");

  for (int i = 0; i < fixtures.length; i++) { tempFixtureObjectArray[i] = fixtures[i].green; }
  save1Darray(tempFixtureObjectArray, "fixtures.green");

  for (int i = 0; i < fixtures.length; i++) { tempFixtureObjectArray[i] = fixtures[i].blue; }
  save1Darray(tempFixtureObjectArray, "fixtures.blue");

Thanks for your helpo!

Answers

  • edited October 2014

    Fist: To newcomers in this forum: read attentively these instructions
    Beware of code formatting and category choice.

    Fixed that for you.

    Next, indeed, this isn't very good... :-) (but that's an interesting take on the problem).
    Do you really need to save in CSV format?

    There was a recent thread about saving the state of objects, and CSV isn't really a good option (you don't save strings the same way you save an int or a float).
    In Java, you can use serialization, but it has problems when modifying the class (adding a field, for example) and trying to read back old states.
    You can save in another format, like Json (using Jackson for example) or XML (XStream is quite good at this).

Sign In or Register to comment.