Jump to content

Basic If...Else...EndIf that make my head spin fast


Go to solution Solved by DW1,

Recommended Posts

Ok, I admit this is (very) basic but currently it make my head spin fast :(

I have some variable like this (note: i dont want array even with array maybe it will be much simpler and easier, consider it's a flaw from the beginnning of design)

$key1="value1"
$key2="value2"
$key3="value3"
$key4="value4"
$key5="value5"
$key6="value6"

if $key1 = "" then set $condition = false and doesn't need to check other key anymore

if $key1<>"" and FileExists($key1) then $condition = true and pls check another key
if $key2<>"" and FileExists($key2)  then $condition = true and pls check another key, but if $key2<>"" but NOT FileExists($key2) then $condition=false and do not need to check another key anymore.

if $key3<>"" and FileExists($key3)  then $condition = true and pls check another key, but if $key3<>"" but NOT FileExists($key3) then $condition=false and do not need to check another key anymore.

<do that above until key6>

If NOT $condition then

   ...

EndIf

Thank you!

Link to comment
Share on other sites

You seem to have a lot of what you want/need there, so why not turn what you have into code ... makes it easier for the rest of us, and you never know, you may not even need us if you have most of the logic in front of you.

Don't forget, that you can use AND and OR in your IF statements, as well as NOT or <>

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Another point, if your conditional check of If something = ""fails, there's no need to test if it's <> "" because you already know it is.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hi TheSaint,

I did turn that into the code, but it doesn't seem right :ermm:

Global $condition = False

$key1 = "c:\file1.txt"
$key2 = "c:\file2.txt"
$key3 = "c:\file3.txt"
$key4 = "c:\file4.txt"
$key5 = "c:\file5.txt"
$key6 = "c:\file6.txt"

If $key1 = "" Then
    $condition = False
Else
    If $key1 <> "" And FileExists($key1) Then $condition = True
    If $key2 <> "" And FileExists($key2) Then $condition = True
    If $key3 <> "" And FileExists($key3) Then $condition = True
    If $key4 <> "" And FileExists($key4) Then $condition = True
    If $key5 <> "" And FileExists($key5) Then $condition = True
    If $key6 <> "" And FileExists($key6) Then $condition = True
EndIf
Link to comment
Share on other sites

 

Perhaps something like this?

Local $condition = True
For $i = 1 To 6
    Local $skey = Eval("key" & $i)
    If $skey = "" Or Not FileExists($skey) Then
        $condition = False
        ExitLoop
    EndIf
Next

 

Wow, this seems great! I never know about Eval until now.

But I need something like this:

$key1="c:file1.txt"

$key2=""

$key3="c:file3.txt"

Above will make the $condition = false because $key2=""

but this:

$key1="c:file1.txt"

$key2=""

$key3=""

$key4=""

$key5=""

$key6=""

will make $condition = true because minimal requirement for $condition = true is $key1 <> "" AND FileExists($key1) = True

while other keys can be not defined. If other keys are defined then they have to satisfy FileExists.

Also if keys are jump like this:

$key1="c:file1.txt"

$key2=""

$key3="c:file3.txt"

even if FileExist for $key1 and $key3, $condition should be false because it jump.

this will cause $condition to true if FileExists for all of them:

$key1="c:file1.txt"

$key2="c:file2.txt"

$key3="c:file3.txt"

Link to comment
Share on other sites

Seems like this loop is making up for bad coding practices, but you seem to be aware of that already.

Local $condition = True
Local $bBlank = False
For $i = 1 To 6
    Local $skey = Eval("key" & $i)
    If $skey = "" Then
        $bBlank = True
    Else
        If $bBlank = True Or Not FileExists($skey) Then
            $condition = False
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0,'test',$condition)

Should meet all of the criteria you have given us so far.  Though it does return true if all are blank (this is a condition you have not addressed that I can see, so I don't know if it should be true or false)

EDIT: I think you actually did specify that all blanks should return false.  This will return false for all blanks

Local $condition = True
Local $iBlank = 0
For $i = 1 To 6
    Local $skey = Eval("key" & $i)
    If $skey = "" Then
        $iBlank += 1
        If $iBlank = 6 Then
            $condition = False
            ExitLoop
        EndIf
    Else
        If $iBlank > 0 Or Not FileExists($skey) Then
            $condition = False
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0,'test',$condition)
Edited by danwilli
Link to comment
Share on other sites

  • Solution

Hi Danwilli,

Thank you very much for your help! :)

This is posted at post #1: if $key1 = "" then set $condition = false and doesn't need to check other key anymore

 

So no matter what other key's value is, as long as $key1="" then $condition = false

Ah so you did.  In that case

Local $condition = True
Local $bBlank = False
For $i = 1 To 6
    Local $skey = Eval("key" & $i)
    If $skey = "" Then
        $bBlank = True
        If $i = 1 Then
            $condition = False
            ExitLoop
        EndIf
    Else
        If $bBlank = True Or Not FileExists($skey) Then
            $condition = False
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0, 'test', $condition)
Link to comment
Share on other sites

 

Seems like this loop is making up for bad coding practices, but you seem to be aware of that already.

Local $condition = True
Local $bBlank = False
For $i = 1 To 6
    Local $skey = Eval("key" & $i)
    If $skey = "" Then
        $bBlank = True
    Else
        If $bBlank = True Or Not FileExists($skey) Then
            $condition = False
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0,'test',$condition)

Should meet all of the criteria you have given us so far.  Though it does return true if all are blank (this is a condition you have not addressed that I can see, so I don't know if it should be true or false)

EDIT: I think you actually did specify that all blanks should return false.  This will return false for all blanks

Local $condition = True
Local $iBlank = 0
For $i = 1 To 6
    Local $skey = Eval("key" & $i)
    If $skey = "" Then
        $iBlank += 1
        If $iBlank = 6 Then
            $condition = False
            ExitLoop
        EndIf
    Else
        If $iBlank > 0 Or Not FileExists($skey) Then
            $condition = False
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0,'test',$condition)

 

Tested, working very great!!

I'm not really sure how it works but it just works!!

I will learn how the code works

I spend almost 1.5 days to figure it out and u done that in 5 mins (or maybe less) o:)

Thanks again for your help, Danwilli :thumbsup:

Link to comment
Share on other sites

Tested, working very great!!

I'm not really sure how it works but it just works!!

I will learn how the code works

I spend almost 1.5 days to figure it out and u done that in 5 mins (or maybe less) o:)

Thanks again for your help, Danwilli :thumbsup:

I commented the code so you can learn from it and understand the thought process :)  Glad it's working for you

;Start with $condition = True until proven false
Local $condition = True
;We will use $bBlank to track if there has been a blank value (since any non blank following a blank makes $condition = False)
Local $bBlank = False
;Loop through the 6 keys
For $i = 1 To 6
    ;Set $skey to the value of the currect key in the loop (using eval due to lack of array and naming convention allows for it)
    Local $skey = Eval("key" & $i)
    ;If the key is blank then...
    If $skey = "" Then
        ;Record that we found a blank (we can check against this later)
        $bBlank = True
        ;If we found a blank and it is the first key, we set condition to false and exit the loop (no more checks)
        If $i = 1 Then
            ;Set condition to false
            $condition = False
            ;Exit the loop (no more checks)
            ExitLoop
        EndIf
    ;If the key is NOT blank then...
    Else
        ;Check to see if we have seen a blank before (if so, we have a "jump" as you put it, and will set condition to false and exitloop).  Also checking if the file doesn't exist for this key (and if it doesn't set to false and exitloop)
        If $bBlank = True Or Not FileExists($skey) Then
            ;Set condition to false
            $condition = False
            ;Exit the loop (no more checks)
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0, 'test', $condition)
Link to comment
Share on other sites

Just a quick reminder declaring variables in a loop will increase the execution time. Declare outside instead.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Just a quick reminder declaring variables in a loop will increase the execution time. Declare outside instead.

D'oh.  And in a thread where I mention coding practices.

Corrected

;Start with $condition = True until proven false
Local $condition = True
;We will use $bBlank to track if there has been a blank value (since any non blank following a blank makes $condition = False)
Local $bBlank = False
;Declase our $skey var
Local $skey
;Loop through the 6 keys
For $i = 1 To 6
    ;Set $skey to the value of the currect key in the loop (using eval due to lack of array and naming convention allows for it)
    $skey = Eval("key" & $i)
    ;If the key is blank then...
    If $skey = "" Then
        ;Record that we found a blank (we can check against this later)
        $bBlank = True
        ;If we found a blank and it is the first key, we set condition to false and exit the loop (no more checks)
        If $i = 1 Then
            ;Set condition to false
            $condition = False
            ;Exit the loop (no more checks)
            ExitLoop
        EndIf
    ;If the key is NOT blank then...
    Else
        ;Check to see if we have seen a blank before (if so, we have a "jump" as you put it, and will set condition to false and exitloop).  Also checking if the file doesn't exist for this key (and if it doesn't set to false and exitloop)
        If $bBlank = True Or Not FileExists($skey) Then
            ;Set condition to false
            $condition = False
            ;Exit the loop (no more checks)
            ExitLoop
        EndIf
    EndIf
Next
MsgBox(0, 'test', $condition)
Link to comment
Share on other sites

No problem, we're all learning still.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Danwilli,

Ok, understand :)

That is if I read the code...

If write it from beginning, it could be a different story :idiot:

btw, this:

;Set $skey to the value of the currect key in the loop (using eval due to lack of array and naming convention allows for it)
Local $skey = Eval("key" & $i)

 

What if the naming convention doesn't allows for it? (the variable name is random)

Edited by michaelslamet
Link to comment
Share on other sites

Danwilli,

Ok, understand :)

That is if I read the code...

If write it from beginning, it could be a different story :idiot:

btw, this:

;Set $skey to the value of the currect key in the loop (using eval due to lack of array and naming convention allows for it)
Local $skey = Eval("key" & $i)

What if the naming convention doesn't allows for it? (the variable name is random)

Then, personally, I would move all of the values of the variables in to an array prior to starting a loop.  Same basic idea, just an extra step.

Link to comment
Share on other sites

Eval, Assign and IsDeclared should be used sparingly in my humble opinion.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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...