cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Regular Visitor

Safely access an array by index without outside bounds of array error

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.'.

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Support
Community Support

Re: Safely access an array by index without outside bounds of array error

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.

1.png

 

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.

View solution in original post

Highlighted
Regular Visitor

Re: Safely access an array by index without outside bounds of array error

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.

 

Capture.JPG

 

Capture.JPG

 

 

Cheers

View solution in original post

6 REPLIES 6
Highlighted
Super User
Super User

Re: Safely access an array by index without outside bounds of array error

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

 

 



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Highlighted
Regular Visitor

Re: Safely access an array by index without outside bounds of array error

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

Highlighted
Super User
Super User

Re: Safely access an array by index without outside bounds of array error

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



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Highlighted
Community Support
Community Support

Re: Safely access an array by index without outside bounds of array error

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.

1.png

 

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.

View solution in original post

Highlighted
Regular Visitor

Re: Safely access an array by index without outside bounds of array error

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.

 

Capture.JPG

 

Capture.JPG

 

 

Cheers

View solution in original post

Highlighted
Community Support
Community Support

Re: Safely access an array by index without outside bounds of array error

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.

Helpful resources

Announcements
firstImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

firstImage

Join the new Power Virtual Agents Community!

We are excited to announce the launch of Power Virtual Agents Community. Check it out now!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

Top Solution Authors
Top Kudoed Authors
Users online (8,995)