cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JohnMaxwell
Level: Powered On

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
Community Support Team
Community Support Team

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

JohnMaxwell
Level: Powered On

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
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!



JohnMaxwell
Level: Powered On

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

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!



Community Support Team
Community Support Team

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

JohnMaxwell
Level: Powered On

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

Community Support Team
Community Support Team

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

Microsoft Business Applications Virtual Launch Event

Watch the event on demand for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020.

thirdimage

Flow Community User Group Member Badge

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

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 412 members 2,843 guests
Please welcome our newest community members: