Table removeColumn(index) not working correctly?

Hello, as followup to this post.
I’m now trying to remove some columns but I’m having strange issue.
Basically this:

    table.removeColumn(0);
    table.removeColumn(1);
    table.removeColumn(2);

will remove columns 0, 2 and 4 instead of columns 0, 1 and 2. I thought IDs was basically the order in which columns appear in the spreadsheet?

full code

void setup() {  
  String[] files = listPaths(dataPath(""), "files", "recursive", "extensions=,csv");
  int rowCount = 100000;
    
  for(int i = 0; i < files.length; i++){
    Table table = loadTable(files[i]);
    if(table.getRowCount() < rowCount)
      rowCount = table.getRowCount();
  }
  
  int index = 1;
  String parent = "";
  for(int i = 0; i < files.length; i++){
    if(!parent.equals(new File(files[i]).getParent()))
      index = 1;

    parent = new File(files[i]).getParent();  
    Table table = loadTable(files[i]);
    //println(table.getColumnCount());
    table.removeColumn(0);
    table.removeColumn(1);
    table.removeColumn(2);
    table.setRowCount(rowCount - 1);
    saveTable(table, parent + "_p" + (index++) + ".csv");
  }
  println("Done!");
}
1 Like

Isn’t that because once you remove column 0,

  • the column 1 becomes column 0 and
  • the column 2 becomes column 1 etc.?

So when you come to your 2nd line table.removeColumn(1); you already had this shift, so you delete your previous column 2.

Suggestion

You could say

    table.removeColumn(2);
    table.removeColumn(1);
    table.removeColumn(0);

instead

2 Likes

Also, in general, do not iterate over things forward and delete (due to the skipping problem, above). Iterate over things backwards and delete. This is true for Table, TableRow, ArrayList, IntList, StringList, et cetera. Here is an example of what happens when you do it wrong vs. right:

IntList nums;

println("forwards");
nums = new IntList(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
for(int i=0; i<nums.size(); i++){
  println(nums.get(i));
  nums.remove(i);
}
println(nums, "\n");

println("backwards");
nums = new IntList(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
for(int i=nums.size()-1; i>=0; i--){
  println(nums.get(i));
  nums.remove(i);
}
println(nums, "\n");

Output:

forwards
0
2
4
6
8
IntList size=5 [ 1, 3, 5, 7, 9 ]

backwards
9
8
7
6
5
4
3
2
1
0
IntList size=0 [ ]

1 Like

For completeness’ sake, although I prefer backwards :back: loops for deleting elements, here’s how to do the same w/ a forward :arrow_forward: loop the right way: :smile_cat:

final IntList nums = IntList.fromRange(10);
println(nums);
println("Forwards:");

for(int i = 0; i < nums.size(); ++i) {
  println("index:", i, TAB, "value:", nums.get(i));
  nums.remove(i--);
}

println(nums);
exit();

B/c we’re emptying the whole container, the index i is stuck at value 0 on all iterations. :open_mouth:

Here’s the same example; but this time we’re sparing the indices containing the values 5 & 8: :smirk:

final IntList nums = IntList.fromRange(10);
println(nums);
println("Forwards:");

for (int i = 0; i < nums.size(); ++i) {
  final int val = nums.get(i);
  println("index:", i, TAB, "value:", val);
  if (val != 5 & val != 8)  nums.remove(i--);
}

println(nums);
exit();

The index i ends up w/ value 2; b/c that’s how many indices which has been spared. :nerd_face:

1 Like