Skip inside the for each loop

Hi All,

Consider a scenario where  ,I am looping on an array of 100 values.

I got the value which i was looking at the index of 20 . then i need set 2 variables with predefined values and skip rest of the looping in for each.

Could you please let me know if it is possible ? sample code is really appreciated .

Thanks in advance

Raj

  Discussion posts and replies are publicly visible

  • If I understand the question properly, Im assuming that you want to update an array where a particular value has been set.

    If thats the case then you can do something like:

    updatearray(
      ri!myArray,
      wherecontains(
        ri!valueToUpdate,
        ri!myArray
      ),
      ri!myNewValue
    )

    Else, if you are just trying to update at index 20, then you can do:

    updatearray(
      ri!myArray,
      20,
      ri!myNewValue
    )

  • 0
    Certified Lead Developer
    More detail is needed for when you say "then i need set 2 variables with predefined values". What are the "2 variables" you're setting, what "predefined values", and what is their relation to the value you find at index 20 of the array?

    As far as "skipping the rest of the looping" in a!forEach(), as far as i know this isn't possible as it's not the way it works, but basically if you have the a!forEach expression return an empty set ( "{}" ) when a particular iteration is not a match, then only the matching entries will produce any output (effectively "skipping" any of the iterations that you don't need).
  • Hi Benjamin , Thanks for the reply .
    I am not trying to update the array itself . I need to update a cdt or a local variable which is defined in load or with if if the condition defined in expression of for each met and skip the loop. Any other loops will it help ?

    Ex:

    local!a : {"1","2","3","4","5","6","7","8","9","10"}
    local!toset,
    local!sampleJsonObj: "{ 1:null,2:null,3:null, 4:India, 5:australia, 6:null,7:spain,8:null,9:yyyy,0:jjjjj}" /*this array holds json objects with more than 100 */

    For ex: a!forEach(
    items:local!a,
    expression: if ( not (isnull ( index (samplejsonObj, fv!item )) , set the first index value / json object of not null to a local toset or cdt , else skip the next values)
    )

    hope it is clear with pseudo code.

    Thanks
    Rajesh
  • Hi Mike,
    Thanks for the reply

    Please find sample code.
    Ex:

    local!a : {"1","2","3","4","5","6","7","8","9","10"}
    local!toset,
    local!sampleJsonObj: "{ 1:null,2:null,3:null, 4:India, 5:australia, 6:null,7:spain,8:null,9:yyyy,0:jjjjj}" /*this array holds json objects with more than 100 */

    For ex: a!forEach(
    items:local!a,
    expression: if ( not (isnull ( index (samplejsonObj, fv!item )) , set the first index value / json object of not null to a local toset or cdt , else skip the next values)
    )

    hope it is clear with pseudo code.

    Thanks
    Rajesh
  • HI Raj,

    1-> one statement is not clear here : "hen i need set 2 variables with predefined values " .

    2-> if you want to skip the loop or simply i say you just want to terminate the loop like we do in java/c# with the help "break" keyword that is not possible in Appian loop till now.

    3-> And if the requirement is like you want to replace some value( the index which you don't know) in array but you don't know the index then you can use wherecontains function , it will return you the index for the value which you are searching and now you have the index value so you can do further process.


    Regards
    Abhay Giri
  • +1
    Certified Lead Developer
    in reply to rajeshr501

    Well I'm not altogether clear what's supposed to be inside your "sampleJsonObj" object since it's currently structured more like a dictionary instead of an array, and not valid JSON text.  But i'll prepare a code example where I assume it's a dictionary (already converted back from JSON) with each entry in its own property named "index1"..."index10".  The same sample can be applied with simple changes in the case that you're using an array here, the rest should work about the same.

    So it sounds like you're asking to return the first(?) index of a non-null entry within the JSON array.  For that first we can simply get the indices of all non-null entries like this:

    with(
      local!a : {"1","2","3","4","5","6","7","8","9","10"},
      local!toset,
      
      local!sampleJsonObj: {
        index1: null,
        index2: null,
        index3: null, 
        index4: "India", 
        index5: "australia",
        index6: null,
        index7: "spain",
        index8: null,
        index9: "yyyy",
        index10: "jjjjj"
      }, /*this array holds json objects with more than 100 */
    
      
      /* get all indices */
      a!forEach(
        items: local!a,
        expression: if( 
          not( isnull( property(local!sampleJsonObj, "index" & fv!item ))),
          tointeger(fv!item),
          {}
        )
      )
    )

     

    Then if you only want the first such index, my suggestion is you simply use the same code and just pull the first value from the resulting array of indices:

    with(
      local!a : {"1","2","3","4","5","6","7","8","9","10"},
      /*local!toset,*/  /* now setting this below */
      
      local!sampleJsonObj: {
        index1: null,
        index2: null,
        index3: null, 
        index4: "India", 
        index5: "australia",
        index6: null,
        index7: "spain",
        index8: null,
        index9: "yyyy",
        index10: "jjjjj"
      }, /*this array holds json objects with more than 100 */
    
      
      /* get all indices */
      local!allIndices: a!forEach(
        items: local!a,
        expression: if( 
          not( isnull( property(local!sampleJsonObj, "index" & fv!item ))),
          tointeger(fv!item),
          {}
        )
      ),
      
      /* capture the first index into local!toSet */
      local!toSet: index(
        local!allIndices,
        1,
        {}
      ),
      
      /* reader-friendly output: */
      "The first non-null index is: " & local!toSet & "." & char(10) &
      "The associated value is: " & property(local!sampleJsonObj, "index" & tostring(local!toSet), "(n/a)")
    )

  • Hi Mike,
    Thanks , with your code .
    It helped me to get an approach
    Thanks again
    Raj