Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I like LIKE 3

Status
Not open for further replies.

rvBasic

Programmer
Oct 22, 2000
414
BE
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]
 
Could you enlighten me as to what the difference is between:
Mid() and Mid$() ?

-Mats
 
Visual Basic supplies most functions that return strings in two formats, one ending with a dollar sign (e.g. Mid$) and one ending without the dollar sign (Mid).

The non-dollar ones effectively accept and return Variants and as such they are able to handle Null values. Throwing a variant into a dollar function generates an error. Non-dollar functions are more flexible than dollar ones.

This comes at a price: performance. Because the dollar functions only accept string variables, they don't need to perform any data conversion. As a consequence they are much faster.

Morale: If flexibility is important, use non-dollar functions. If performance is key, use dollar functions _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
OK, thanx a lot!

-M
 
The February 2001 issue of VBPJ contains an interesting article (pp 54 - 65) written by Francesco Balena: Streamline Input Validation. This is a MUST READ for every decent programmer.

It contains a Side Bar Validate the Smart Way that stresses the Value of the LIKE operator for this kind of real life situations.

The really good news is that this article with its sample code is entirely available online:

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top