# Filling a table from 2 other tables

**URL:** <https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872>\
**Category:** General Discussion\
**Created:** [July 20, 2020, 5:37pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872 "2020-07-20T17:37:19Z")\
**Posts on this page:** 6\
**Page:** 4

<div class="post-metadata">

**Author:** ![toddayers](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@toddayers](https://forum.inductiveautomation.com/u/toddayers)\
**Post date:** [July 27, 2020, 1:20pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872/61 "2020-07-27T13:20:44Z")

</div>

Nope…same thing.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 27, 2020, 2:30pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872/62 "2020-07-27T14:30:40Z")

</div>

In my script I had a space in ‘Position Table’, so make sure it matches what you have.  
This one has a couple of print statements in there as well, so we can see in the console what the list values are.

```python
table = event.source.parent.getComponent('PositionTable')
dataIn = table.data
headers = list(dataIn.getColumnNames())
positionList = dataIn.getColumnAsList(0)
print headers
print positionList
dataOut = []
for row in positionList:
	dataOut.append([row, '', 0])
table.data = system.dataset.toDataSet(headers, dataOut)
```

---

<div class="post-metadata">

**Author:** ![toddayers](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@toddayers](https://forum.inductiveautomation.com/u/toddayers)\
**Post date:** [July 27, 2020, 2:35pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872/63 "2020-07-27T14:35:47Z")

</div>

This is what I got in the output console:

[u’Position’, u’EmployeeID’, u’FirstName’, u’LastName’, u’Shift’, u’Department’]  
[Janitor]  
09:34:43.824 [AWT-EventQueue-2] ERROR com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter - Error executing script for event:&nbsp;`actionPerformed`  
on component:&nbsp;`Button`.  
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):  
File “event:actionPerformed”, line 10, in   
IndexError: Row 0 doesn’t have the same number of columns as header list.

```
at org.python.core.Py.IndexError(Py.java:250)
at com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities.toDataSet(DatasetUtilities.java:489)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:186)
at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:204)
at org.python.core.PyObject. __call__ (PyObject.java:404)
at org.python.core.PyObject. __call__ (PyObject.java:408)
at org.python.pycode._pyx65.f$0(<event:actionPerformed>:10)
at org.python.pycode._pyx65.call_function(<event:actionPerformed>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:636)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:180)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:271)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy32.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at com.inductiveautomation.snap.swing.RibsEventQueue.dispatchEvent(RibsEventQueue.java:95)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

```

Caused by: org.python.core.PyException: Traceback (most recent call last):  
File “event:actionPerformed”, line 10, in   
IndexError: Row 0 doesn’t have the same number of columns as header list.

```
... 57 common frames omitted
```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 27, 2020, 2:45pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872/64 "2020-07-27T14:45:47Z")

</div>

Doh! 🤦‍♂️ I had manually padded the row to the length I needed for the example.

Modified to grab the number of headers, and pad the rows accordingly. Now, if you change your columns, it should follow.

```python
# Decalre table component
table = event.source.parent.getComponent('PositionTable')
# Get the data from the table.
dataIn = table.data
# Get the table headers, so we can keep them
headers = list(dataIn.getColumnNames())
# Get the column data holding the positions 
positionList = dataIn.getColumnAsList(0)
# Set how many columns we have to pad
# This is the length of the header list minus 1,
# Because position will take up one.
padding = len(headers)-1
dataOut = []
for row in positionList:
	# Generate data for a new row
	# [element] * x will give a repeating list of x elements.
	# e.g. [''] * 3 will result in ['', '', '']
	# We use this to pad out the row to the same number
	# of columns listed in the header.
	newRow = [row] + [''] * padding
	# Add new row to dataOut
	dataOut.append(newRow)
# Write dataset back to the table
table.data = system.dataset.toDataSet(headers, dataOut)

```

---

<div class="post-metadata">

**Author:** ![toddayers](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@toddayers](https://forum.inductiveautomation.com/u/toddayers)\
**Post date:** [July 27, 2020, 2:50pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872/65 "2020-07-27T14:50:23Z")

</div>

Ok… for future reference can you explain to me exactly what is taking place with this script?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 27, 2020, 3:05pm UTC](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872/66 "2020-07-27T15:05:07Z")

</div>

Edited my post above to add comments.

you can see how list adding and multiplying works by putting this into the script console:

```python
list1 = ['abc'] + ['x'] * 2
print 'list1 = ', list1

list2 = ['abc'] + ['x'] * 3
print 'list2 = ', list2

list3 = ['abc'] + ['x'] * 4
print 'list3 = ', list3

```

 ![2020-07-27_11-04-41](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/f/2fecaefbb626981c5d395f0f0cceebff988dd7c1.png)

[Previous page](https://forum.inductiveautomation.com/t/filling-a-table-from-2-other-tables/36872.md?page=3)
