Nested forach caused a infinite loop error

0 votes
asked Feb 18, 2022 in Bug by c6supper (120 points)
I met problem when dealing with nested foreach,  I found the problem is related to the number of loops, I paste the example as follows.

http://www.plantuml.com/plantuml/uml/rT4xQeP050NW_gxYUB4A4ZIFHWYuXwGBAQvwHOdpG6r4t7iS4WFAxmB-wfmPlcdE5ECamsGwMnln5CMDStZ30gwSyM9dpi7ltxFLYHEm6j-xMNLavFWaO64hHiE9fUyg3MYKNeRmC4jJhCbzgDAL1_EQhlYYbUW17wtYznwk0IXgtA1IjOZL-0oTHLZjyNyR0WxawIOveUSBT68lrqnjlKawzhtjMMpvzza_

@startmindmap
!$foo = { "company": "Skynet", "employees" : [
  {"name" : "alice", "salary": 100 },
  {"name" : "alice", "salary": 100 },
  {"name" : "alice", "salary": 100 },
  {"name" : "alice", "salary": 100 },
  {"name" : "alice", "salary": 100 },
  {"name" : "bob", "salary": 50} ]
}

* The salary of  
!foreach $emp1 in $foo.employees
  !foreach $emp2 in $foo.employees
    !foreach $emp3 in $foo.employees
      !foreach $emp4 in $foo.employees
      !endfor
    !endfor
  !endfor
!endfor
@endmindmap
commented Feb 18, 2022 by c6supper (120 points)
I found the problem may be related to the following code snippet,
Can we use a jar variable instead of hard coded for countJump?

public void jumpToCodePosition(CodePosition newPosition) throws EaterException {
        this.countJump++;
        if (this.countJump > 999) {
            throw EaterException.unlocated("Infinite loop?");
        }
        final Position pos = (Position) newPosition;
        this.current = pos.pos;

    }

1 Answer

0 votes
answered Feb 22, 2022 by kirchsth (5,080 points)

Do you really want 6*6*6*6 (=1296) entries in a diagramm?

You can split the for calls in multiple procedures (e.g. add a $subSubSubLoop() procedure)

@startmindmap
!$foo = { "company": "Skynet", "employees" : [
  {"name" : "alice1", "salary": 100 },
  {"name" : "alice2", "salary": 100 },
  {"name" : "alice3", "salary": 100 },
  {"name" : "alice4", "salary": 100 },
  {"name" : "alice5", "salary": 100 },
  {"name" : "bob6", "salary": 50} ]
}

!procedure $subSubSubLoop($employees)
  !foreach $emp4 in $foo.employees
***** "4"+$emp4.name
  !endfor
!endprocedure


* The salary of  
!foreach $emp1 in $foo.employees
** "1"+$emp1.name
  !foreach $emp2 in $foo.employees
*** "2"+$emp2.name
    !foreach $emp3 in $foo.employees
**** "3"+$emp3.name

' move foreach in a procedure
'      !foreach $emp4 in $foo.employees
'***** $emp4.name
'      !endfor

       $subSubSubLoop($foo.employees)
    !endfor
  !endfor
!endfor
@endmindmap

:
 

commented Feb 25, 2022 by c6supper (120 points)

Hi kirchsth 

That's just an example, actually I was preprocessing the data from a large json file, while include nested array.

I suppose the hard coded limitation should be treated more nicely.

...