Jump to content

Is there a "shorter" way?


MariusN
 Share

Recommended Posts

Here one more:
 

Global $read1 = 4, $read2 = 4, $read3 = 4, $read4 = 4, $read5 = 4, $read6 = 4

If BitAND($read1 = 4, $read2 = 4, $read3 = 4, $read4 = 4, $read5 = 4, $read6 = 4) Then
    MsgBox(0, "Test", "Bingo")
EndIf

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

One thing to remember about using And in a comparison, as soon as the comparison returns false, the comparing stops. So if you have a very long set of variables to check, it would be faster to just use And in some cases.

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

I do not understand why.

 

this line:

If _IfAndOr($TerminateKey,"F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12","or") = 1 Then

does not seem to you shorter than this line ?:

  If $ TerminateKey = "OFF" Or $ TerminateKey = "F1" Or $ TerminateKey = "F2" Or $ TerminateKey = "F3" Or $ TerminateKey = "F4" Or $ TerminateKey = "F5" Or $ TerminateKey = "F6" Or $ TerminateKey = "F8" Or $ TerminateKey = "F8" Or $ TerminateKey = "F9" Or $ TerminateKey = "F10" Or $ TerminateKey = "F11" Or $ TerminateKey = "F12" Then

Because one line is shorter than calling a multi-line function from a single line, because you still have the multi-line function in your script. So, if you think 10 lines is shorter than 1 line then you and I have different opinions of what shorter means.

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

Naturally it depends on the context what is bad and what is good practice

If not stringinstr((stringinstr("OFFF1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11F12", $TerminateKey)-1)/3,".") Then
    consolewrite("We matched " & @CRLF)
EndIf

Is more bulletproof but far away from more readable

and if its about the shortest if line for the first question

if a() then consolewrite("Yes its four")

func a()
    If $read1 = 4 And $read2 = 4 And $read3 = 4 And $read4 = 4 And $read5 = 4 And $read6 = 4 Then
        return True
    else
        return false
    EndIf
EndFunc

Not sure what MariusN intention is. Seems to be more readibility

Link to comment
Share on other sites

one way:

$bIs4 = True
For $i = 1 To 6
    If Eval("read" & $i) <> 4 Then 
        $bIs4 = False
        ExitLoop
    EndIf
Next
If $bIs4 Then ConsoleWrite("All Are Four" & @CRLF)

or dynamically (if you add more read variables in the same fashion)

$bIs4 = True

$iCounter = 1
While True
    If Eval("read" & $iCounter) <> 4 Then
        If @error Then ExitLoop
        $bIs4 = False
        ExitLoop
    EndIf
    $iCounter += 1
WEnd
If $bIs4 Then ConsoleWrite("All Are Four" & @CRLF)





If you have so many of the same type of variable, I would suggest adding them to an array.  Then you can loop through

Global $array[6]=[4,4,4,4,4,4]
$bIs4 = True
For $i = 0 To UBound($array) - 1
    If $array[$i] <> 4 Then
        $bIs4 = False
        ExitLoop
    EndIf
Next
If $bIs4 Then ConsoleWrite("All Are Four" & @CRLF)

Last one.  You can get tricky with arrays, and enum the dims.  So you know what dim correlates to what variable you are defining

Global Enum $iObject1,$iObject2,$iObject3,$iObject4,$iObject5,$iObject6,$iObjectUBound
Global $array[$iObjectUBound]

; where you add the proper value in for each array dim, somewhere in your code...this is just to populate for demo
$array[$iObject1] = 4
$array[$iObject2] = 4
$array[$iObject3] = 4
$array[$iObject4] = 4
$array[$iObject5] = 4
$array[$iObject6] = 4

$bIs4 = True
For $i = 0 To UBound($array) - 1
    If $array[$i] <> 4 Then
        $bIs4 = False
        ExitLoop
    EndIf
Next
If $bIs4 Then ConsoleWrite("All Are Four" & @CRLF)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Because one line is shorter than calling a multi-line function from a single line, because you still have the multi-line function in your script. So, if you think 10 lines is shorter than 1 line then you and I have different opinions of what shorter means.

But in fact you always use multi-line functions. The difference is that these functions are Included in autoit and you don't see them in the editor.

for example when shorting it with stringregexp() or BitOr() or something else then i fact you still "calling a multi-line function from a single line" but the difference is that you don't see them..

but for the compiler if think that what i do and what you do is the same thing.. the compiler will have to write the BitOr() or stringregexp() or my my function in a machine code..

Edited by Guest
Link to comment
Share on other sites

Improved function:

;================= Example 1 ==========================
$IfAndOr = _IfAndOr("a|b|c|d|e","f|g|h|i|k","and")
; This equal to:
;   If "a" = "f" And "b" = "g" And "c" = "h" And "d" = "i" And "e" = "k" Then
;   EndIf

If $IfAndOr = 1 Then
    MsgBox(0,"Example 1","Condition exist")
Else
    MsgBox(0,"Example 1","Condition Not exist")
EndIf
;================= END Example 1 =======================


;================= Example 2 ===========================
$IfAndOr = _IfAndOr("a,b,c,d,e","a,b,c,d,e","and",",")
; This equal to:
;   If "a" = "a" And "b" = "b" And "c" = "c" And "d" = "d" And "e" = "e" Then
;   EndIf
If $IfAndOr = 1 Then
    MsgBox(0,"Example 2","Condition exist")
Else
    MsgBox(0,"Example 2","Condition Not exist")
EndIf
;================= END Example 2 =======================

;================= Example 3 ===========================
$IfAndOr = _IfAndOr("a!b!c!d!e","x!w!c!g!n","or","!")
; This equal to:
;   If "a" = "x" Or "b" = "w" Or "c" = "c" Or "d" = "g" Or "e" = "n" Then
;   EndIf
If $IfAndOr = 1 Then
    MsgBox(0,"Example 3","Condition exist")
Else
    MsgBox(0,"Example 3","Condition Not exist")
EndIf
;================= END Example 3 =======================


;================= Example 4 ===========================
$IfAndOr = _IfAndOr("a|b|c","a","or")
; This equal to:
;   If "a" = "a" Or "a" = "b" Or "a" = "c" Then
;   EndIf
If $IfAndOr = 1 Then
    MsgBox(0,"Example 4","Condition exist")
Else
    MsgBox(0,"Example 4","Condition Not exist")
EndIf
;================= END Example 4 =======================

;================= Example 5 ===========================
$IfAndOr = _IfAndOr("a","a|b|c","and")
; This equal to:
;   If "a" = "a" And "a" = "b" And "a" = "c" Then
;   EndIf
If $IfAndOr = 1 Then
    MsgBox(0,"Example 5","Condition exist")
Else
    MsgBox(0,"Example 5","Condition Not exist")
EndIf
;================= END Example 5 =======================

;Do not do Exampels:
; _IfAndOr("a|b|c|d","a|b|c","and") <= will Return -1 because it's improper use
; _IfAndOr("a|b|c","a|b|c|d","and") <= will Return -1 because it's improper use





; #FUNCTION# ====================================================================================================================
; Name ..........: _IfAndOr
; Description ...: function to shorten and / or conditions
; Syntax ........: _IfAndOr($Variables1, $Variables2, $mode[, $SplitVar = "|"])
; Parameters ....: $Variables1          - Group of variables/one variable
;                  $Variables2          - Group of variables/one variable
;                  $mode                - condition can be "and" or "or"
;                  $SplitVar            - [optional] the split letter that will be use to process groups of the $Variables. Default is "|".
; Return values .: Return 1 if True ,  Return 0 if False , Return -1 if the parameters are incorrect 
; Author ........: gil900
; Modified ......: yes
; Remarks .......: 
; Related .......: 
; Link ..........: http://www.autoitscript.com/forum/topic/155336-is-there-a-shorter-way/page-2
; Example .......: yes
; ===============================================================================================================================
Func _IfAndOr($Variables1,$Variables2,$mode,$SplitVar = "|")
    Local $Output = 0 , $count = 0
    $var1 = StringSplit($Variables1,$SplitVar,1)
    $var2 = StringSplit($Variables2,$SplitVar,1)
    If $var1[0] = $var2[0] Then
        For $a = 1 To $var1[0]
            If $var1[$a] = $var2[$a] Then
                $count = $count+1
                If $mode = "and" And $count = $var1[0] Then
                    $Output = 1
                ElseIf $mode = "or" Then
                    $Output = 1
                    ExitLoop
                EndIf
            EndIf
        Next
    Else
        If $var1[0] = 1 Or $var2[0] = 1 Then
            If $var1[0] > 1 Then
                $max = $var1[0]
                $maxvar = $var1
                $mainvar = $var2[1]
            Else
                $max = $var2[0]
                $maxvar = $var2
                $mainvar = $var1[1]
            EndIf
            For $a = 1 To $max
                If $mainvar = $maxvar[$a] Then
                    $count = $count+1
                    If $mode = "and" And $count = $max Then
                        $Output = 1
                    ElseIf $mode = "or" Then
                        $Output = 1
                        ExitLoop
                    EndIf
                EndIf
            Next
        Else
            $Output = -1
        EndIf
    EndIf
    Return $Output
EndFunc
Edited by Guest
Link to comment
Share on other sites

 

@gil900: I doubt if below wil work

$read1=4
$read2=4

_IfAndOr("$read1|$read2","4|4","and") then

It should not work.

This should work:

$read1=4
$read2=4

$test = _IfAndOr($read1&"|"&$read2,"4|4","and")

ConsoleWrite($test&@CRLF)
Link to comment
Share on other sites

But in fact you always use multi-line functions. The difference is that these functions are Included in autoit and you don't see them in the editor.

for example when shorting it with stringregexp() or BitOr() or something else then i fact you still "calling a multi-line function from a single line" but the difference is that you don't see them..

but for the compiler if think that what i do and what you do is the same thing.. the compiler will have to write the BitOr() or stringregexp() or my my function in a machine code..

But the OP asked for a shorter way to write a comparison, you method isn't any shorter to write. What happens "under the hood" has no meaning here, what matters here is that your function takes just as much to write out as his comparison, with perhaps a couple of less characters. Plus, it makes troubleshooting it a lot harder because of the way it's written.

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

But the OP asked for a shorter way to write a comparison, you method isn't any shorter to write. What happens "under the hood" has no meaning here, what matters here is that your function takes just as much to write out as his comparison, with perhaps a couple of less characters. Plus, it makes troubleshooting it a lot harder because of the way it's written.

the OP really do not need it.

It is important to note that I also mentioned my problem in this thread and then I came up with a solution.

There are cases that the use of my function will lead to shorter code.

if someone nead to write a lot of very long conditions and he want to short the conditions then maybe it can be a good idea to use my function even if my function is have 44 lines.

but it is important to know that using my function can lead to a slowdown.

Edited by Guest
Link to comment
Share on other sites

So to make things shorter we "miss" in AutoIT the IN and ANY operator in IF expressions

and the EQ condition (in doubt if this is fully equal to IN operator)

if _IF($a,$b,$c,"eq", 4,4,4) = if _IF($a,$b,$c,"eq", 4) = if $a=4 and $b=4 and $c=4

if _IF($a,"in", $b,$c)          = if $a=$b or $a=$c

if _IF($a, $b, "any", $c,$d) = if $a=$c or $b=$d

if _IF($a, $b, "any", $c)      = if $a=$c or $b=$c

if _IF($a, "any", $c, $d)      = false although it could be rewritten as if $a=$c or $a=$d

 

Alternative function that handles constants and variables

only handles up to about 10 variables (easy to extend to more)

handles not all logical combinations

func _IF($v1 = 0, $v2 = 0, $v3 = 0, $v4 = 0, $v5 = 0, $v6 = 0, $v7 = 0, $v8 = 0, $v9 = 0, $v10 = 0, _
    $v11 = 0, $v12 = 0, $v13 = 0, $v14 = 0, $v15 = 0, $v16 = 0, $v17 = 0, $v18 = 0, $v19 = 0, $v20=0)
    #forceref $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9, $v10, $v11, $v12, $v13, $v14, $v15, $v16, $v17, $v18, $v19, $v20
    const $cOPin=1
    const $cOPeq=2
    const $cOPeqS=3
    const $cOPany=4
    const $cOPanyS=5

    Local $tValue    ;- Temporary value
    local $op     ;~ operator

;~  consolewrite("Pcount " & @numparams & @CRLF)
;~  for $i=1 to @NumParams
;~      consolewrite(" var " & $i & eval("v"&$i) & @CRLF)
;~  Next

    $tValue=stringupper(eval("v" & (@numparams-1)))
    $tMidValue=(@numparams/2) + 0.5
;~  consolewrite("Value is " & $tValue & @CRLF)
;~  consolewrite("MidValue is " & $tMidValue & @CRLF)
;~  consolewrite("modValue is " & mod(@numparams,2) & @CRLF)
    if $tValue="EQ" then $op=$cOPeqS
    if $tValue="ANY" then $op=$cOPanyS
    if stringupper($v2)="IN" Then $op=$cOPin
    if mod(@numparams,2)=1 Then
        $tValue=stringupper(eval("v" & $tMidValue))
        if $tValue="EQ" then $op=$cOPeq
        if $tValue="ANY" then $op=$cOPany
    EndIf

    switch $op
        case $op=$cOPin
            for $i=3 to @NumParams
                if eval("v"&$i)=$v1 then return True
            Next
        case $op=$cOPeqS
            $tVal1=eval("v"&@NumParams)
;~          consolewrite("Value 2 is" & $tValue & @CRLF)
            for $i=1 to @NumParams-2
                $tVal2=eval("v"&$i)
                if $tVal1=$tVal2 then return True
            Next
        case $op=$cOPeq
            for $i=1 to $tMidValue-1
                $tVal1=eval("v"&$i)
                $tVal2=eval("v"&$i+$tMidValue)
;~              consolewrite($i & "Value EQ compare is [" & $tVal1 & "][" & $tVal2 & "]" & @CRLF)
                if $tVal1<>$tVal2 then return false
            Next
            return True
        case $op=$cOPany
            for $i=1 to $tMidValue-1
                $tVal1=eval("v"&$i)
                $tVal2=eval("v"&$i+$tMidValue)
;~              consolewrite($i & "Value ANY compare is [" & $tVal1 & "][" & $tVal2 & "]" & @CRLF)
                if $tVal1=$tVal2 then return true
            Next
            return false
        case $op=$cOPanyS
            $tVal2=eval("v"&@NumParams)
            for $i=1 to @NumParams-2
                $tVal1=eval("v"&$i)
;~              consolewrite($i & "Value ANY compare is [" & $tVal1 & "][" & $tVal2 & "]" & @CRLF)
                if $tVal1=$tVal2 then return true
            Next
            return false
    endswitch

    return false
EndFunc

some examples of usage

$read1=4
$read2=4
$read3=4
$read4=4
$read5=4
$read6=4

$terminatekey="OFF"
if _IF($terminatekey, "in", "OFF","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12") Then
    consolewrite("Yes we have a match" & @CRLF)
else
    consolewrite("Yes we have NOT a match" & @CRLF)
EndIf

if _IF($read1,$read2,$read3, "EQ", 4) Then
    consolewrite("Yes we have a match" & @CRLF)
Else
    consolewrite("Yes we have NOT a match" & @CRLF)
EndIf

if _IF($read1,$read2,$read3, "EQ", 4,4,4) Then
    consolewrite("Yes we have a match" & @CRLF)
Else
    consolewrite("Yes we have NOT a match" & @CRLF)
EndIf

if _IF($read1,$read2,$read3, "EQ", 4,5,6) Then
    consolewrite("Yes we have a match" & @CRLF)
Else
    consolewrite("Yes we have NOT a match" & @CRLF)
EndIf

if _IF($read1,$read2,$read3, "ANY", 4,5,6) Then
    consolewrite("Yes we have a match" & @CRLF)
Else
    consolewrite("Yes we have NOT a match" & @CRLF)
EndIf
if _IF($read1,$read2,$read3, "ANY", 4) Then
    consolewrite("Yes we have a match" & @CRLF)
Else
    consolewrite("Yes we have NOT a match" & @CRLF)
EndIf
Exit

and i learned that EVAL function does not recognize/need the $ sign when evaluating variables

Edited by junkew
Link to comment
Share on other sites

BTW I question the need to test that many variables/conditions in any single real-world test (barring extremely poor code structure).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Junkew good job. I also thought that i missed another operators.

I guess you did not write this function if I did not I think of that idea. :)

A few points:

  •  What about those operators - "<" , ">" , "<>" ?it will be good if you also add those operators..
  • I would prefer to call the operators  with regular names.. i mean that i prefer "=" then "eq" "or" then "in" ..
  • There is a scenario in which the use of your function would be problematic.
    if the user have the following group of words that he need to check and in that group one of the words: "in" , "any" , "eq" are included in his group then how do you think he can write his condition with your function? for example let's assume the user has to write to write this condition:
    if "abc" = $var1 or "in" = $var2
    then.
    So write it in your way:
    _IF("abc","in","in",$var1,$var2)
    is problematic and I do not need to explain why. You see it yourself.
  • What if the user decided to call to the operators as variable? for example he can create $Operator variable that can be in one case "in" and in other case "eq" so if he writes that then it can look like this:
    _IF($aVar1,$aVar2,$aVar4,$aVar5,$Operator,$bVar1,bVar2,bVar3,bVar4,bVar5)
    to me it seems a bit hard to read especially if the condition will be very long.

 

so i think that you should place the operator at the start or at the end of your function as i did.

good work

Junkew good job. I also thought that i missed another operators.

I guess you did not write this function if I did not I think of that idea. :)

A few points:

  •  What about those operators - "<" , ">" , "<>" ?it will be good if you also add those operators..
  • I would prefer to call the operators  with regular names.. i mean that i prefer "=" then "eq" "or" then "in" ..
  • There is a scenario in which the use of your function would be problematic.
    if the user have the following group of words that he need to check and in that group one of the words: "in" , "any" , "eq" are included in his group then how do you think he can write his condition with your function? for example let's assume the user has to write to write this condition:
    if "abc" = $var1 or "in" = $var2
    then.
    So write it in your way:
    _IF("abc","in","in",$var1,$var2)
    is problematic and I do not need to explain why. You see it yourself.
  • What if the user decided to call to the operators as variable? for example he can create $Operator variable that can be in one case "in" and in other case "eq" so if he writes that then it can look like this:
    _IF($aVar1,$aVar2,$aVar4,$aVar5,$Operator,$bVar1,bVar2,bVar3,bVar4,bVar5)
    to me it seems a bit hard to read especially if the condition will be very long.

 

so i think that you should place the operator at the start or at the end of your function as i did.

good work

 

Holy macaroni guys...with all THESE codes...we build ourselfes a new WINDOWS 9 here! LMAO :zorro:

PS: thx anyways for all your input :-)

without people like him, functions as BitOr, BitAnd was not exist.

 

BTW I question the need to test that many variables/conditions in any single real-world test (barring extremely poor code structure).

Our brain uses a lot of very long variables/conditions.

so yes, ther is a such machine in the real world

and if some one(I'm not the one) want to write a code for complicated robat with autoit then yes, he would have to write very long conditions and a lot of conditions

Edited by Guest
Link to comment
Share on other sites

  • fun part on this function is that you have on left hand side of operator 1,n variables and also on the right hand side.you have 1,n variables as a possibility (and yes this can lead to complex scenarios)
  • operators in beginning or the end is a matter of taste you could also use fortran operators with a dot before and after like .EQ., .NEQ.,, made the function in such a way you can choose yourself what you want
  • If i put the operator in the beginning as first variable like _IF("=",a,b,c,d) then the intention could be if a=b and a=c and a=d then ... but also could mean if a=c and b=d then .... or means if a=d and b=d and c=d then ....

 

I am still in doubt if I like this syntax yes/no as it becomes very confusing when and logic is better than or logic or in which way the combinations should work

_if(a,b,"=",c,d) could be defined as

1. if a=c and b=d then ...

2. if a=c and a=d and b=c and b=d then ...

but 

_if(a,b,c, "=", d, e) it actually is equal to

if a=d and a=e and b=d and b=e and c=d and c=e

_if(a,b,c, "ANY", d, e) it actually is equal to

if a=d or a=e or b=d or b=e or c=d or c=e

 

 

These on the other hand are easier then writing it out

_if(a,b,"=", 4) looks easy enough

_if(a,"in", "F1","F2") looks easy enough

 

ok new function (needs more testing to see if logic is good enough)

supporting "=", "<>", "!=", ">","<",">=","<=","IN", "ANY", "BETWEEN"

func _IF($v0 = 0, $v1 = 0, $v2 = 0, $v3 = 0, $v4 = 0, $v5 = 0, $v6 = 0, $v7 = 0, $v8 = 0, $v9 = 0, $v10 = 0, _
    $v11 = 0, $v12 = 0, $v13 = 0, $v14 = 0, $v15 = 0, $v16 = 0, $v17 = 0, $v18 = 0, $v19 = 0)
    #forceref $v0, $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9, $v10, $v11, $v12, $v13, $v14, $v15, $v16, $v17, $v18, $v19
    const $cOPeq=1, $cOPne=2, $cOPgt=3, $cOPlt=4, $cOPge=5, $cOPle=6, $cOPin=7, $cOPany=8, $cOPbetween=9
    local $operators[10] = ["=", "<>", "!=", ">","<",">=","<=","IN", "ANY", "BETWEEN"]
    local $opconst[10]   = [ 1 , 2   ,    2, 3  ,  4, 5  ,  6 ,  7 ,     8,     9]
    local $opConstant
    local $v[20]
    local $i, $j

;~  consolewrite("Pcount " & @numparams & @CRLF)
;~  for $i=1 to @NumParams
;~      consolewrite(" var " & $i & eval("v"&$i) & @CRLF)
;~  Next

;~  So first determine the operator and put all variables in an array for easier manipulation
    $operator=-1
    for $i=0 to 19
        $v[$i]=eval("v" & $i)
        if $operator = -1 Then
            for $j=0 to ubound($operators)-1
                if stringupper($v[$i])=stringupper($operators[$j]) Then
                    $operatorLoc=$i
                    $operator=stringupper($operators[$j])
                    $opconstant=$opConst[$j]
                    exitloop
                EndIf
            next
        endif
    Next

;~  Determine the middle one
    $tMidValue=(@numparams/2) - 0.5

    switch $opconstant
        case $cOPeq, $cOPne, $cOPgt, $cOPlt, $cOPge, $cOPle,$cOpIn, $cOpIn

        ;~  If the operator is in the middle do an a=c and b=d comparison (n vars on the left compared with n vars on the right)
            if ($tMidValue=$operatorloc) Then
                for $i=0 to $operatorloc-1
                    $tVal1=$v[$i]
                    $tVal2=$v[$i+$tMidValue+1]
                    consolewrite($i & " Value compare is [" & $tVal1 & "][" & $tVal2 & "]" & $opconstant & $operator & @TAB)
                    $retval=True
                    switch $opconstant
                        case $cOPeq
                            if $tVal1<>$tVal2 then return false
                        case $cOPne
                            if $tVal1=$tVal2 then return false
                        case $cOPgt
                            if $tVal1<=$tVal2 then return false
                        case $cOPlt
                            if $tVal1>=$tVal2 then return false
                        case $cOPge
                            if $tVal1<$tVal2 then return false
                        case $cOPle
                            if $tVal1>$tVal2 then return false
                        case $cOPin
                            $retval=False
                            for $j=$operatorloc+1 to @numparams-1
                                $tVal2=$v[$j]
                                if $tVal1=$tVal2 then return true
                            next
                        case $cOPany
                            $retval=False
                            if $tVal1=$tVal2 then return true
                    endswitch
                Next
                return $retVal
            Else
                for $i=0 to $operatorloc-1
                    for $j=$operatorloc+1 to @numparams-1
                        $tVal1=$v[$i]
                        $tVal2=$v[$j]
                        consolewrite($i & $j & " Value compare is [" & $tVal1 & "][" & $tVal2 & "]" & $opconstant &$operator&  @TAB)
                        $retval=True
                        switch $opconstant
                            case $cOPeq
                                if $tVal1<>$tVal2 then return false
                            case $cOPne
                                if $tVal1=$tVal2 then return false
                            case $cOPgt
                                if $tVal1<=$tVal2 then return false
                            case $cOPlt
                                if $tVal1>=$tVal2 then return false
                            case $cOPge
                                if $tVal1<$tVal2 then return false
                            case $cOPle
                                if $tVal1>$tVal2 then return false
                            case $cOPin
                                $retVal=false
                                if $tVal1=$tVal2 then return true
                            case $cOPany
                                $retVal=false
                                if $tVal1=$tVal2 then return true
                        endswitch
                    Next
                Next
                return $retval
            EndIf
        case $cOPbetween
            for $i=0 to $operatorloc-1
                $tVal1=$v[$i]
                $tVal2=$v[$operatorloc+1]
                $tVal3=$v[$operatorloc+2]
                consolewrite($i & $j & " Value compare is [" & $tVal1 & "][" & $tVal2 & "]" & "][" & $tVal3 & "]" & $opconstant &$operator&  @TAB)
                if not (($tval1>=$tval2) and ($tval1 <=$tval3)) then return False
            next
            return true
    EndSwitch

    return false
EndFunc

and some testing code / examples

$read1=4
$read2=4
$read3=4
$read4=4
$read5=4
$read6=4

$terminatekey1="F15"
$terminatekey2="OFF"
$terminatekey3="F3"

_unitTest_IF("TC1a in ",_IF($terminatekey1,$terminatekey2, "in", "OFF","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12"))
_unitTest_IF("TC1b in ",_IF($terminatekey1, "in", "OFF","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12"))
_unitTest_IF("TC1c in ",_IF($terminatekey3, "in", "OFF","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12"))
_unitTest_IF("TC2a = ",_IF($read1,$read2,$read3, "=", 4) )
_unitTest_IF("TC2b = ",_IF($read1,$read2,$read3, "=", 4,4,4))
_unitTest_IF("TC2c = ",_IF($read1,$read2,$read3, "=", 4,5,6))
_unitTest_IF("TC2d = ",_IF($read1,$read2,$read3, "=", 4,5,6))
_unitTest_IF("TC2e = ",_IF($read1,$read2,$read3, "=", 4) )

_unitTest_IF("TC2f = ",_IF($read1, $read2, "=",4,  $read3))
_unitTest_IF("TC2g = ",_IF($read1, "=",4, $read2, $read3))

_unitTest_IF("TC2h = ",_IF(1, 2, 3, "=",4))
_unitTest_IF("TC2i = ",_IF(1, 2, "=", 1,5))
_unitTest_IF("TC2j = ",_IF(1, "=",1, 1, 1))

_unitTest_IF("TC3a = ",_IF(1, 2, 3, "!=",1,2,3))
_unitTest_IF("TC3b = ",_IF(1, 2, "!=", 1,5))
_unitTest_IF("TC3c = ",_IF(1, "!=",1, 1, 1))

_unitTest_IF("TC3d = ",_IF(1, 2, 3, "!=",4))
_unitTest_IF("TC3e = ",_IF(1, 2, "!=", 1,5))
_unitTest_IF("TC3f = ",_IF(1, "!=",1, 1, 1))

_unitTest_IF("TC3g = ",_IF(1, "!=",1, 1, 1))

_unitTest_IF("TC4a = ",_IF(2,3, "between",1, 4))
_unitTest_IF("TC4b = ",_IF(2,3, "between",1, 1))
_unitTest_IF("TC4c = ",_IF(2,3, "between",1, 2))
_unitTest_IF("TC4d = ",_IF(2, "between",1, 3))

;~ _unitTest_IF("TC2 eq ",
;~ _unitTest_IF("TC2 eq ",

func _unitTest_IF($t,$v)
    if $v Then
        consolewrite(@CRLF & $t & "TRUE  " &  @CRLF)
    else
        consolewrite(@CRLF & $t & "FALSE " & @CRLF)
    EndIf
EndFunc

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