[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 532
  • Last Modified:

nested loops

i have the below code which contains a nested for loop...

i am writing a sample code which should automatically check for nested loops...

when i give the below code to my application it should say that you had a nested for loop....

how can i do that...


Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
    On Error GoTo Scan_Issue
    Dim p1 As IASLib.IAS_RECORD_1
    Dim p As IAS_RECORD_0
 For Each p In pRoot.Tree.Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
        p.Tree.Parent.Foil_Number.Foil_Number_From_Scan =p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
     For Each p In pRoot.Tree.Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
        p.Tree.Parent.Foil_Number.Foil_Number_From_Scan =p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
     Next  p    
Next  p
   
Exit Sub

Open in new window

0
shragi
Asked:
shragi
  • 6
  • 2
  • 2
  • +1
1 Solution
 
mrjoltcolaCommented:
Proper way is to parse the language, then build a syntax tree (AST) and analyze the AST
Simpler, hack is to just use regular expressions or recursive descent parsing and for each "For Each" you find, try to identify another "For Each" that begins before the last one ends ("Next")
0
 
käµfm³d 👽Commented:
The quick-and-dirty would be to read each line and check for a "For" at the start of it (ignoring whitespace). Count the number of "For"s you receive before getting to a "Next" and if it's greater than one, you have a nested loop. You would need to add conditions for "While" and "Do" if you care about those.

The proper way would be to create a parser which searched through the string looking for loops. A recursive descent parser would probably get you by.
0
 
mrjoltcolaCommented:
Great minds think alike. ;)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Wow. Those posts are eerily similar. Great minds, eh?    = )
0
 
käµfm³d 👽Commented:
Jinx!
0
 
UnifiedISCommented:
If you are reading line by line in order, look for either "For Each" or "Next" within the line.
use an integer variable to indicate if you are expecting a "Next" or a "For Each", if you find the opposite, you have a nested loop.

When you find a for each, add 1, when you encounter a next, subtract 1
If your value exceeds 1, you have a nested for...next loop.
0
 
käµfm³d 👽Commented:
@UnifiedIS

If you are reading line by line in order, look for either "For Each" or "Next" within the line.

So what happens when you receive the following:
Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
    On Error GoTo Scan_Issue
    Dim p1 As IASLib.IAS_RECORD_1
    Dim p As IAS_RECORD_0

    ' I had this enabled, but it was wrong, but I might use it again, so I didn't delete it
    'For Each p In pRoot.Tree.Pages

    p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
    p.Tree.Parent.Foil_Number.Foil_Number_From_Scan = p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
    For Each p In pRoot.Tree.Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
        p.Tree.Parent.Foil_Number.Foil_Number_From_Scan = p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
    Next p

    'Next p
Exit Sub

Open in new window

0
 
UnifiedISCommented:
Look for the single quote at the front of the trimmed line and ignore those lines
0
 
shragiAuthor Commented:
i foudn index of for each and next ... and verified whether index of for each is greater than next ...

but not able to Count the number of "For"s you receive before getting to a "Next" and if it's greater than one
while ((forLoopIndex = TextFromTextArea.indexOf("For Each", forLoopIndex))!= -1)
	{
		forLoopIndex++;
		System.out.println("Search Index for  is:"+forLoopIndex);
		forLoopCounter++;
	
	}
	
	while ((nextIndex = TextFromTextArea.indexOf("Next", nextIndex))!= -1)
	{
		nextIndex++;
		nextCounter++;
		if(forLoopCounter>nextCounter)
		{
			loopDisplay="Code Contains Nested loops";
			out.println(loopDisplay);
			
		}
		else if(forLoopCounter<=nextCounter)
		{
			loopDisplay="Module contains for loop.";
			out.println(loopDisplay);
		}
	
	}

Open in new window

0
 
käµfm³d 👽Commented:
I really think you should invest some time in researching a parser. It is too easy to circumvent (or miss or mistakenly include) various combinations of code.

To demonstrate, how would you handle the following scenarios (all of which encompass valid code):

For Each item As String In Lines : For Each c As Char In item
        Console.WriteLine(c)
    Next
Next

Open in new window


For Each item As String In Lines ' For Each c As Char In item
    Console.WriteLine(item)
Next

Open in new window


For Each item As String In Lines
    Next(item)
Next

...

Sub Next(ByVal item As String)
    Console.WriteLine(item)
End Sub

Open in new window


For Each [Next] As String In Lines
    Console.WriteLine([Next])
Next

Open in new window

0
 
käµfm³d 👽Commented:
Ok. One of those (number 3) isn't valid. Ooops!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now