How do I display an array in a table field?

Arrays are not plain text and cannot be displayed in a field without some extra work first.

This lesson shows how to use the "combine" command to convert data from an array to text.

Create a test stack

Create a test stack

Create a new stack by selecting "New Stack" from the File menu.

Drag a button and a table field from the Tools palette into your stack.

Open the Inspector and change the name of the button to "Array" and the field to "ArrayData"

Create an array

Select the "Array" button and edit its script.

The first thing is to create a simple array for use in our tests, so copy the script below into the button's script and click the "Apply" button.

on mouseUp

repeat with x = 1 to 20

put random(1000) into tArray[x]

end repeat

end mouseUp

This creates an array with 20 members, each of which contains a random number between 1 and 1000 e.g.

tArray[1] = 492, tArray[20] = 3 although the actual data will vary each time.

Try to display the array data

Try to display the array data

For testing purposes, let's see what happens if you just try to display the array in the table field as is.

Add the following line to the bottom of the button's mouseUp handler, then click "Apply".

put tArray into field "ArrayData"

This is just treating the array variable as if it was any text or numeric variable and putting it into the field.

Switch back to the browse tool and click the "Array" button.

The Script Editor window will appear as shown above, showing the error and highlighting the problem line.

Make sure the "Error" tab is selected in the bottom pane and you will see that the error is "Chunk: can't store to destination container", which is telling us that we can't put an array directly into a field.

Click the blue square button to stop debugging.

Using "combine" to convert an array to text

The "combine" command can take an array and change it into text form.

Change the script so that it reads like this:

on mouseUp

repeat with x = 1 to 20

put random(1000) into tArray[x]

end repeat

combine tArray using return

put tArray into field "ArrayData"

end mouseUp

Compile this script and try clicking on the button again.

This time you should see a list of random numbers appear in the first column of the table field.

By telling the "combine" command to combine using return, the script has taken each entry in the array and put it on a new line in the list. The tArray variable has been changed to a text variable and is no longer an array, so it can be displayed in a field.

Showing the array index with the data

Showing the array index with the data

While the previous script did allow us to display the contents of the array in a field, it wasn't very useful because it didn't show which bit of data was in which element of the original array. To show both the array index and the array data, we have to add an extra parameter - the secondary delimiter - to the "combine" command.

Edit the "Array" button's script again and change the "combine" line as follows:

combine tArray using return and tab

This time, when you click the button you will see two columns in the table field filled in. I made the table field slightly longer so I could see all 20 lines of data.

By specifying "return and tab", the command showed both the array index and the data, separated by a tab, with each section on a new line.

Sorting the list

As you can see from the previous step, the array is sorted as if it was text, not as if it was numbers. You should never assume that arrays will be sorted in any particular way and especially not in the order they were filled.

To sort the array, we need to "combine" it into text first, then sort the text before displaying it.

Copy the line below into the Array button's script, just after the "combine" line:

sort lines of tArray numeric by word 1 of each

Then when you click the button again, the list will be sorted numerically by the array index i.e. it will correctly list from 1 to 20.

Variable types

One important thing to remember is that once you have used the "combine" command, your array variable is no longer an array!

If you need to keep your data as an array but just want to display it temporarily, copy the array to a new temporary variable before combining the temporary variable and leaving the original array variable as an array.


Add your comment

E-Mail me when someone replies to this comment