Sign in to follow this  
Followers 0
storme

Eliminate multiple IFs

18 posts in this topic

I've got a script where I'm checking input and if the first X characters = "Something" I do "Something".

The usual thing you'll find in many scripts.

The usuall way I handle this is with Select CASE or IF's if it isn't too big.

But it can be a problem when you mis count with a Stringleft....SO.....

What do you think would be better.

Local $selectArray[3][2] = [["fred", "fredfunc()"],["willma", 'ConsoleWrite("Willam ran from fred" & @cr)'],["Betty", 'BettyFunc("Barney")']]

    local $inputString = "Willma Flintstone"
    ConsoleWrite('$inputString = ' & $inputString & @crlf)

    for $index = 0 to ubound($selectArray) - 1
        ConsoleWrite("Index = " & $index & '$selectArray[$index][0] = ' & $selectArray[$index][0] & @crlf)

        If StringLeft($inputString, StringLen($selectArray[$index][0])) = $selectArray[$index][0] Then
            ConsoleWrite("FOUND YOU" & @cr)
            execute($selectArray[$index][1])
            exitloop
        EndIf
    next

Func fredfunc()
    ConsoleWrite("FRED ran" & @cr)
EndFunc

Func BettyFunc($husband)
    ConsoleWrite("Betty ran" & @cr)
EndFunc

or

local $inputString = "Willma Flintstone"
    ConsoleWrite('$inputString = ' & $inputString & @crlf)

    select
        case StringLeft($inputString, 4) = "fred"
            fredfunc()
        Case StringLeft($inputString, 4) = "willma"
            ConsoleWrite("Willam ran from fred" & @cr)
        Case StringLeft($inputString, 4) = "Betty"
            BettyFunc("Barney")
    EndSelect

Func fredfunc()
    ConsoleWrite("FRED ran" & @cr)
EndFunc

Func BettyFunc($husband)
    ConsoleWrite("Betty ran" & @cr)
EndFunc

Each piece of code should do exactly that same thing. But which is better (please give support for your position...Thank you!)

I know the obvious limitation is that Execute can only evaluate one line of code but that is easy to get around.

AND in this small example the Select case is smaller.

But is there anything that makes one better than the other?

Thanks for your replies.

John Morrison

AKA

Storme-E

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Choose what you suck at:

- telling jokes

- AutoIt

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I would do it this way using a switch case statement providing you always use the same number of characters from the start of the string otherwise I'd use select case.  Both methods will be much easier to follow the flow of the code and quicker. The only time I would use the loop method is if the list of options got ridiculously long (100s) 

local $inputString = "Willma Flintstone"
    ConsoleWrite('$inputString = ' & $inputString & @crlf)

    Switch StringLeft($inputString, 4)
        case "fred"
            fredfunc()
        Case "will"
            ConsoleWrite("Willam ran from fred" & @cr)
        Case "Bett"
            BettyFunc("Barney")
    EndSwitch

Func fredfunc()
    ConsoleWrite("FRED ran" & @cr)
EndFunc

Func BettyFunc($husband)
    ConsoleWrite("Betty ran" & @cr)
EndFunc
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Your code is wrong too.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Choose what you suck at:

- telling jokes

- AutoIt

For an MVP you arn't very Valuable or usefull.

Why do you spend so much of your energy attacking people?

Why not actually live up to the MVP title and HELP someone in stead of going out of your way to try and HURT People?

Share this post


Link to post
Share on other sites

I would do it this way using a switch case statement providing you always use the same number of characters from the start of the string otherwise I'd use select case.  Both methods will be much easier to follow the flow of the code and quicker. The only time I would use the loop method is if the list of options got ridiculously long (100s) 

Yep that is what I thought.

I just got anoyed as I missed a Stringleft that had an incorrect number of characters and spent a lot of time off messing up other code with debug lines untill I finally found the wrong REAL mistake. :(

Anyway I tried to find a solution to that problem and came up with loop/execute.

Started the post and discovered 1/2 way through that it wasted too much time.

But I thought I'd post it anyway to see if anyone had anything usefull to add.

Looks like so far you were the only one. :)

BTW did you post the intentional error in the "select case"? I just cut and paste and thought I'd leave it that way to demonstrate the error I'd run into.

Thanks for the assistance.

Share this post


Link to post
Share on other sites

Occam's Razor: The Simplest Solution is the Best Solution.

If it's easier for you to understand and follow, generally it's also easier for the computer to do so as well.

Share this post


Link to post
Share on other sites

Occam's Razor: The Simplest Solution is the Best Solution.

If it's easier for you to understand and follow, generally it's also easier for the computer to do so as well.

I agree.....NOW.... :(

After spending all that time trying to find the fault only to discover that I'd miscounted the number of characters in a string. The "Simplest Solution" was the one where the "human" (ME) didn't have to count. :)

Of course now that I've wasted more time trying to devise a solution to the "HUMAN" error I find that it is more complex than the first.

BUT we live and learn.

One of the things that keeps amazing me is the flexabilty of AutoIT. Not many langages would even allow me to write/excute the Loop/Execute solution code.

Thanks for your advise

John Morrison

aka

Storm-E

Share this post


Link to post
Share on other sites

Just a couple of things to point out.

The second method is probably the better although your first could have been shortened considerably.

A SRER could solve a few potential problems and make the Switch easier

local $inputString = "Willma Flintstone"
    ConsoleWrite('$inputString = ' & $inputString & @crlf)
    $sCharacter = StringLower(StringRegExpReplace($inputString, "(?i)([a-z]+).*", "$1"))
    Switch $sCharacter
        case "fred"
            fredfunc()
        Case "willma"
            ConsoleWrite("Willam ran from fred" & @cr)
        Case "Betty"
            BettyFunc("Barney")
    EndSwitch

Func fredfunc()
    ConsoleWrite("FRED ran" & @cr)
EndFunc

Func BettyFunc($husband)
    ConsoleWrite("Betty ran" & @cr)
EndFunc

The case statements don't care if the strings are upper, lower or mixed case so I have it returning all lower.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Just a couple of things to point out.

The second method is probably the better although your first could have been shortened considerably.

Agreed! It was just a slapped together piece of code to eliminate "THE" problem I had in my mind at the time.

A SRER could solve a few potential problems and make the Switch easier

I guess you mean

$sCharacter = StringLower(StringRegExpReplace($inputString, "(?i)([a-z]+).*", "$1"))

I can see the use for StringLower I usually use it. In my case it's only the first X character that should be StringLower and the data is coming from a known source so and should have the right case.

I am curious what StringRegExpReplace($inputString, "(?i)([a-z]+).*", "$1")) does. I've got a mental blank where Regex is concerned and everytime I need to use it I have to go back to basics. :):( It's annoying as I know how powerfull it is.

Thanks

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Agreed! It was just a slapped together piece of code to eliminate "THE" problem I had in my mind at the time.

I guess you mean

$sCharacter = StringLower(StringRegExpReplace($inputString, "(?i)([a-z]+).*", "$1"))

I can see the use for StringLower I usually use it. In my case it's only the first X character that should be StringLower and the data is coming from a known source so and should have the right case.

I am curious what StringRegExpReplace($inputString, "(?i)([a-z]+).*", "$1")) does. I've got a mental blank where Regex is concerned and everytime I need to use it I have to go back to basics. :):( It's annoying as I know how powerfull it is.

Thanks

Since your example is just that and not explicitly what you want to do then the RegEx would have to be fine tuned. I just used that RegEx as an example to work with what your example but I'll explain it anyway.

(?i) = Case insensitive, so this would have to go if case sensitivity is a problem.

Within the braces (the part that we will match) we have this

[a-z] = match any alpha character between a and z. because of the case insensitivity it will also match from A to Z

+ = match the [a-z] one or more times

then we have .* which is outside the braces so anything that didn't match (0 or more times) inside the braces is not matched. Therefore in your example it stops matching when it hits the space or the end of the string if there is nothing between the first word and the end. It could be more complex so it appears elegant but there is nothing elegant required for this one. I'm a firm believer in keeping it simple. I could have used "((?i)\b[a-z]\b.*(?:\v|\z)" but you don't need it. For your actual application it sounds like you probably need something more like this

"([a-z][A-Z]+)\b.*"

Which is, match only if, the first character is lower-case, followed by one or more upper-case characters up to the word boundary, and stop matching.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I believe what Trancexxx meant is that some of the examples given (e.g. StringLeft($inputString, 4) = "willma") are bound to failure, as written.

I understand that those statements were just for illustration, not real code.

Regarding to approach, I think you now have a range of solutions depending on your context.


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 an MVP you arn't very Valuable or usefull.

Why do you spend so much of your energy attacking people?

Why not actually live up to the MVP title and HELP someone in stead of going out of your way to try and HURT People?

Don't be ridiculous.

Being angry at yourself doesn't give you the right to take it on me.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Don't be ridiculous.

Being angry at yourself doesn't give you the right to take it on me.

I don't think that I am the one that is being ridiculous in this exchange.

Telling someone they SUCK at anything unless you are joking (obviously not in this case) will elicit a response.

My reaction was simply to your unneeded/belittling comment. If as “jchd” says it was simply because of the “mistake” in the code then I still fail to see how your negative comment would have helped. The forum is after all the “General Help and Support” forum.

I didn't take out any anger of myself on you because I didn't feel angry at myself as you are describing it. I don't do that type of thing anyway if I'm angry with myself there is only one person to blame and that is ME.

Setting that aside I don't consider my response was particularly angry, I didn't denigrate you as you did to me. Though in this circumstance I could have taken the low road.

Edited by storme

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I don't think that I am the one that is being ridiculous in this exchange.

Telling someone they SUCK at anything unless you are joking (obviously not in this case) will elicit a response.

My reaction was simply to your unneeded/belittling comment. If as “jchd” says it was simply because of the “mistake” in the code then I still fail to see how your negative comment would have helped. The forum is after all the “General Help and Support” forum.

I didn't take out any anger of myself on you because I didn't feel angry at myself as you are describing it. I don't do that type of thing anyway if I'm angry with myself there is only one person to blame and that is ME.

Setting that aside I don't consider my response was particularly angry, I didn't denigrate you as you did to me. Though in this circumstance I could have taken the low road.

Your code was shit. It didn't deserve a reaction.

If you have thought "what is she trying to tell me? is there something obvious that I'm missing?", you would have saved your self a lots of time (your words).

But you didn't. You have chosen to try hurting (again your word) me instead.

You are being ridiculous.

After all this, I think my comment helped you. But you don't have balls to admit it. (There, I guess I just hurt you again.)

Sorry, you have balls.

Happy Easter

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Your code was shit. It didn't deserve a reaction.

If you have thought "what is she trying to tell me? is there something obvious that I'm missing?", you would have saved your self a lots of time (your words).

But you didn't. You have chosen to try hurting (again your word) me instead.

You are being ridiculous.

After all this, I think my comment helped you. But you don't have balls to admit it. (There, I guess I just hurt you again.)

Sorry, you have balls.

Happy Easter

Your code was shit. It didn't deserve a reaction.

Hmm I've never seen any code made from human or animal excrement. I would think that it would be hard to work with and not very productive (smelly to). I don’t program with animal by products as a rule.

Oh BTW if my example code was “excrement” and “didn't deserve a reaction” why did you go out of your way to give such an obtuse answer? While LOTS of others (Bowmore, ericnail , GEOSoft ) gave quite lucid to the point and helpful responses.

If you have thought "what is she trying to tell me?

Why should I have to decipher your response? Your grasp of the English language appears to be quite good and forums are to communicate so why not communicate so it can be understood?

is there something obvious that I'm missing?", you would have saved your self a lots of time (your words).

But you didn't. You have chosen to try hurting (again your word) me instead.

I can’t see where I have I tried to hurt you? I did give you some advice for the future though it may have been tinged with a little reaction from your initial message. If I have inadvertently caused you, harm in any way please accept my apology.

Of course telling someone they SUCK could never be misunderstood as trying to hurt/belittle/put down anyone.

You are being ridiculous.

After all this, I think my comment helped you.

Sorry no, nothing you’ve said has helped! But my thanks goes out to (Bowmore, ericnail , GEOSoft ) for being as helpful as they have been. Thank you!

But you don't have balls to admit it. (There, I guess I just hurt you again.)

Sorry, you have balls.

You appear to have some worry about my balls. Just to set your mind to rest yes I do have them firmly attached and thanks for the concern.

No, you didn’t hurt me at all. I find the verbal intercourse you’ve instigated rather stimulating.

I hope you had a very good Easter and great weekend.

Share this post


Link to post
Share on other sites

Rhetorical fallacies. Elementary school.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

This topic is going nowhere.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0