Skip to main content
sivakrishnam0002
May 5, 2023
Question

How to loop first 100 rows for array using foreach?

  • May 5, 2023
  • 6 replies
  • 0 views

I have 120 rows in array but i want to loop only first 100 rows using foreach function.

Please advise.

    6 replies

    May 5, 2023

    a!localVariables(
      local!data:enumerate(120),
      a!forEach(
        items: index(local!data,enumerate(100)+1,{}),
        expression: fv!item
      )
    )

    stefanhelzle0001
    May 5, 2023

    This is also covered in the Appian documentation.

    docs.appian.com/.../parts-of-an-expression.html

    mikes0011
    Brainy
    May 5, 2023

    I usually use the two functions that can grab a piece of the array depending on length (with a bit of effort), rDrop() and lDrop().  These both drop a part of the array from the "left end" or "right end" depending on the value passed in.  It would be more straightforward if Appian had an equivalent function that lets you GET the left-or-right-most X items from an array, which would essentially be the inverse (or contrapositive?) of these two functions, and i'm unclear why they don't exist.  However you can accomplish it with just a bit of effort.

    a!localVariables(
      local!array: enumerate(120),
      local!originalLength: length(local!array),
      local!lengthWanted: 100,
      
      local!modifiedArray: rdrop(local!array, local!originalLength - local!lengthWanted),
      
      a!forEach(
        local!modifiedArray,
        fv!item
      )
    )

    stefanhelzle0001
    May 6, 2023

    [mention:b45a4f6a20b14a008e4e0ec732a8bf98:e9ed411860ed4f2ba0265705b8793d05], please help me. What is this foreach() doing?

    mikes0011
    Brainy
    May 8, 2023

    [mention:126a676c85024855948336691b0a7b92:e9ed411860ed4f2ba0265705b8793d05] - in this case it's in place for demonstration.  OP's question was specifically about how they'd loop through "the first X rows of a Y-length array using a!forEach()", so my example here prepares a truncated array and then feeds it into a!forEach(), which would then contain arbitrary extra functionality, not seen here.

    Remember, having code that's readable, intelligible and maintainable is 100x more important than having code that's as-short-as-humanly-possible!