Jump to content
Sign in to follow this  
JohnBailey

Best way to do ORs

Recommended Posts

JohnBailey

I don't really know how to search examples of ORs in the forums (seeing as OR is only two chrs long)

I'm not sure I have the syntax correct, also it looks convoluted.

If $lineArray[1] = 026468 OR 123457 OR 462134 OR 45342725 OR 22316 OR 7004689 OR 000144 OR 8880941 OR 1324647 OR 999878884 OR 46561667 _
            OR 4947997 OR 494897892 OR 92843745 OR 45345312 Then
etc.

It doesn't check beyond the first one

MODs: I'm sorry. Can you please move this thread. I didn't mean to put it in the GUI help. It will never be found

Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites
Jos

This is a workaround I use :

If StringInStr("|026468|123457|462134|45342725|22316|7004689|000144|8880941|1324647|999878884|46561667|4947997|494897892|92843745|45345312|","|" & $lineArray[1] & "|") Then

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
JohnBailey

This is a workaround I use :

If StringInStr("|026468|123457|462134|45342725|22316|7004689|000144|8880941|1324647|999878884|46561667|4947997|494897892|92843745|45345312|","|" & $lineArray[1] & "|") Then
ooooooooo I like that! :shocked:

oh and JdeB thank you for moving this thread! My apologies for the improper location - moving faster than I was reading.

Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites
jvanegmond

I like this method:

Switch $lineArray[1]
Case 026468, 123457, 462134, 45342725, 22316, 7004689, 000144, 8880941, 1324647, 999878884, 46561667, 4947997, 494897892, 92843745, 45345312

;do stuff here

EndSwitch

Share this post


Link to post
Share on other sites
JohnBailey
:shocked: ok geez guys both of these methods make sense and I think are MUCH better! However, any reasons why one is preferred over the other (really interested in reading your thoughts on that!) ?

A decision is a powerful thing

Share this post


Link to post
Share on other sites
jvanegmond

It is obvious that both methods work. The method I proposed is 3 times faster then the method JdeB proposed. This is due to the fact that the Switch command is specifically designed for this purpose, and StringInStr is not. I also find the long string to become very confusing, and it does not allow for quickly expanding it.

In short, if you want to be a neat programmer.. Go with Switch.

Test Script:

Dim $lineArray[1] = [ 462134 ]

$Time = TimerInit()

If StringInStr("|026468|123457|462134|45342725|22316|7004689|000144|8880941|1324647|999878884|46561667|4947997|494897892|92843745|45345312|","|" & $lineArray[0] & "|") Then
EndIf

$Time1 = TimerDiff($Time)

$Time = TimerInit()
Switch $lineArray[0]
Case 026468, 123457, 462134, 45342725, 22316, 7004689, 000144, 8880941, 1324647, 999878884, 46561667, 4947997, 494897892, 92843745, 45345312

;do stuff here

EndSwitch

$Time2 = TimerDiff($Time)

MsgBox(0, "Test Script", "StringInStr method: " & @TAB & $Time1 & @LF & "Switch method: " & @TAB & @TAB & $Time2 & @LF & @LF & "Switch speed gain: " & @TAB & @TAB & Round($Time1/$Time2,2))
Edited by Manadar

Share this post


Link to post
Share on other sites
JohnBailey

Manadar, VERY NICE!!

Thank you very much for the explanation! That's the stuff I'm wanting to know (the whys). I want my scripts to be versatile, quick, and "neat."

Thank you also for the example. I've never thought to do that before in testing the efficiency of a script.


A decision is a powerful thing

Share this post


Link to post
Share on other sites
tAKTelapis

BTW, when I do

$lineArray[1] = 026468 OR $lineArray[1] = 123457
etc.

It doesn't check beyond the first one

Just a thought, but if the first option rings true:

$lineArray[1] = 026468

Then why would it need to test the second? it stands to reason that as soon as any one of the OR statements were positive it would continue with whatever is required of it when that condition is met.

(unless autoIT still enumerates all possibilities before continuing ?).

To the others who actually helped.. i like the solutions, i dont think i have ever used the OR function of AutoIT for anything, but those are some creative work arounds, its great when you learn things when your not expecting to.

Share this post


Link to post
Share on other sites
JohnBailey

...its great when you learn things when your not expecting to.

I totally agree!

Just a thought, but if the first option rings true:

$lineArray[1] = 026468

Then why would it need to test the second? it stands to reason that as soon as any one of the OR statements were positive it would continue with whatever is required of it when that condition is met.

(unless autoIT still enumerates all possibilities before continuing ?).

My bad for the confusion there. It literally didn't check beyond the first one, so if 02468 was true or not it responded accordingly. Anything after 026468 was not even considered. In some cases, I can drop the additional $lineArray[1] = and it (or similar things) will work, but not in all cases. Although I really would like to understand what the proper syntax is (thus understanding too why it would work some times and not others), the other methods seem much better. The method posted Manadar really is faster too!

A decision is a powerful thing

Share this post


Link to post
Share on other sites
Sokko

The "proper syntax" is to put the full equation ($lineArray[1] = 026468) for each condition that you want to check. The only reason you're allowed to do things like "If $lineArray[1] = 026468 OR 123457 Then..." is because the condition "123457" (this is taken as a condition by itself) is converted to a true/false value, which in this case will be true, like any non-zero number.

Share this post


Link to post
Share on other sites
JohnBailey

The "proper syntax" is to put the full equation ($lineArray[1] = 026468) for each condition that you want to check. The only reason you're allowed to do things like "If $lineArray[1] = 026468 OR 123457 Then..." is because the condition "123457" (this is taken as a condition by itself) is converted to a true/false value, which in this case will be true, like any non-zero number.

wholly smokes! wait wait let me get this totally straight. If (and a big if after reading everything in here) I was to use OR and numbers it would have to employ "" around the number?

I just never considered that 123457 would be taken into a binary form. I'm not totally sure I understand why, but you popped the lid for sure. I need to ponder more.

thanks

Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites
Sokko

Nonono! I just used quotes to provide emphasis. You would NOT use quotes when utilizing numbers in a condition. My point was that if the entire condition is just a number, it gets converted to a true/false value. Like if you said "If 123457 Then", that statement would always be true. I repeat, there are NO quotes involved whatsoever. My quotes were to indicate which parts of the post were code.

Share this post


Link to post
Share on other sites
JohnBailey

OH! shoot my bad totally I read into that wrong. Thank you very much for clarifying! I wouldn't use this method anymore, but I'm really glad I'm getting a stronger understanding of how and why things work in autoit.

Basically, autoit converts numbers into true/false (binary) value when running conditions.

I get now that what you wrote was an emphasis, and I thank you more so for that.

The "proper syntax" is to put the full equation ($lineArray[1] = 026468) for each condition that you want to check.

This is the important part of the "proper syntax" Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites
Sokko

Basically, autoit converts numbers into true/false (binary) value when running conditions.

Well, when you get right down to it, all conditions are converted to a binary value. Take this line:

If $var = 026468 or 123457 ThenoÝ÷ Ù8b±Ú²z-槪ê-7êö«

And of course that will pass the test, because one of the values is true.

Share this post


Link to post
Share on other sites
JohnBailey

OH WOW!!! crystal clear!! Thank you thank you!

Goodness, that's good to know. I see now why it would always execute. I was thinking in English instead of script, but I didn't know the script. Thank you for explaining that; seriously that helped me really understand how the script is "thinking."

: a thank you bow :

Sokko, what you wrote above is the sort of thing that would be wonderful to have in the manual. Of course, this may be "common sense" for most people. :shocked:

Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites
SadBunny

Just a thought, but if the first option rings true:

$lineArray[1] = 026468

Then why would it need to test the second? it stands to reason that as soon as any one of the OR statements were positive it would continue with whatever is required of it when that condition is met.

(unless autoIT still enumerates all possibilities before continuing ?).

To the others who actually helped.. i like the solutions, i dont think i have ever used the OR function of AutoIT for anything, but those are some creative work arounds, its great when you learn things when your not expecting to.

Ok, you ask why would it need to test the second condition... But, it doesn't! Check out this code:

If 5=5 Or MsgBox(4,"test","Show the test message?") = 6 Then
    MsgBox(0,"test","here is your test message!")
EndIf

Since 5=5 returns True, the second condition (which shows a msgbox with a question) is never executed since the result does not matter, and the script goes ahead and shows the final msgbox. This is done to speed up script execution.

(If you change 5=5 to 3=5 returning False instead of true, the question msgbox will be shown because THEN the second result suddenly does matter.)


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
Sokko

Sokko, what you wrote above is the sort of thing that would be wonderful to have in the manual. Of course, this may be "common sense" for most people. :shocked:

For better or for worse, the manual makes no effort to teach general programming concepts. It is just that, a reference manual. If you are already a programmer and you know some other language, the manual makes it fun and easy for you to learn AutoIt. But if AutoIt is your first programming language, simply reading the manual will not allow you to really understand what's going on when you write the code (which is why many newcomer questions are basically "How do I change the example from the help files so it does what I want"). Nobody has yet written a book that attempts to teach AutoIt as a first programming language, so for now you are stuck with us. :(

Share this post


Link to post
Share on other sites
JohnBailey

For better or for worse, the manual makes no effort to teach general programming concepts. It is just that, a reference manual. If you are already a programmer and you know some other language, the manual makes it fun and easy for you to learn AutoIt. But if AutoIt is your first programming language, simply reading the manual will not allow you to really understand what's going on when you write the code (which is why many newcomer questions are basically "How do I change the example from the help files so it does what I want"). Nobody has yet written a book that attempts to teach AutoIt as a first programming language, so for now you are stuck with us. :(

haha :P Well, I like you guys better anyway! I was just saying what I said (odd sentence). I know why stuff like that is not inlcuded. While this is not my first scripting language, I am lacking some 'low level' understanding. I really like the 'low level' understanding. Wikipedia helps me most of the time. However, there are also many times where it can't help worth crud. :shocked: I like asking here anyway, because I hope it helps other scripters/programmers. Moreover, the replies from everyone really get some great discussion going (well in my opinion). It's really cool to 'stand on the sholders of giants' or at least listen to their conversations.

Thank you!


A decision is a powerful thing

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  

×