Setting Repeats Using for Each

Document created by Chris Sommerstad Employee on Jul 21, 2017
Version 1Show Document
  • View in full screen mode

Use "Property: For Each" with an Array of Values from a Response

In the first example, a test composition with two test clips is presented. The "First Clip" contains a single message that will receive an array from the server as a message response, while the "Second Clip" will repeat one time for each value that is received. Note that the "First Clip" has scope local set. The "Second Clip" is set to repeat using "for each".

The "First Clip" has a custom clip property, Received count.

Additionally, there are five output values in the message.

The xpath automatically generated by CloudTest to match the single value1 response field was edited manually to create an xpath that matches more than one value. In the example xpath shown on the right, any item under "out" will be matched. This will cause the Received count custom property to automatically be an array. In this case the array will contain the five returned values (e.g. value1 through value5).

The "Second Clip" has two messages and those messages each include a substitution of the original response value for which the current iteration of the Clip is being repeated. The clip will be executed five times, one for each value, and the clip and its elements have access to the value for which it is being executed each time.

In the Composition Editor, the "Second Clip" is set to use "Property: For Each" as shown on the right. The path {%% prop : track : First Clip/Received count %%} has been used to get the value of the Received count custom property in "First Clip".

When the test composition is played, the "First Clip" will run and the message in it will cause the Received count property to be set to an array of values. The "Second Clip" will run one time for each value in that array.

In the Input/Output list, the First Clip's message response is shown.

In the Events List, the value (Array) is shown. The detail for a given array is not shown in results (since it could be prohibitively long).

Use Property: For Each with an Array of Values from a Script

This example shows another use of the "Property: For Each" repeat type. In this case, the value that the repeat keys off of is constructed by a script that defines a two-dimensional array (rather than a one-dimensional array as shown above).

The composition contains two clips: "Property setup Clip" and "Send Clip".

The script in the "Property setup" Clip and will set a property in the ""Send Clip"" that "Send Clip" then repeats based on. The "Send Clip" has "Property: For Each" set as above.

As was the case above, the setup for the "Second Clip" uses "Property: For Each". The repeat value comes from a custom property that is owned by the clip. Since this property is defined within the "Second Clip" in this example, the clip name doesn't have to be specified in the repeat value. The path {%% prop : clip : Channel Map %%} has been used to get the value of the current clip's custom property, which is Channel Map.

The script sets up a hard-coded array and then puts that value into the Channel Map property of the following clip (using currentClip.nextItem).

The hard-coded two-dimensional array defined in this code is a list of cable TV channels. Each outer array contains an inner array with three values: channel abbreviation, channel description, and the channel number. This results in a 5x3 array (e.g. five entries, each with 3 values in it). The source code can be copied and pasted from here.

var channelMap = [ ["MTVJ", "MTV Jams", "134"], ["GREEN", "Planet Green TV", "201"], ["SHOXw", "Showtime Extreme", "582"], ["TVG", "TVG Horse Racing", "409"], ["HIS-I", "History International", "276"] ]; $context.currentClip.nextItem.propertyList.setPropertyValue("Channel Map", channelMap);

The "Property setup Clip" simply calls the script shown above.

The "Send Clip" sends one message and it uses the "for each" value that its containing clip is being called for. Any clip being repeated by "for each" has access to the value that it's being called for. In this case, the value it's being called for is in itself an array.

The first value on the right is getting the first element of the array, followed by the second and third for the "for each" value in use. To recap, the "Property setup Clip" will run a script that sets a property of the "Send Clip". The "Send Clip" will run once for each property value, which is in itself an array.

When this composition is played, the following Result Details are presented.

Note: The "Send Clip" has an entry without an index because its property was set before it began executing, and therefore, before it was known that it would repeat. Once it came time to execute the clip, it repeated five times, once each per "for each" value.

When the "Property setup Clip" ran it executed the script and was successful.

The Property value of the "Second Clip" got set by the script (only (Array) is shown as above).

Once the "Send Clip" was executed, the three values from the sub-array (the second dimension) within each value of the outer property array were inserted into each of the five runs of "Send Clip".

This example has demonstrated how a script is used to setup a complex grid of values. Another possible use for such a scripted array might be a grid of name and password pairs where the following clip is run for each name/value pair and also has access to both values in that pair.