More efficient way to write "If Like OR"

RainX

Registered User.
Local time
Today, 11:58
Joined
Sep 22, 2006
Messages
89
Hi all,

I was wondering if there was a more efficient or simple way of writing this

If address$(2) Like "* IL *" Or address$(2) Like "* IL.*" Or address$(2) Like "*,IL*" Or address$(2) Like "* ILL *" Or address$(2) Like "*ILLINOIS*" Then

Thanks in advance
 
You could probably simplify the three first to

address$(2) Like "*[ ,]IL[ ,.]*"

but then I'm not able to simplify more.

Edit:
Nah, make this

address$(2) Like "*[ ,]IL[ .]*"

the two first

perhaps RegExp, though one might question whether that would count as "simplify"... ;)
 
Last edited:
You could try a case statement
 
^^ its via vba

btw thanks roy =D ill give it a go.
 
Last edited:
Here's a short, late bound RegExp sample that I think satisfy the original criteria, though it could probably be written far more elegant.
Code:
    dim re             as object
    set rs = createobject("vbscript.regexp")

    with re
        .IgnoreCase = True
        .Pattern = "( IL | IL\.|,IL| ILL |ILLINOIS)"
        debug.print .Test(address$(2))
    end with
http://regexlib.com/CheatSheet.aspx
 
^^ I would use a select case statement but this is part of a If.... ElseIf Block and it would complicate things.
atleast to me it would lol.
 
I think you should probably move your If...ElseIf to a select case statement too. They are way easier to read and actually less writing.
 
^^ hmm ill give it a try and see if its easier to read.
Right now i have it like this

Code:
If address$(2) Like "*[ ,]IL[ .]*" Or address$(2) Like "*,IL*" Or address$(2) Like "* ILL *" Or address$(2) Like "*ILLINOIS*" Then
                    Print #fx%, ArrayX$(1) + Chr$(9) + ArrayX$(2) + Chr$(9) + address$(1)
ElseIf address$(2) Like "APT *" Then
                    Print #fx%, ArrayX$(1) + Chr$(9) + ArrayX$(2) + Chr$(9) + address$(1)
ElseIf address$(2) = "" And address$(3) = "" And address$(4) = "" And address$(5) = "" Then
                    Print #fx%, ArrayX$(1) + Chr$(9) + ArrayX$(2) + Chr$(9) + ArrayX$(13) + Chr$(9) + city$
Else
                    Print #fx%, ArrayX$(1) + Chr$(9) + ArrayX$(2) + Chr$(9) + address$(1)
End If

Would it really be a whole lot less if i did a case statement?
 
Last edited:
Roy, I'm curious about your method, which looks like it would be simpler. I have no experience with RegExp, but a brief test using that code (I fixed the little typo) returns False no matter what, even when data is "IL". Am I missing something?
 
Well, I just learned something.

from the Access VBA help file:
Note that Is and Like can't be used as comparison operators in a Select Case statement.
 
OK, this has actually run on one of my setups :o
Code:
Sub prohibchar(ByVal v_strIn As String)
    
    Dim re                  As Object
    Set re = CreateObject("vbscript.regexp")
    
    With re
        .IgnoreCase = True
        .Pattern = "( IL | IL\.|,IL| ILL |ILLINOIS)"
        Debug.Print .test(v_strIn)
    End With
    
End Sub
looking at the actual rules "IL" is not supposed to be allowed, but the following combinations inside text. " IL ", " IL.", ",IL", " ILL " and "ILLINOIS". Note the spaces, commas, dot... So the following is supposed to give True (which I think they should also do with the initial code)

"Blast,ILLUSTRATED humour"
"I am seriously ILL at the moment"
"I'm not from IL but Norway"
"Blah IL."

But the following are not

"Blast, ILLUSTRATED humour"
"I am seriously ILL, don't say anything else!"
"I'm not from IL, but Norway"
"Blah IL"

Regular Expressions are very powerfull tools for complex pattern matching, replace and similar text operations. Where char by char approaches (looping), are invaluable for working with shorter strings, they have several disadvantages when the search patterns becomes more complex, or the text length increases. In such cases, Regular Expressions can be useful. Where for instance the efficiency of char by char approaches are less and less as text length increases, Regular Expressions are less influenced.

The simple pattern I used, was the five different initial patterns, including spaces. Those are within a group (parenthesis), and separated by the pipe character which means alternation. The backspace character, serves as an escape character, as dot has a special meaning. The group isn't really necessary, though. But the pattern expresses the initial request.

In addition, I allowed for both upper and lower case letters. Without specifying, .IgnoreCase defaults to False. I also used the default values for .Global (false - match only the first occurence) and .Multiline (false - only search first line)
 
Thanks for the explanation Roy. I have it working now. I'd missed the fact that " IL " was surrounded by spaces in the original question, so I was testing "IL". Neat tool, I'll have to watch for appropriate places to use it!
 

Users who are viewing this thread

Back
Top Bottom