A recent thread222-48177 with the original, yet uninspiring
title: Need help, look at my problem nevertheless drew my attention.
The author wanted to replace a particular line in a text file: the line contained a string composed of 6 arbitrary digits, followed by an asterisk (in position 7), and followed again by an arbitrary digit. The string was always 8 characters long, the asterisk always being in position 7. If the string was found, the asterisk was to be replaced by a minus sign. That particular line only contained that string.
Here's an extract of a sample file, the line searched for in red:
^job 09700154 -zSACSS2 -aapon -c01
^global EM.01
Foot doctor of novato
^global EM.02
^global EM.03
PO BOX 192
^global AP.13
111-22-2345
^global CH.NUM
022807*1
^global PW.01
MARK
In the output file the string 022807*1 becomes 022807-1
Several workable and working suggestions were proposed, most of which used IsNumeric or Instr Built-in functions.
There exists however an LIKE Operator within the VB language that solves this problem like a piece of cake. This operator performs comparisons against a mask. As a fringe(?) benefit most times it is
up to 30% faster than preceding functions.
Sample code:
Const intAsteriskPosition As Integer = 7
Dim strInput As String
Dim strTrimInput As String
Open App.Path & "\Test.txt" For Input As #1
Open App.Path & "\TestOutput.txt" For Output As #2
Do Until EOF(1)
Line Input #1, strInput
'Make sure there are no leading or trailing blanks
strTrimInput = Trim$(strInput)
Select Case strTrimInput Like "######[*]#"
Case True
Mid$(strTrimInput, intAsteriskPosition, 1) = "-"
Case False
'do nothing
End Select
Print #2, strTrimInput
Loop
Close #1
Close #2
The code also takes advantage of the fact that MID not only is a function, but also a statement. As such it can be found on the Left side of an assignment.
Most elementary variable search patterns can be solved adequately by using the LIKE operator. If you need to use really complex search patterns, then the Regular Expressions object RegExp of the ScriptControl might be a more appropriate choice.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
title: Need help, look at my problem nevertheless drew my attention.
The author wanted to replace a particular line in a text file: the line contained a string composed of 6 arbitrary digits, followed by an asterisk (in position 7), and followed again by an arbitrary digit. The string was always 8 characters long, the asterisk always being in position 7. If the string was found, the asterisk was to be replaced by a minus sign. That particular line only contained that string.
Here's an extract of a sample file, the line searched for in red:
^job 09700154 -zSACSS2 -aapon -c01
^global EM.01
Foot doctor of novato
^global EM.02
^global EM.03
PO BOX 192
^global AP.13
111-22-2345
^global CH.NUM
022807*1
^global PW.01
MARK
In the output file the string 022807*1 becomes 022807-1
Several workable and working suggestions were proposed, most of which used IsNumeric or Instr Built-in functions.
There exists however an LIKE Operator within the VB language that solves this problem like a piece of cake. This operator performs comparisons against a mask. As a fringe(?) benefit most times it is
up to 30% faster than preceding functions.
Sample code:
Const intAsteriskPosition As Integer = 7
Dim strInput As String
Dim strTrimInput As String
Open App.Path & "\Test.txt" For Input As #1
Open App.Path & "\TestOutput.txt" For Output As #2
Do Until EOF(1)
Line Input #1, strInput
'Make sure there are no leading or trailing blanks
strTrimInput = Trim$(strInput)
Select Case strTrimInput Like "######[*]#"
Case True
Mid$(strTrimInput, intAsteriskPosition, 1) = "-"
Case False
'do nothing
End Select
Print #2, strTrimInput
Loop
Close #1
Close #2
The code also takes advantage of the fact that MID not only is a function, but also a statement. As such it can be found on the Left side of an assignment.
Most elementary variable search patterns can be solved adequately by using the LIKE operator. If you need to use really complex search patterns, then the Regular Expressions object RegExp of the ScriptControl might be a more appropriate choice.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]