Hi,
I have an array variable that's dynamically populated with strings at runtime (see below). The length of the array is unknown.
When accessing the array I'd like to check that the array element exists/ is within the bounds of the array before accessing and using it.
How do I safely access an array?
Here's an example of what I've tried already:
if(greaterOrEquals(length(variables('Categories')), 4),variables('Categories')[3], null)
The array has the following elements: "Unified Interface", "deprecated", "legacy web client"
I would expect the following:
length to return 3
greaterOrEquals statement to result in false
The first return value isn't evaluated and the second return value is used.
However this is the error I'm currently receiving:
InvalidTemplate. Unable to process template language expressions in action '~' inputs at line '1' and column '2725': 'The template language expression 'if(greaterOrEquals(length(variables('Categories')), 4),variables('Categories')[3], null)' cannot be evaluated because array index '3' is outside bounds (0, 2) of array. Please see https://aka.ms/logicexpressions for usage details.'.
Hi @JohnMaxwell ,
Have your problem be solved? If yes, please go ahead and mark the post as solved by clicking “Accept as Solution” so that this thread will be marked for other users to easily identify!
Best regards,
Alice
Community Support Team _ Alice Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @v-alzhan-msft,
The if expression would check the true value even though the expression has false result, so you couldn't use the if expression for you scenes.
That's a shame, I take it there's no way to achieve this solely at the logical expression level?
I got around my issue by using Do Until the array length equaled 20 to fill the rest of the array with empty strings "".
This meant I could access the first 20 array elements (which was all I could use in the Yammer Post message action) via index without worrying about a length check.
Cheers
Hi @JohnMaxwell ,
The if expression would check the true value even though the expression has false result, so you couldn't use the if expression for you scenes.
You could consider using the Condition or Switch on to achieve your needs.
Best regards,
Alice
Community Support Team _ Alice Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @JohnMaxwell ,
Thanks for your quick reply.
I have tested this and my array has 4 values
"Unified Interface", "deprecated", "legacy web client", "Flow"
So the length returned was 4
When I combined with variables('Categories')[3] is null then it did returned false which was expected.
So my question is are these array values are generated dynamically or always you can expect 4 elements.
I would suggest you to check how these arrays are constructed.
If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.
Thanks
Hi @abm,
Thanks for replying but that's what I was expecting and why I'm confused as to why the following if statement is still attempting to access the array despite the greaterOrEquals returning false with the length 3 array.
if(greaterOrEquals(length(variables('Categories')), 4),variables('Categories')[3], null)
greaterOrEquals(length(variables('Categories')), 4)
If for example the array has the following elements then the if statement would work and the array element "Flow" would be accessed successfully, but I need to handle any length array without it breaking.
"Unified Interface", "deprecated", "legacy web client", "Flow"
Cheers
Hi @JohnMaxwell ,
The array index starts from zero.
So in your array Categories are : "Unified Interface", "deprecated", "legacy web client"
The length of the array will return 3. But remember the array index starts with zero.
variables('Categories')[0] returns "Unitifed Interface"
variables('Categories')[1] returns "deprecated"
variables('Categories')[2] returns "legacy web client"
If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.
Thanks
stampcoin
81
Michael E. Gernaey
70
Super User 2025 Season 1
David_MA
48
Super User 2025 Season 1