# Table removeColumn(index) not working correctly?

**URL:** <https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585>\
**Category:** Coding Questions\
**Created:** [February 6, 2020, 12:59pm UTC](https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585 "2020-02-06T12:59:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![plux](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@plux](https://discourse.processing.org/u/plux)\
**Post date:** [February 6, 2020, 12:59pm UTC](https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585/1 "2020-02-06T12:59:31Z")

</div>

Hello, as followup to [this](https://discourse.processing.org/t/open-file-in-folder-and-subfolders/17552/4) post.  
I’m now trying to remove some columns but I’m having strange issue.  
Basically this:

```auto
    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

```auto
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!");
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 6, 2020, 1:06pm UTC](https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585/2 "2020-02-06T13:06:43Z")

</div>

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

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

```

instead

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 12, 2020, 6:22pm UTC](https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585/3 "2020-02-12T18:22:11Z")

</div>

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:

```auto
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 []

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 12, 2020, 6:47pm UTC](https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585/4 "2020-02-12T18:47:49Z")

</div>

> [@jeremydouglass](#):
>
> Also, _in general_, do not iterate over things forward and delete (due to the skipping problem, above).

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

```auto
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. 😮

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

```auto
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. 🤓
