Jump to content
Sign in to follow this  
sackjarrow

Help formatting simple "if" logic

Recommended Posts

sackjarrow

Hi,

 
If $aLine[2] = "" or
   $aLine[2] = "Ingen" or
   $aLine[2] = "//www.powertech.no/cgi-bin/mailform" or
   $aLine[2] = "Send" or
   $aLine[2] = "none" or 
   $aLine[2] = "off" Then _ArrayDelete($aBody, $i)

 The above code gives me error ""If" statements must have a "Then" keyword.". Whereas if I type it on a single line..:
 

If $aLine[2] = "" or $aLine[2] = "Ingen" or $aLine[2] = "//www.powertech.no/cgi-bin/mailform" or $aLine[2] = "Send" or $aLine[2] = "none" or $aLine[2] = "off" Then _ArrayDelete($aBody, $i)

 
..it works perfectly fine. This is just an aesthetic issue, but it's much easier to work with the formatting in case 1 since I'll be adding a lot of "or's".

What's the best way to go about this?

Share this post


Link to post
Share on other sites
somdcomputerguy

The Switch conditional statement would probably be better to use here, the underline character (as posted above) would work fine too. Refer to the Help file for details
 


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites
sackjarrow

Thanks, that underscore solved a lot of problems!
 
About Switch I'm not so sure since I think I would have to state " _ArrayDelete($aBody, $i)" for every case which would be kinda tiresome. e.g:
 

Switch $aLine[2]
    Case """"
        _ArrayDelete($aBody, $i)
    Case "Ingen"
        _ArrayDelete($aBody, $i)
    Case "Send"
        _ArrayDelete($aBody, $i)
;etc. etc.
EndSwitch

..or am I just using Switch wrong?

Share this post


Link to post
Share on other sites
BrewManNH

Switch $aLine[2]
    Case """", "Ingen", "Send"
        _ArrayDelete($aBody, $i)
;etc. etc.
EndSwitch

You can put multiple tests on a Case statement for Switch, separated by commas. As long as they all are going to do the same thing if matched, you can do it all on one line.


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
sackjarrow

Cool. But since I am going for an organized structure I'd prefer having everything on separated lines. So I might aswell go for "if" with underscores:

If $aLine[2] = "" or _
   $aLine[2] = "Ingen" or _
   $aLine[2] = "//www.powertech.no/cgi-bin/mailform" or _
   $aLine[2] = "Send" or _
   $aLine[2] = "none" or _
   $aLine[2] = "off" Then _ArrayDelete($aBody, $i)

I'm just trying to evaluate why somdcomputerguy said it would be better to use "switch".

Share this post


Link to post
Share on other sites
kylomas
I'm just trying to evaluate why somdcomputerguy said it would be better to use "switch".

 

BrewmanNH just showed you.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
BrewManNH

Switch is usually faster than If statements. You can put everything on one line without a lot of Ors.

EDIT: You can also use the continuation character in the case statement of a switch.

Switch $aLine[2]
    Case """", _
            "Ingen", _
            "Send"
        _ArrayDelete($aBody, $i)
        ;etc. etc.
EndSwitch
Edited by BrewManNH

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
somdcomputerguy

I know that Switch is a bit faster than multiple If's, but I meant 'better' in the 'aesthetic', 'easier to read code' case. In my mind, I was seeing something similar to BrewmanNH's post (#5).


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites
sackjarrow

Ah yes you are correct, now I see. By using switch in this case I save typing "$aLine[2] =" for every condition. And when using continuation characters I can get everything on separated lines as well for nice clean structure.Thanks!  :thumbsup:

Sack

Share this post


Link to post
Share on other sites
somdcomputerguy

:)  :graduated:  :)


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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  

×