Tags: active, asp, collection, eliminating, expression, loops, matches, nestin, newcomer, page, programming, regular, relative, script, server, toextract, vbscript, write

Nestin For ..... Next loops

On Programmer » Active Server Page (ASP)

11,625 words with 4 Comments; publish: Wed, 30 Apr 2008 14:45:00 GMT; (20062.50, « »)

I am a relative newcomer to VBScript and I am trying to write a script to

extract a collection of matches of a regular expression, eliminating the

duplicates, the purpose of which is to aid in editing CNC programs.

Below is my attempt at the Function for this task, but I keep getting a

compilation error stating that there is an "Unexpected 'Next'" at line 84

(the 2nd Next call). I have examined my code time and again but I cannot see

what I doing wrong. Can anyone advise me on this issue?

Thanks in advance

Robert Luck

'---

Option Explicit

Dim FileName

Dim intLineNumberFlag

Dim strFeedFind

'Test code

FileName = "D:\cnc\o0923.t"

strFeedFind = MultiFindRegExp (FileName, "F[1-9]+", 10, 100)

MsgBox strFeedFind

'---

Function MultiFindRegExp (strFileName, strFind, intStartLine, intEndLine)

Dim reFind

Dim FSO1

Dim objStream1

Dim strLine

Dim colMatches

Dim objMatch

Dim strFound

Dim intLineNumber

Dim arrTemp()

Dim strArray

Dim intLoopCount

Dim intLoopIndex

Dim boolFirst

Dim boolFound

Set reFind = New RegExp

With reFind

.Pattern = strFind

.IgnoreCase = True

.Global = True

End With

Set FSO1 = CreateObject(FileSystemObject)

Set objStream1 = FSO1.OpenTextFile(strFileName)

boolFirst = True

boolFound = True

Do While Not objStream1.AtEndOfStream

intLineNumber = objStream1.Line

strLine = objStream1.ReadLine

If intLineNumber > intStartLine And intLineNumber < intEndLine Then

If reFind.Test(strLine) Then

Set colMatches = reFind.Execute(strLine)

If colMatches.Count > 1 Then

MsgBox "More than one instance in line",, strFind

End If

For Each objMatch in colMatches

strFound = objMatch.Value

If boolFirst = True Then

Redim Preserve arrTemp(intLoopCount)

arrTemp(intLoopCount) = strFound

intLoopCount =intLoopCount + 1

boolFirst = False

Else If boolFirst = False Then

boolFound = False

For intLoopIndex = 1 to Ubound(arrTemp,1)

If strFound = arrTemp(intLoopIndex) Then

boolFound = True

End If

Next

End If

If boolFound = False Then

Redim Preserve arrTemp(intLoopCount)

arrTemp(intLoopCount) = strFound

intLoopCount =intLoopCount + 1

boolFound = False

End If

Next 'here is the problem

End If

End If

Loop

objStream1.Close

strArray = Join(arrTemp , ",")

MsgBox strArray,, "Transfer string"

MultiFindRegExp = strArray

End Function

All Comments

Leave a comment...

  • 4 Comments
    • Robert Luck wrote:

      > I am a relative newcomer to VBScript and I am trying to write a

      > script to extract a collection of matches of a regular expression,

      > eliminating the duplicates, the purpose of which is to aid in editing

      > CNC programs.

      >

      Go through the script carefully matching up the If's with Else's and End

      If's, Do's with Loop's and For's with Next's. It would be easier if you used

      consistent indentation to align the start/end of logical block structures.

      Make sure that your Else If wasn't supposed to be ElseIf.

      The Else If vs. ElseIf is probably the cause. Note that example 1 below

      needs 2 End If's and example 2 needs only 1.

      'example 1...

      '

      If ... Then

      ..

      Else If ... Then

      ..

      End If

      End If

      'example 2..'

      '

      If ... Then

      ..

      ElseIf ... Then

      ..

      End If

      > Below is my attempt at the Function for this task, but I keep getting

      > a compilation error stating that there is an "Unexpected 'Next'" at

      > line 84 (the 2nd Next call). I have examined my code time and again

      > but I cannot see what I doing wrong. Can anyone advise me on this

      > issue?

      > Thanks in advance

      > Robert Luck

      > '---

      > Option Explicit

      > Dim FileName

      > Dim intLineNumberFlag

      > Dim strFeedFind

      > 'Test code

      > FileName = "D:\cnc\o0923.t"

      >

      > strFeedFind = MultiFindRegExp (FileName, "F[1-9]+", 10, 100)

      > MsgBox strFeedFind

      >

      > '---

      > Function MultiFindRegExp (strFileName, strFind, intStartLine,

      > intEndLine)

      > Dim reFind

      > Dim FSO1

      > Dim objStream1

      > Dim strLine

      > Dim colMatches

      > Dim objMatch

      > Dim strFound

      > Dim intLineNumber

      > Dim arrTemp()

      > Dim strArray

      > Dim intLoopCount

      > Dim intLoopIndex

      > Dim boolFirst

      > Dim boolFound

      >

      > Set reFind = New RegExp

      > With reFind

      > .Pattern = strFind

      > .IgnoreCase = True

      > .Global = True

      > End With

      > Set FSO1 = CreateObject(FileSystemObject)

      > Set objStream1 = FSO1.OpenTextFile(strFileName)

      > boolFirst = True

      > boolFound = True

      > Do While Not objStream1.AtEndOfStream

      > intLineNumber = objStream1.Line

      > strLine = objStream1.ReadLine

      > If intLineNumber > intStartLine And intLineNumber < intEndLine Then

      > If reFind.Test(strLine) Then

      > Set colMatches = reFind.Execute(strLine)

      > If colMatches.Count > 1 Then

      > MsgBox "More than one instance in line",, strFind

      > End If

      > For Each objMatch in colMatches

      > strFound = objMatch.Value

      > If boolFirst = True Then

      > Redim Preserve arrTemp(intLoopCount)

      > arrTemp(intLoopCount) = strFound

      > intLoopCount =intLoopCount + 1

      > boolFirst = False

      > Else If boolFirst = False Then

      > boolFound = False

      > For intLoopIndex = 1 to Ubound(arrTemp,1)

      > If strFound = arrTemp(intLoopIndex) Then

      > boolFound = True

      > End If

      > Next

      > End If

      >

      > If boolFound = False Then

      > Redim Preserve arrTemp(intLoopCount)

      > arrTemp(intLoopCount) = strFound

      > intLoopCount =intLoopCount + 1

      > boolFound = False

      > End If

      > Next 'here is the problem

      > End If

      > End If

      > Loop

      > objStream1.Close

      > strArray = Join(arrTemp , ",")

      > MsgBox strArray,, "Transfer string"

      > MultiFindRegExp = strArray

      > End Function

      Michael Harris

      Microsoft MVP Scripting

      http://maps.google.com/maps?q=Sammamish%20WA%20US

      #1; Wed, 30 Apr 2008 14:46:00 GMT
    • "Robert Luck" wrote in message

      news:YMrTd.176540$B8.22389.asp.questionfor.info.fe3.news.blueyonder.co.uk...

      :I am a relative newcomer to VBScript and I am trying to write a script to

      : extract a collection of matches of a regular expression, eliminating the

      : duplicates, the purpose of which is to aid in editing CNC programs.

      :

      : Below is my attempt at the Function for this task, but I keep getting a

      : compilation error stating that there is an "Unexpected 'Next'" at line 84

      : (the 2nd Next call). I have examined my code time and again but I cannot

      see

      : what I doing wrong. Can anyone advise me on this issue?

      :

      : Thanks in advance

      : '---

      : Option Explicit

      : Dim FileName

      : Dim intLineNumberFlag

      : Dim strFeedFind

      : 'Test code

      : FileName = "D:\cnc\o0923.t"

      : strFeedFind = MultiFindRegExp (FileName, "F[1-9]+", 10, 100)

      : MsgBox strFeedFind

      : Function MultiFindRegExp (strFileName, strFind, intStartLine, intEndLine)

      : Dim reFind

      : Dim FSO1

      : Dim objStream1

      : Dim strLine

      : Dim colMatches

      : Dim objMatch

      : Dim strFound

      : Dim intLineNumber

      : Dim arrTemp()

      : Dim strArray

      : Dim intLoopCount

      : Dim intLoopIndex

      : Dim boolFirst

      : Dim boolFound

      : Set reFind = New RegExp

      : With reFind

      : .Pattern = strFind

      : .IgnoreCase = True

      : .Global = True

      : End With

      : Set FSO1 = CreateObject(FileSystemObject)

      : Set objStream1 = FSO1.OpenTextFile(strFileName)

      : boolFirst = True

      : boolFound = True

      : Do While Not objStream1.AtEndOfStream

      : intLineNumber = objStream1.Line

      : strLine = objStream1.ReadLine

      : If intLineNumber > intStartLine And intLineNumber < intEndLine Then

      : If reFind.Test(strLine) Then

      : Set colMatches = reFind.Execute(strLine)

      : If colMatches.Count > 1 Then

      : MsgBox "More than one instance in line",, strFind

      : End If

      : For Each objMatch in colMatches

      : strFound = objMatch.Value

      : If boolFirst = True Then

      : Redim Preserve arrTemp(intLoopCount)

      : arrTemp(intLoopCount) = strFound

      : intLoopCount =intLoopCount + 1

      : boolFirst = False

      : Else If boolFirst = False Then

      : boolFound = False

      : For intLoopIndex = 1 to Ubound(arrTemp,1)

      : If strFound = arrTemp(intLoopIndex) Then

      : boolFound = True

      : End If

      : Next

      : End If

      No, this is your problem here...

      : Else If boolFirst = False Then

      Use:

      Else and remove If boolFirst = False Then

      A boolean only has two possibilities.

      : boolFound = False

      : For intLoopIndex = 1 to Ubound(arrTemp,1)

      : If strFound = arrTemp(intLoopIndex) Then

      : boolFound = True

      : End If

      : Next

      : End If

      or use:

      ElseIf boolFirst = False Then

      : boolFound = False

      : For intLoopIndex = 1 to Ubound(arrTemp,1)

      : If strFound = arrTemp(intLoopIndex) Then

      : boolFound = True

      : End If

      : Next

      : End If

      Roland Hall

      /* This information is distributed in the hope that it will be useful, but

      without any warranty; without even the implied warranty of merchantability

      or fitness for a particular purpose. */

      Technet Script Center - http://www.microsoft.com/technet/scriptcenter/

      WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp

      MSDN Library - http://msdn.microsoft.com/library/default.asp

      #2; Wed, 30 Apr 2008 14:47:00 GMT
    • Roland & Micheal,

      Thanks for you help gents, it was the Else If call that was causing the

      problem.

      I will try and improve my indenting (which is an alien concept in the world

      of CNC programming)

      Robert

      #3; Wed, 30 Apr 2008 14:48:00 GMT
    • "Robert Luck" wrote in message

      news:F2mUd.196011$B8.10447.asp.questionfor.info.fe3.news.blueyonder.co.uk...

      : Roland & Micheal,

      :

      : Thanks for you help gents, it was the Else If call that was causing the

      : problem.

      :

      : I will try and improve my indenting (which is an alien concept in the

      world

      : of CNC programming)

      Michael's pretty handy to have around. Not sure what CNC programming is but

      here it means it's time to get off the computer and take a break. CNC =

      Could Not Compute (O:=

      Roland Hall

      /* This information is distributed in the hope that it will be useful, but

      without any warranty; without even the implied warranty of merchantability

      or fitness for a particular purpose. */

      Technet Script Center - http://www.microsoft.com/technet/scriptcenter/

      WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp

      MSDN Library - http://msdn.microsoft.com/library/default.asp

      #4; Wed, 30 Apr 2008 14:49:00 GMT