Jump to content

Simplifying multiple 'Or' conditions in a single function


mtmartis
 Share

Recommended Posts

I would just like to get some feedback from the gurus.

What would be an alternative to a potentially very long 'Or' statement?

I was thinking, it could be possible to have a file with a list of values that my script could run against, but I can't picture how it would be structured into my code.

Or is having a plethora of Or conditions on one line acceptable?

Thanks!

#Include <File.au3>

$FileList=_FileListToArray("C:\Users","*",2)
For $n = 1 To $FileList[0]
   If StringInStr($FileList[$n],"admin1") or StringInStr($FileList[$n],"user1") or StringInStr($FileList[$n],"user2") Then
  MsgBox(0,'',$FileList[$n] & " is exempt")
   Else
  MsgBox(0,'',$FileList[$n] & " is NOT exempt")
   EndIf
Next
Link to comment
Share on other sites

  • Moderators

Hi, mtmartis. How about a Case statement? It may be a little more typing, but it will allow you to easily add and delete entries.

#Include <File.au3>

$FileList=_FileListToArray("C:Users","*",2)

For $n = 1 To $FileList[0]
Select
  Case StringInStr($FileList[$n],"admin1")
    MsgBox(0,'',$FileList[$n] & " is exempt")
  Case StringInStr($FileList[$n],"user1")
    MsgBox(0,'',$FileList[$n] & " is exempt")
  Case StringInStr($FileList[$n],"user2")
    MsgBox(0,'',$FileList[$n] & " is exempt")
  Case Else
    MsgBox(0,'',$FileList[$n] & " is NOT exempt")
EndSelect
Next
Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Also try adding the ContinueCase statement which allows a Case statement to "fall through" to the next one. Here is a modified version of JLogan3o13's example.

#Include <File.au3>

$FileList=_FileListToArray("C:Users","*",2)

For $n = 1 To $FileList[0]
Select
  Case StringInStr($FileList[$n],"admin1")
    ContinueCase
  Case StringInStr($FileList[$n],"user1")
    ContinueCase
  Case StringInStr($FileList[$n],"user2")
    MsgBox(0,'',$FileList[$n] & " is exempt")
  Case Else
    MsgBox(0,'',$FileList[$n] & " is NOT exempt")
EndSelect
Next

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

Hi,

you could use StringRegExp to match multiple users

#Include <File.au3>

$sStrings = "admin1|user1|user2" ; FileRead('pathtofile.ext') ; save strings, separated by a horizontal bar |, in a file and read that
$aFileList = _FileListToArray(@HomeDrive & "Users", "*", 2)

For $n = 1 To $FileList[0]
    ; simply separate the string with a |, if the string contains special characters just wrap it in QE, e.g. Qfoo(bar)E, but I suspect you don't need it
    If StringRegExp($FileList[$n], "(?i)b" & $sStrings & "b") Then ; (?i) makes matches case insensitive, the b are boundaries to avoid matching user21 as valid if  only user2 is in the list
        MsgBox(0,'',$FileList[$n] & " is exempt")
    Else
        MsgBox(0,'',$FileList[$n] & " is NOT exempt")
    EndIf
Next

Edit: added flag and boundaries

Edited by Robjong
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...