Sign in to follow this  
Followers 0
mtmartis

Simplifying multiple 'Or' conditions in a single function

6 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
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".

Share this post


Link to post
Share on other sites

Much cleaner, JohnQSmith :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Thanks, all for the suggestions. Double thanks Robjong for hitting the nail on the head! I never used StringRegExp before.

Share this post


Link to post
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
Sign in to follow this  
Followers 0