|Table of Contents||Sections|
Branching is done using the customary if keyword. This keyword marks the beginning of a block (like sub). It must be followed by an expression that must resolve to a value. If the value is false or null, then the expressions in the block are skipped; otherwise they are executed sequentially.
RCL also supports the else keyword with the usual meaning:
Finally, RCL also supports the elsif keyword which can be used to write the example above more succinctly:
This notation is especially useful to chain multiple tests without having to keep indenting the code.
There are three different kinds of loops: the traditional while loop allows repeating a block until a given condition is met. The foreach loop allows iterating over a resource collection or an array of values. Finally the map loop is also an iterator but it returns one or more collection(s) built from the elements of the initial collection.
The while keyword marks the beginning of a block. It must be followed by an expression that resolves to a value. The sub-expressions are executed until the value resolves to false or null:
This example will loop $app_servers_count times and in each iteration will create a server using a given set of arguments and launch it. The + operator applied to hashes returns a hash built from merging the left hand side of the operator into the right hand side. The same operator following a string concatenates the string with the right hand side value (a string or an integer). So the loop body in the example above will override the name used to create the server using a common prefix and the index as suffix.
Foreach loops allow iterating over either a resource collection or an array of values. The syntax is:
As mentioned above, foreach also allows iterating through collections as in:
Note: A cloud workflow always acts on resource collections, the elements returned by foreach are thus collections themselves (albeit made of a single resource).
The map iterator constructs a new collection by iterating over an existing collection and applying a transformation to each element. The full form of the map iterator is fairly flexible and can iterate over a collection or an array of values, and it can construct one or multiple collections or arrays of values. The example below shows it iterating over the @servers collection and producing a $states array of values and @instances collection.
The map iterator follows the same syntax as the foreach operator, but adds a return keyword used to specify which variable(s) or reference(s) from the inner block should be used to build the resulting arrays(s)/collection(s).
The above code creates two collections: the resource collection @instances and an the array $states. The @instances resource collection gets built by appending the content of @instance at the end of each iteration. Similarly, $states gets built by appending the value of $state at the end of each iteration. If a reference or a variable specified after return does not get defined in the execution of an iteration, then nothing is appended to the corresponding resulting collection.
|RCL||Resources||Cloud Workflows & Definitions||Variables||Attributes & Error Handling||► Branching & Looping||Processes||Functions||Operators||Mapping|
© 2006-2014 RightScale, Inc. All rights reserved.
RightScale is a registered trademark of RightScale, Inc. All other products and services may be trademarks or servicemarks of their respective owners.