Xplan Xmerge Coding – Enumerate

Xplan Quick Xplain

Enumerate: a better
built-in counter

About enumerate

Enumerate! Enumerate!

Despite sounding similar to a certain Dalek catch cry, enumerate is actually a small but useful item that can you improve and reduce your coding.

Enumerate

Verb
To establish the number of.   “6,079 residents were enumerated in 241 establishments”

Enumerate with our xplan coding is all about counting each item in a loop as it progresses.  It essentially allows us to generate a built in counter that automatically progresses with each item in the iteration (eg when you are looping over a group, for each item found a respective number is bound to that item). 

How does it work?

enumerate(insert group or item to be looped)

An example of it in practice:

<:for counter, item in enumerate($client.asset):>
<:=counter:> - <:=item.desc:>
<:end:>

# 0 – Family home
# 1 – Work car
# 2 – Fun car
# 3 – Cash savings

You can see as the loop progresses so too does our counter (enumerator), automatically. This can be used to replace coding structures that need a counter, as enumerate is now doing that for you without the need for additional dependencies eg variables.

Important notes:

  • The first item in the loop will always be the enumerated number.  In this example I have called it ‘counter’ (you can call it whatever you want).
  • When looping like this place a comma after the enumerator.
  • The second item is your standard iterator you always use when looping, in this case we have called it the very common ‘item’
  • The group or object you want to loop over goes inside the enumerate ()

Practical Example (coding group elements in one sentence)

In a previous article we looked at how to code group elements in a single sentence.  The coding we used was:

<:let i = 0:><:let x = len($client.dependent):><:if x > 0:><:for item in $client.dependent:><:if len($client.dependent) > 1:><:if i == x - 1:> and <:else:><:if i > 0:>, <:end:><:end:><:end:><:=item.dep_name:><:let i=i+1:><:end:><:end:>

# Jimmy, Jane and Joffery

You can see that we created a counter variable called ‘i’ to help us determine the elements iterated over in the group, relative to the total number of items so that we could assign the ‘and’ or ‘,’ correctly.

Using enumeration we can do away with the ‘i’ counting variable all together:

<:let x = len($client.dependent):><:if x > 0:><:for counter, item in enumerate($client.dependent):><:if len($client.dependent) > 1:><:if counter == x - 1:> and <:else:><:if counter > 0:>, <:end:><:end:><:end:><:=item.dep_name:><:end:><:end:>

# Jimmy, Jane and Joffery

Using enumerate:

  • Get an automatic built in counter that adds itself with each iteration
  • Therefore we don’t need to create another unnecessary variable or dependency
  • We are using a built-in function of the system.  This is always preferable as is using less elements where practical.
  • Keep in mind in the example it doesn’t appear we’ve saved much code but that’s because we’ve replaced a poorly named variable ‘i’ with the much longer ‘counter’.  if you used ‘num’ or something similar the coding would still be more readable and shorter.
Matthew Townsend
Xplan Consultant and Developer | Wealth Management Technologist at Create Something
Matthew is an experienced and innovative Xplan consultant and developer, having worked on and developed some of the largest advice projects in the industry. Passionate about building great experiences in xplan that enable businesses and clients to get the most out of this powerful software.

Matthew Townsend

Matthew is an experienced and innovative Xplan consultant and developer, having worked on and developed some of the largest advice projects in the industry. Passionate about building great experiences in xplan that enable businesses and clients to get the most out of this powerful software.

This Post Has 4 Comments
  1. Every weekend i used to visit this site, for the reason that this this site conations really pleasant funny
    data too.

  2. I havenˇt checked in here for some time as I thought it was getting boring, but the last several posts are great quality so I guess Iˇll add you back to my everyday bloglist.

Comments are closed.