473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how can I exit from a recursive subroutine?

I am very new to programming and VB.net, and I wrote the code below,
but it seem to go on an infinite loop. Please tell me what I am doing
wrong.

Private Sub search(ByVal indexstart)
Pos1 = LineNoSpace.IndexOf("<!--", indexstart) 'checks for the
begining of the comment
While Pos1 = -1 And Not (line Is Nothing) ' keep checking till
end of file
nextline()
Pos1 = LineNoSpace.IndexOf("<!--", indexstart)
End While
If Pos1 <> -1 Then ' first comment was found, continue
Pos2 = LineNoSpace.IndexOf("#includevirtual", Pos1) 'check
if the starting comment contains #include virtual
While Pos2 = -1 And Not (line Is Nothing) 'if not found in
the same line check the next line till found or end reached
nextline()
Pos2 = LineNoSpace.IndexOf("#includevirtual",
indexstart)
End While
If Pos2 <> -1 Then 'if virtual
include is found, check for the first double quotes
Pos3 = LineNoSpace.IndexOf("""", Pos2) 'in same
line
While Pos3 = -1 And Not (line Is Nothing) 'keep
checking for the first double quotes in next line to end of file
nextline()
Pos3 = LineNoSpace.IndexOf("""", indexstart)
End While
If Pos3 <> -1 Then 'if first double quotes are found
then
Pos4 = LineNoSpace.IndexOf("""", Pos3 + 1) ' check
for second double quotes in the same line
If Pos4 <> -1 Then ' if second double quotes is
found in the same line continue
OriginalLine = Mid(LineNoSpace, Pos3 + 1, Pos4
- Pos3 + 2) & "in line: " & linenumber 'creating a temporary message
before moving to the next line
CheckCommentClose() 'check if there is a
problem with the closing of the comment tag, display problem if there
is
If Pos4 <> 0 Then 'if there was no problem
with the closing comment then
While LineNoSpace.IndexOf("<!--", Pos4) <>
-1 'as long as there is a new comment tag in the same line check for
the virtual include
ListBox1.Items.Add(filename & "
includes: " & Mid(LineNoSpace, Pos3 + 1, Pos4 - Pos3 + 1) & " - in
line: " & linenumber) 'list the current virtual include found
counter = counter + 1 'count of
virtual includes found
search(Pos3) 'start search again from
the position right before the new comment open tag(RECURSION)
End While
ListBox1.Items.Add(filename & " includes: "
& Mid(LineNoSpace, Pos3 + 1, Pos4 - Pos3 + 1) & " - in line: " &
linenumber)
counter = counter + 1 'count of
virtual includes found
nextline()
Else 'else there was a problem with the comment
closing tag in the next line and beyond
ListBox1.Items.Add(filename & " - includes:
" & OriginalLine)
counter = counter + 1
nextline()
End If
Else 'if the second double quotes is not found
in the same line, display error and continue
ListBox1.Items.Add("There is an error in line:
" & linenumber & " - missing double quotes -")
counter = counter + 1 'count of virtual
includes found
nextline()
End If
End If
End If
End If
indexstart = 0
Pos1 = -1
Pos2 = -1
Pos3 = -1
Pos4 = -1
End Sub

Apr 2 '06 #1
3 2777
I didn't look in detail at your code but you exit a recursive routine when
you do a Return or Exit Sub without calling the Function or Sub before, i.e.,
see some code below that I think will work. Notice if I>=10, the DoSomething
does not call itself again. Be careful with recursive routines as you can
get an out-of-Stack error very easily.

Private Sub DoSomething( )
Static i as integer
i +=1
if i <10 then DoSomething else exit sub
end sub

--
Dennis in Houston
"Ameen" wrote:
I am very new to programming and VB.net, and I wrote the code below,
but it seem to go on an infinite loop. Please tell me what I am doing
wrong.

Private Sub search(ByVal indexstart)
Pos1 = LineNoSpace.IndexOf("<!--", indexstart) 'checks for the
begining of the comment
While Pos1 = -1 And Not (line Is Nothing) ' keep checking till
end of file
nextline()
Pos1 = LineNoSpace.IndexOf("<!--", indexstart)
End While
If Pos1 <> -1 Then ' first comment was found, continue
Pos2 = LineNoSpace.IndexOf("#includevirtual", Pos1) 'check
if the starting comment contains #include virtual
While Pos2 = -1 And Not (line Is Nothing) 'if not found in
the same line check the next line till found or end reached
nextline()
Pos2 = LineNoSpace.IndexOf("#includevirtual",
indexstart)
End While
If Pos2 <> -1 Then 'if virtual
include is found, check for the first double quotes
Pos3 = LineNoSpace.IndexOf("""", Pos2) 'in same
line
While Pos3 = -1 And Not (line Is Nothing) 'keep
checking for the first double quotes in next line to end of file
nextline()
Pos3 = LineNoSpace.IndexOf("""", indexstart)
End While
If Pos3 <> -1 Then 'if first double quotes are found
then
Pos4 = LineNoSpace.IndexOf("""", Pos3 + 1) ' check
for second double quotes in the same line
If Pos4 <> -1 Then ' if second double quotes is
found in the same line continue
OriginalLine = Mid(LineNoSpace, Pos3 + 1, Pos4
- Pos3 + 2) & "in line: " & linenumber 'creating a temporary message
before moving to the next line
CheckCommentClose() 'check if there is a
problem with the closing of the comment tag, display problem if there
is
If Pos4 <> 0 Then 'if there was no problem
with the closing comment then
While LineNoSpace.IndexOf("<!--", Pos4) <>
-1 'as long as there is a new comment tag in the same line check for
the virtual include
ListBox1.Items.Add(filename & "
includes: " & Mid(LineNoSpace, Pos3 + 1, Pos4 - Pos3 + 1) & " - in
line: " & linenumber) 'list the current virtual include found
counter = counter + 1 'count of
virtual includes found
search(Pos3) 'start search again from
the position right before the new comment open tag(RECURSION)
End While
ListBox1.Items.Add(filename & " includes: "
& Mid(LineNoSpace, Pos3 + 1, Pos4 - Pos3 + 1) & " - in line: " &
linenumber)
counter = counter + 1 'count of
virtual includes found
nextline()
Else 'else there was a problem with the comment
closing tag in the next line and beyond
ListBox1.Items.Add(filename & " - includes:
" & OriginalLine)
counter = counter + 1
nextline()
End If
Else 'if the second double quotes is not found
in the same line, display error and continue
ListBox1.Items.Add("There is an error in line:
" & linenumber & " - missing double quotes -")
counter = counter + 1 'count of virtual
includes found
nextline()
End If
End If
End If
End If
indexstart = 0
Pos1 = -1
Pos2 = -1
Pos3 = -1
Pos4 = -1
End Sub

Apr 2 '06 #2
Ameen,

If your search on a string is so complex, than there is for that Regex

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

I hope this helps a little bit?

Cor
"Ameen" <Am*****@gmail.com> schreef in bericht
news:11**********************@i40g2000cwc.googlegr oups.com...
I am very new to programming and VB.net, and I wrote the code below,
but it seem to go on an infinite loop. Please tell me what I am doing
wrong.

Private Sub search(ByVal indexstart)
Pos1 = LineNoSpace.IndexOf("<!--", indexstart) 'checks for the
begining of the comment
While Pos1 = -1 And Not (line Is Nothing) ' keep checking till
end of file
nextline()
Pos1 = LineNoSpace.IndexOf("<!--", indexstart)
End While
If Pos1 <> -1 Then ' first comment was found, continue
Pos2 = LineNoSpace.IndexOf("#includevirtual", Pos1) 'check
if the starting comment contains #include virtual
While Pos2 = -1 And Not (line Is Nothing) 'if not found in
the same line check the next line till found or end reached
nextline()
Pos2 = LineNoSpace.IndexOf("#includevirtual",
indexstart)
End While
If Pos2 <> -1 Then 'if virtual
include is found, check for the first double quotes
Pos3 = LineNoSpace.IndexOf("""", Pos2) 'in same
line
While Pos3 = -1 And Not (line Is Nothing) 'keep
checking for the first double quotes in next line to end of file
nextline()
Pos3 = LineNoSpace.IndexOf("""", indexstart)
End While
If Pos3 <> -1 Then 'if first double quotes are found
then
Pos4 = LineNoSpace.IndexOf("""", Pos3 + 1) ' check
for second double quotes in the same line
If Pos4 <> -1 Then ' if second double quotes is
found in the same line continue
OriginalLine = Mid(LineNoSpace, Pos3 + 1, Pos4
- Pos3 + 2) & "in line: " & linenumber 'creating a temporary message
before moving to the next line
CheckCommentClose() 'check if there is a
problem with the closing of the comment tag, display problem if there
is
If Pos4 <> 0 Then 'if there was no problem
with the closing comment then
While LineNoSpace.IndexOf("<!--", Pos4) <>
-1 'as long as there is a new comment tag in the same line check for
the virtual include
ListBox1.Items.Add(filename & "
includes: " & Mid(LineNoSpace, Pos3 + 1, Pos4 - Pos3 + 1) & " - in
line: " & linenumber) 'list the current virtual include found
counter = counter + 1 'count of
virtual includes found
search(Pos3) 'start search again from
the position right before the new comment open tag(RECURSION)
End While
ListBox1.Items.Add(filename & " includes: "
& Mid(LineNoSpace, Pos3 + 1, Pos4 - Pos3 + 1) & " - in line: " &
linenumber)
counter = counter + 1 'count of
virtual includes found
nextline()
Else 'else there was a problem with the comment
closing tag in the next line and beyond
ListBox1.Items.Add(filename & " - includes:
" & OriginalLine)
counter = counter + 1
nextline()
End If
Else 'if the second double quotes is not found
in the same line, display error and continue
ListBox1.Items.Add("There is an error in line:
" & linenumber & " - missing double quotes -")
counter = counter + 1 'count of virtual
includes found
nextline()
End If
End If
End If
End If
indexstart = 0
Pos1 = -1
Pos2 = -1
Pos3 = -1
Pos4 = -1
End Sub

Apr 2 '06 #3
Hi Ameen,

Don't mind, but your code does not appear to be the best way to do
whatever it intends to do. If you give more details about exactly what
you intend to do, it might help others to give you valid suggestions /
alternatives.

Although your code is well commented, I'm still unsure about what
exactly it does. An eagle-eye examination of the code suggests that
this might be better done with Regex.

Regards,

Cerebrus.

Apr 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: deko | last post by:
When I loop through this function, it works fine until it hits End Function - then it jumps to End Select. Very strange... This behavior occurs when Case = 255. Any ideas why this is happening? ...
14
by: BQ | last post by:
Due to a lack of resources, I have to translate the following recursive function in its iterative form. It's a kind of dichotomic search. void SearchSlaves(unsigned long start_uid, unsigned long...
17
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
10
by: nasau | last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the...
16
by: jayapal | last post by:
hi all, what is the differrence b/w the usage or return and the exit in the C programming.. thanks, jay
11
by: Rahul | last post by:
Hi Everyone, I have seen code in different styles like main(argc,argv) int argc; char **argv; { if(argc != 2) exit(1); else exit(0);
6
by: Michael_R_Banks | last post by:
I have a program with a user input subroutine that I want to exit after a specific amount of time. How do I exit the sub when the timer elapsed event fires? Regards, Michael
39
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main'...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.