Skip to main content
fernandob9308
Participating Frequently
Jun 14, 2024 2:49 PM UTC
Question

Nested For Each

Hello all, 

I have a question that I think it should be easy but I cannot figure it out, and also I cannot see any response in this forum. 

If I have a a!forEach within another a!forEach, is there a way of refering to the items of the outer loop, within the first loop? 

Example to understand with non real data: 

I have two lists:

local!text: {"1111 - GREAT", "1000 - BAD", "5600 - MEDIUM"}

local!words: {"GREAT", "MEDIUM"}

as you can see, "text" is a list of text, and each string contains a code a "-" and a word. I want to create a nested loop that returns a list with the codes only when the words contained in the list "words" are in the list a.
In this particular example: {"GREAT", "MEDIUM"} because "GREAT" is included in "1000 - GREAT" and "MEDIUM" is included in "5600 - MEDIUM".

I woud like to iterate though every item of list "words", check if it is in any item of list "text", and then, moved to the next item of list "words". something like.

a!forEach(

items: local!words,

expression:

a!forEach(

items: local!text,

expression:

if(contains( fv!itemintext,fv!iteminwords),
fv!itemintext[fv!index],
null
)
)
)



in other languajes it will be really easy because for every loop you could assign an identifier, as for example in python you would be able to do something like:

for w in words:

for t in text:

I hope you can help me with this one

4 replies

Brainy
Jun 14, 2024 3:22 PM UTC

I think we can achieve this without a nested loop. See if the following code and output helps:

a!localVariables(
  local!text: {
    "1111 - GREAT",
    "1000 - BAD",
    "5600 - MEDIUM",
    "8600 - MEDIUM",
    "3111 - GREAT",
    "5111 - GREAT"
  },
  local!words: { "GREAT", "MEDIUM" },
  local!data: a!forEach(
    items: local!text,
    expression: a!map(
      word: split(fv!item, " - ")[2],
      data: split(fv!item, " - ")[1]
    )
  ),
  a!forEach(
    items: local!words,
    expression: a!map(
      word: fv!item,
      data: index(
        index(
          local!data,
          wherecontains(fv!item, index(local!data, "word"))
        ),
        "data"
      )
    )
  )
)

fernandob9308
Participating Frequently
Jun 14, 2024 3:26 PM UTC

oh! did not even considered this approach! Many many thanks, Yogi! I will explore it! 
Thanks again!!

zukiswal
New Participant
Jun 15, 2024 6:20 PM UTC

using a nested loop, the below code should resolve your question

a!localVariables(
  local!text: {
    "1111 - GREAT",
    "1000 - BAD",
    "5600 - MEDIUM"
  },
  local!words: { "GREAT", "MEDIUM" },
  local!output: a!forEach(
    items: local!words,
    expression: if(
      contains(
        a!forEach(
          items: local!text,
          expression: split(fv!item, " - ")[2]
        ),
        fv!item
      ),
      fv!item,
      null
    )
  ),
  remove(local!output, wherecontains(null, local!output))
)

fernandob9308
Participating Frequently
Jun 17, 2024 6:06 AM UTC

Hello Zukiswa,

Thank you very much for your answer! It looks direct and simple! Great.

Do you think I could also use this approach if I later want to create another list of strings with the codes, e.g. {"1111","5600"}?

Thanks in advance!