Skip to main content

Adding conditional variation

So far, your script only covers the possibility that certain computed values (e.g. the change in a jurisdiction's population) will increase.

While it's unlikely that a jurisdiction's population would decrease or stay exactly the same, you might want your script to cover these possibilities just in case. Fortunately, Studio makes it easy to build conditional statements that cover multiple possibilities and then selects among them depending on the input data.

You will now add two conditional statements to your script.

  1. Select the first call to the percentageChange function (don't include the % sign) and copy it.

  2. Delete the first up in your script and replace it with up | down | — that is, the two words up and down separated by a vertical bar and ending with a vertical bar. Your script should look like this:

    ACVT1 (new).png
  3. Select the up | down | snippet, and click the Insert Conditional button.

    ACVT2 (new).png

    This opens the Conditional Builder dialog:

    ACVT3 (new).png

    A conditional consists of a set of cases. Each case has a condition that holds between two parameters and a result that will follow if the condition is met. There is also a default result that will follow if none of the specified conditions are met.

    You’ll notice that the RESULT fields for Case 1 and Case 2 are already filled in. That’s because we called the Insert Conditional operation on our selected up | down | text. If we had provided some text after the final vertical bar, that text would have appeared in the DEFAULT RESULT field (at the bottom of the dialog).

    Now all you have to do is add the conditions associated with each result.

  4. Click in Case 1’s PARAMETER A field. This produces a drop-down list of the available variables, but we have already copied the data we want to test here.

  5. Paste the call to the copied percentageChange function. It should appear as [[percentageChange(StatePop2016,StatePop2010,1)]].

  6. Click in Case 1’s CONDITION field to get a drop-down list of conditions, and select Is Greater Than.

  7. Click in Case 1’s PARAMETER B field, and type 0 (zero).

  8. Repeat steps 4 to 7 for Case 2, except select Is Less Than in the CONDITION field.

    ACVT4 (new).png
  9. Scroll down past the fields for Case 3, and click in the DEFAULT RESULT field.

  10. Type "a change of". This covers the remaining possibility that the percentage change is equal to zero.

    ACVT5 (new).png

    Tip

    The Conditional Builder has its own local preview mode at the bottom of the dialog. Here you can navigate through your data rows to check that the values being computed for each row make sense.

  11. Click Build to insert the conditional into your script. Once done, your script should look like this:

    ACVT6 (new).png

    Notice that the conditional statement is underlined in purple.

    Tip

    Here you’ve used a conditional statement to select from among alternative words and phrases, but the same mechanism can be used to select among sentences, or even entire paragraphs.

    There is one more thing to do: if the percentage change is negative, we’ll generate the string down –N%, where N is the percentage change. This would be a problem because the number should not be negative, because the preposition down already captures the direction. This is easily fixed using the abs function, which ensures that the absolute value of the number is displayed.

  12. Select the first call to the percentageChange function, then click Insert Function > math > abs. This opens the Function Builder dialog.

    ACVT7 (new).png
  13. Click Save. Your script should now look like this:

    ACVT8 (new).png
  14. Repeat steps 1-13 for the second call to the percentageChange function. Once done, your script should look like this:

    ACVT9 (new).png

    The underlying ATL for the second sentence should look like this:

    The capital city, [[Capital]], has a population of [[CapitalPop2016]], which is [[if(percentageChange(CapitalPop2016,CapitalPop2010,1)>0){up}elseif(percentageChange(CapitalPop2016,CapitalPop2010,1)<0){down}else{a change of}]] [[abs(percentageChange(CapitalPop2016,CapitalPop2010,1))]]% from 2010's count of [[CapitalPop2010]]. Today the capital is home to [[CapitalPopPercent]]% of the state's population.

  15. Click Preview. The output text for Row 1 should be:

    New South Wales has a land mass of 800,641 square kilometers and a population in 2016 of 7,618,200, up 5.2% from 2010's figure of 7,238,800. The capital city, Sydney, has a population of 4,526,479, which is up 8.2% from 2010's count of 4,183,471. Today the capital is home to 59.4% of the state's population.

    Oh, there is one more fix to make: the final sentence talks about the state’s population, but Australia is made up of jurisdictions of two types; each jurisdiction is either a state or a territory, and this information is provided in the Type column of the data table.

  16. Select the word state in your script (don't include the following apostrophe in the selection).

  17. Click Insert Variable and select Type from the drop-down.

    Your script should now look like this:

    ACVT10 (new).png
  18. Click Preview. You’ll notice that the contents of the Type variable are words with initial capitals (either State or Territory), but you need all lowercase words here. You can easily fix this using the lower function.

  19. Select the reference to the Type variable, then click Insert Function > text > lower. This opens the Function Builder dialog.

  20. No changes are required, so just click Insert to insert the function call into the script.

    Your script should now look like this:

    ACVT11 (new).png
  21. Preview your text to make sure everything is correct.

    The output text for Row 1 should be:

    New South Wales has a land mass of 800,641 square kilometers and a population in 2016 of 7,618,200, up 5.2% from 2010's figure of 7,238,800. The capital city, Sydney, has a population of 4,526,479, which is up 8.2% from 2010's count of 4,183,471. Today the capital is home to 59.4% of the state's population.

So far, you've worked with the Main script only. Next, you'll learn how to work with multiple scripts.

Using multiple scripts