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 elements, 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, nothing will happen as an array cannot be directly displayed in a field.

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 element 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 element on a new line.

Sorting the list

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.

1 Comments

bogs

There is another way to do this which is non-destructive to the array, and that is to skip combining it.

on mouseUp
repeat with x = 1 to 20
put random(1000) into tArray[x]
end repeat
repeat for each line theKey in the Keys of tArray
put theKey & tab & tArray[theKey] into line theKey of field "Field"
end repeat
end mouseUp

It also came out already sorted, although I suspect that is because of how it was generated, however, sorting it after the fact can be done directly in the field with (almost) the same code displayed above -

sort lines of field "FieldName" by word 1 of each.

The above is mildly efficient, if you set the lock screen, for instance, it takes 2 to 3 milliseconds to run it 50,000 times. If you don't lock the screen heh, well, let's just say lock the screen if your going to run it 50k times (but the effect of the changing numbers was pretty cool :D )

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.