Sign in to follow this  
Followers 0
MariusN

Is there a "shorter" way?

37 posts in this topic

Hi guys...Is there a shorter way of coding the following code --->

If $read1 = 4 And $read2 = 4 And $read3 = 4 And $read4 = 4 And $read5 = 4 And $read6 = 4 Then

...tia

Share this post


Link to post
Share on other sites



If you have to check 4 variables you have to check 4 variables.

No shorter way I can think of.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

MariusN,

Depending on the possible values of the variables you might be able to use:

If ($read1 + $read2 + $read3 + $read4 + $read5 + $read6) = 24 Then
Obviously that will only work if the values are always numeric AND the value of each variable cannot be more than 4. ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Depending on the script $read could be an array.

Global $read[6]
; ... code to set values here
$fCondition = True
For $i = 0 to 5
    If $read[$i] <> 4 then
        $fCondition = False
        Exitloop
    EndIf
Next
If $fCondition then
    ; Code here
EndIf

Of course that's not shorter...

Edited by DatMCEyeBall
1 person likes this

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Share this post


Link to post
Share on other sites

i also want shorter way for this:

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Global $sTerminateKeys[13] = ; Keys here
For $i = 0 To 12
    If $TerminateKey = $sTerminateKeys[$i] Then
        ; Do something
        Exitloop
    EndIf
Next

Same solution as above.

Edited by DatMCEyeBall

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Share this post


Link to post
Share on other sites

more shorter way :) :

this is my Function

; Example 1
If _IfAndOr("a","a|a|a|a","and") = 1 Then ; This equal to - if a = a And a = a And a = a And a = a
    MsgBox(0,"","Condition exist")
Else
    MsgBox(0,"","Condition Not exist")
EndIf

; Example 2
If _IfAndOr("a","a|a|a|b","and") = 1 Then ; This equal to - if a = a And a = a And a = a And a = b
    MsgBox(0,"","Condition exist")
Else
    MsgBox(0,"","Condition Not exist")
EndIf

; Example 3
If _IfAndOr("a","a|b|c|d","or") = 1 Then ; This equal to - if a = a Or a = b Or a = c Or a = b
    MsgBox(0,"","Condition exist")
Else
    MsgBox(0,"","Condition Not exist")
EndIf


Func _IfAndOr($MainVariable,$Variables,$mode)
    Local $Output = 0 , $count = 0
    $var = StringSplit($Variables,"|",1)
    For $a = 1 To $var[0]
        If $mode = "or" Then
            If $MainVariable = $var[$a] Then
                $Output = 1
                ExitLoop
            EndIf
        ElseIf $mode = "and" Then
            If $MainVariable = $var[$a] Then $count = $count+1
            If $count = $var[0] Then $Output = 1
        EndIf
    Next
    Return $Output
EndFunc

Share this post


Link to post
Share on other sites

$TerminateKey = "F10"
MsgBox(0, $TerminateKey, _IsTerminateKeyOK($TerminateKey))

$TerminateKey = "OFF"
MsgBox(0, $TerminateKey, _IsTerminateKeyOK($TerminateKey))

$TerminateKey = "F13"
MsgBox(0, $TerminateKey, _IsTerminateKeyOK($TerminateKey))

Func _IsTerminateKeyOK($trmKey)
    Switch $trmKey
        Case "OFF", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"
            Return True
    EndSwitch
    Return False
EndFunc

;================================================

dim $Read[7] = [0, 4, 4, 4, 4, 4, 4];$Read[1] = 4, $Read[2] = 4, $Read[3] = 4, $Read[4] = 4, $Read[5] = 4, $Read[6] = 4
MsgBox(0, 'Array equal to 4', _IsReadArrayEqualTo($Read, 4))

Func _IsReadArrayEqualTo($theArray, $theNumber)
    for $i = 1 to UBound($theArray)-1
        if $theArray[$i] <> $theNumber Then Return False
    Next
    Return True
EndFunc

Share this post


Link to post
Share on other sites

Hi guys...Is there a shorter way of coding the following code --->

If $read1 = 4 And $read2 = 4 And $read3 = 4 And $read4 = 4 And $read5 = 4 And $read6 = 4 Then

...tia

 

If by "shorter" you mean "easier to shoot myself in the foot", then yes there are a fair number of ways.

Among them:

If $read1 & $read2 & $read3 & $read4 & $read5 & $read6 = "444444" Then

But of course there are many other ways to setup creative bugs.

2 people like this

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)

Share this post


Link to post
Share on other sites

for you:

; Example 1
Local $read1 = 4 , $read2 = 4 , $read3 = 4 , $read4 = 1 , $read5 = 4
If _IfAndOr(4,$read1&"|"&$read2&"|"&$read3&"|"&$read4&"|"&$read5,"and") = 1 Then ; This equal to - if a = a And a = a And a = a And a = a
    MsgBox(0,"","Condition exist")
Else
    MsgBox(0,"","Condition Not exist")
EndIf

; Example 2
Local $read1 = 4 , $read2 = 4 , $read3 = 4 , $read4 = 4 , $read5 = 4
If _IfAndOr(4,$read1&"|"&$read2&"|"&$read3&"|"&$read4&"|"&$read5,"and") = 1 Then ; This equal to - if a = a And a = a And a = a And a = a
    MsgBox(0,"","Condition exist")
Else
    MsgBox(0,"","Condition Not exist")
EndIf


Func _IfAndOr($MainVariable,$Variables,$mode)
    Local $Output = 0 , $count = 0
    $var = StringSplit($Variables,"|",1)
    For $a = 1 To $var[0]
        If $mode = "or" Then
            If $MainVariable = $var[$a] Then
                $Output = 1
                ExitLoop
            EndIf
        ElseIf $mode = "and" Then
            If $MainVariable = $var[$a] Then $count = $count+1
            If $count = $var[0] Then $Output = 1
        EndIf
    Next
    Return $Output
EndFunc
1 person likes this

Share this post


Link to post
Share on other sites

is the intention really shorter or more readable?

if $read1=4 and _
   $read2=4 and _
   $read3=4 and _
   $read4=4 and _
   $read5=4 _
Then
     consolewrite("All are four")
EndIf

for the functionkeys you just could do

$TerminateKey="F111"

if stringregexp($TerminateKey,"(OFF)|(F[0-9])",0)=1 Then
    consolewrite("We matched 1" & @CRLF)
EndIf

If stringinstr("OFF,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12", $TerminateKey) > 0 Then
    consolewrite("We matched 2" & @CRLF)
EndIf

I do not know if AutoIT can handle matrix comparison directly (for sure it will not be shorter

http://www.extremeoptimization.com/QuickStart/CSharp/MatrixVectorOperations.aspx

1 person likes this

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

is the intention really shorter or more readable?

if $read1=4 and _
   $read2=4 and _
   $read3=4 and _
   $read4=4 and _
   $read5=4 _
Then
     consolewrite("All are four")
EndIf

for the functionkeys you just could do

$TerminateKey="F111"

if stringregexp($TerminateKey,"(OFF)|(F[0-9])",0)=1 Then
    consolewrite("We matched 1" & @CRLF)
EndIf

If stringinstr("OFF,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12", $TerminateKey) > 0 Then
    consolewrite("We matched 2" & @CRLF)
EndIf

I do not know if AutoIT can handle matrix comparison directly (for sure it will not be shorter

http://www.extremeoptimization.com/QuickStart/CSharp/MatrixVectorOperations.aspx

 

Thank you.

But I developed myself this solution:

$TerminateKey="F6"
If _IfAndOr($TerminateKey,"OFF|F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12","or") = 1 Then
    MsgBox(0,"","Condition exist")
Else
    MsgBox(0,"","Condition Not exist")
EndIf


Func _IfAndOr($MainVariable,$Variables,$mode)
    Local $Output = 0 , $count = 0
    $var = StringSplit($Variables,"|",1)
    For $a = 1 To $var[0]
        If $mode = "or" Then
            If $MainVariable = $var[$a] Then
                $Output = 1
                ExitLoop
            EndIf
        ElseIf $mode = "and" Then
            If $MainVariable = $var[$a] Then $count = $count+1
            If $count = $var[0] Then $Output = 1
        EndIf
    Next
    Return $Output
EndFunc

It works great

Edited by gil900

Share this post


Link to post
Share on other sites

But that doesn't come close to answering the question of making a one liner shorter. Your code turned making toast in a toaster to making toast with something that Rube Goldberg would have shuddered to contemplate.

1 person likes this

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

Share this post


Link to post
Share on other sites
if bitor($read1,$read2,$read3,$read4,$read5,$read6)=4 Then
consolewrite("Yes its four")
endIf

A good example about how to shoot yourself in the foot.

Make $read1 = 4, $read2 to $read6 = 0 and you're set :


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)

Share this post


Link to post
Share on other sites

Ok thx guys...i came to the conclusion the one i used IS the shortest way, lol

Thx for all the replies and help though :sweating:

Share this post


Link to post
Share on other sites

But that doesn't come close to answering the question of making a one liner shorter. Your code turned making toast in a toaster to making toast with something that Rube Goldberg would have shuddered to contemplate.

 

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

?

and this line (Suggested by junkew) :

If stringinstr("OFF,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12", $TerminateKey) > 0 Then

is a bad Idea. At first it seemed a good idea. But on second thought, it will also work when $TerminateKey = "," or

$TerminateKey = "OFF,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12"

or $TerminateKey = "F" or $TerminateKey = Number from 1 to 12

or $TerminateKey = "F10,F11" ..

a lot of bugs can Happen with this line.

Share this post


Link to post
Share on other sites

MariusN,

Depending on the possible values of the variables you might be able to use:

If ($read1 + $read2 + $read3 + $read4 + $read5 + $read6) = 24 Then
Obviously that will only work if the values are always numeric AND the value of each variable cannot be more than 4. ;)

M23

 

...good one...thx Melba

Share this post


Link to post
Share on other sites

Hi guys...Is there a shorter way of coding the following code --->

If $read1 = 4 And $read2 = 4 And $read3 = 4 And $read4 = 4 And $read5 = 4 And $read6 = 4 Then

...tia

 

Yes!,  there is a way to shorten:

If $read1=4 And $read2=4 And $read3=4 And $read4=4 And $read5=4 And $read6=4 Then

remove the spaces before and after the = sign ....... :idiot:

:muttley:

1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

MariusN,

Depending on the possible values of the variables you might be able to use:

If ($read1 + $read2 + $read3 + $read4 + $read5 + $read6) = 24 Then
Obviously that will only work if the values are always numeric AND the value of each variable cannot be more than 4. ;)

M23

 

 

another similar way without the need to check the sum:

if ($read1 = 4) * ($read2 = 4) * ($read3 = 4) * ($read4 = 4) * ($read5 = 4) * ($read6 = 4) Then
1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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