Array indexing within Set Variable module

I am trying to convert a month represented by a number to a written month name, for example going from 9 to September. I have three sequential Set Variable modules:

Variable 1 - previousMonthNumber
Uses the built-in date functions to take the current date, add -1 months to get the previous month, and formats it to just be the number month value:
{{formatDate(addMonths(now; -1); "M")}}

Resulting variable value is: 9


Variable 2 - monthsArr
Uses the built-in array functions to create a blank array, and populate it with the written months:
{{add(emptyarray; "January"; "February"; "March"; "April"; "May"; "June"; "July"; "August"; "September"; "October"; "November"; "December")}}

Resulting variable value is: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

Variable 3 - previousMonthRead
Should use the previous two variables to get the previous month as a written word. I thought it would be quite simple to do this using array indexing, as I would were I coding. I tried this:

However, the output of the above is:
"previousMonthRead": "January, February, March, April, May, June, July, August, September, October, November, December[9]"

I have tried varying syntax changes such as monthsArr.previousMonthNumber, but nothing seems to work and the result is always some variation of the full array with the previousMonthNumber just added onto the end of it.

So, my question is, if not with standard indexing syntax, how do I do array indexing within the variable value definition box? I feel like I must be missing something really obvious because I thought this would be very simple to do.

Iā€™d be grateful for any help!

1 Like

Not sure why youā€™re making it this hard. The mask token for the full month name is MMMM

{{formatDate(addMonths(now; -1); "MMMM")}}

will do this without array manipulation. It does seem that make is casting your number to a string and that array index approach breaks.

2 Likes

Did not know you could get the full month name with MMMM, thank you very much for that! I had not thought to look at the documentation for date formatting as I was focusing on the array. Appreciate the pointer.

Nonetheless, though, I would like to know how array indexing works. Even just trying monthsArr[9] (using 9 rather than the variable) fails.

1 Like

This is a tricky thing to do as you have to put the number directly into the formatted array syntax. Canā€™t type it out manually. Take a snapshot of how you have the numeric index formatted.

As for the rest of it I encourage you to read all the docs. Youā€™re going to start banging your head against the wall if you donā€™t thoroughly digest what make is. Start with this thread.

1 Like

I appreciate your comments and Iā€™ll be sure to peruse the documentation. I have been using Make/Integromat (and Zapier) for a few years now, and this is the first time Iā€™ve really become stuck on something. Some additional context on this particular use case - Iā€™m retrieving all rows within a Google Sheet that match a Google Charts query, turning that range into a .csv and attaching it to an email to be sent automatically on the first day of every month.

For the sake of clarity, I thought Iā€™d just show you everything involvedā€¦

This is how I define the array:

I can confirm that works by inspecting the output:

This is how Iā€™m doing the indexing with a number:

The result of that is the array is now a collection, as it contains my list of strings and an integer:

The array reference is incorrect. The number has to go into the colour token between the [ and ].

As far as I can tell, itā€™s not possible to edit that purple token or add a number between the square brackets. Am I missing something?

I seem to have figured it out, my badā€¦

Rather than clicking the monthsArr[] token to insert the full variable reference, expanding the array and clicking the [1] token inserted the square bracket tokens, and I was then able to change the index number, and successfully extract a value from the array.

Screenshot 2022-10-26 at 13.04.46

Not hugely intuitive, and it does seem odd you canā€™t just type those square brackets manually or insert them from the ā€˜Functions for working with arraysā€™ box, but it works! Thanks again for the help.

1 Like

Not at all but yes. Also you can copy the whole expression and edit it in a text editor. All expressions are just text and much easier to edit in a regular text editor and then paste back in. Try it!

1 Like

That is a lot easier indeed. Well Iā€™ve learnt a few things doing this, so all good experience! Thanks for your time today, Alex, much appreciated. Consider me unstuck!

2 Likes

Using get() is even easier and it is probably easier to access dynamic array indices.

3 Likes

Youā€™re right, it is! Not the most obvious solution though. Perhaps itā€™s just me but Iā€™d have thought that anyone coming from a coding background would anticipate arrays can be indexed with [ ]. I realise Make is a ā€˜no/minimum-codeā€™ platform, but it does seem that something rather simple has been made overly complex for the sake of not typing any code syntax.

It would be nice if the Array functions plainly stated how to index an array, given itā€™s a fundamental functionality of arrays.

But hey-ho, got there eventually :man_shrugging:

The documentation needs a ton of work. I hear itā€™s coming. Itā€™s the only really weak part of make to be honest.

3 Likes