Jump to content

Too many IF Statements


Go to solution Solved by Melba23,

Recommended Posts

Once again looking for some advice. I want to clean up and improve my code a bit. 

What would be the most efficient way to write this code? 

If $vState = "Alabama" Then
$vAbbreviation = "AL"
EndIF

If $vState = "Arkansas" Then
$vAbbreviation = "AR"
EndIF

If $vState = "Arizona" Then
$vAbbreviation  = "AZ"
EndIF

If $vState = "Alaska" Then
$vAbbreviation  = "AK"
EndIF

 

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

  • Moderators
  • Solution

SkysLastChance,

 Two suggestions:

1. Use a Switch block.

2. Put the names and abbreviations into an array and search through it to find a match.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Like @Melba23said in 2)

From https://en.wikipedia.org/wiki/List_of_U.S._state_and_territory_abbreviations copy thee content of the states' table, remove the unwanted columns and make the data a global array (or better: a Map) in your code.

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

Thanks for the help guys! Really appreciate it. 

#include <Array.au3>

Global $aStateArray[4][2] = [["Alabama","AL"],["Arkansas","AR"],["Arizona","AZ"],["Alaska","AK"]]

$vAbbreviations = "Alaska"
Local $iIndex = _ArraySearch($aStateArray, $vAbbreviations)
For $i = 0 to UBound($aStateArray, 2)-1
   If $aStateArray[$iIndex][$i] = $vAbbreviations Then $iSubIndex = $i
Next

MsgBox(0, "Found",$aStateArray[$iIndex][1])

This is what I have. 

 

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

Maps are simpler and faster:

Global $mCodes[]
$mCodes["Alabama"] = "AL"
$mCodes["Alaska"] = "AK"
$mCodes["Arizona"] = "AZ"
$mCodes["Arkansas"] = "AR"
$mCodes["California"] = "CA"
$mCodes["Colorado"] = "CO"
$mCodes["Connecticut"] = "CT"
$mCodes["Delaware"] = "DE"
$mCodes["District of Columbia"] = "DC"
$mCodes["Florida"] = "FL"
$mCodes["Georgia"] = "GA"
$mCodes["Hawaii"] = "HI"
$mCodes["Idaho"] = "ID"
$mCodes["Illinois"] = "IL"
$mCodes["Indiana"] = "IN"
$mCodes["Iowa"] = "IA"
$mCodes["Kansas"] = "KS"
$mCodes["Kentucky"] = "KY"
$mCodes["Louisiana"] = "LA"
$mCodes["Maine"] = "ME"
$mCodes["Maryland"] = "MD"
$mCodes["Massachusetts"] = "MA"
$mCodes["Michigan"] = "MI"
$mCodes["Minnesota"] = "MN"
$mCodes["Mississippi"] = "MS"
$mCodes["Missouri"] = "MO"
$mCodes["Montana"] = "MT"
$mCodes["Nebraska"] = "NE"
$mCodes["Nevada"] = "NV"
$mCodes["New Hampshire"] = "NH"
$mCodes["New Jersey"] = "NJ"
$mCodes["New Mexico"] = "NM"
$mCodes["New York"] = "NY"
$mCodes["North Carolina"] = "NC"
$mCodes["North Dakota"] = "ND"
$mCodes["Ohio"] = "OH"
$mCodes["Oklahoma"] = "OK"
$mCodes["Oregon"] = "OR"
$mCodes["Pennsylvania"] = "PA"
$mCodes["Rhode Island"] = "RI"
$mCodes["South Carolina"] = "SC"
$mCodes["South Dakota"] = "SD"
$mCodes["Tennessee"] = "TN"
$mCodes["Texas"] = "TX"
$mCodes["Utah"] = "UT"
$mCodes["Vermont"] = "VT"
$mCodes["Virginia"] = "VA"
$mCodes["Washington"] = "WA"
$mCodes["West Virginia"] = "WV"
$mCodes["Wisconsin"] = "WI"
$mCodes["Wyoming"] = "WY"

; ...

; to use it:

Local $sState = "Iowa"

ConsoleWrite("The code for " & $sState & " is: " & $mCodes[$sState] & @LF)

I made a map of official US state names and USPS codes. Change if needed.

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

Use a function to do that with a return value so you can exit your loop as soon as the match is found and have it return the value you want as part of the function.

 

Else put an ExitLoop as part of your If/then statement to at least save some loops when the first value is matched.

 

#include <Array.au3>

Global $aStateArray[4][2] = [["Alabama","AL"],["Arkansas","AR"],["Arizona","AZ"],["Alaska","AK"]]

$vAbbreviations = "Alaska"
Local $iIndex = _ArraySearch($aStateArray, $vAbbreviations)
For $i = 0 to UBound($aStateArray, 2)-1
   If $aStateArray[$iIndex][$i] = $vAbbreviations Then $iSubIndex = $i
Next

MsgBox(0, "Found",$aStateArray[$iIndex][1])


;New Code
MsgBox(0, "Found", AbrevLookup($vAbbreviations))


;Functions
Func AbrevLookup($sState)
    For $i = 0 to UBound($aStateArray) -1
        If StringInStr($aStateArray[$i][0], $sState) Then Return $aStateArray[$i][1]
    Next
EndFunc

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

4 minutes ago, Exit said:

But runs only in beta version!

Wrong!

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

@Exitare you using the latest release?

Should the OP or anyone else need the reverse lookup, just use that:

Global $mStates[]
$mStates["AL"] = "Alabama"
$mStates["AK"] = "Alaska"
$mStates["AZ"] = "Arizona"
$mStates["AR"] = "Arkansas"
$mStates["CA"] = "California"
$mStates["CO"] = "Colorado"
$mStates["CT"] = "Connecticut"
$mStates["DE"] = "Delaware"
$mStates["DC"] = "District of Columbia"
$mStates["FL"] = "Florida"
$mStates["GA"] = "Georgia"
$mStates["HI"] = "Hawaii"
$mStates["ID"] = "Idaho"
$mStates["IL"] = "Illinois"
$mStates["IN"] = "Indiana"
$mStates["IA"] = "Iowa"
$mStates["KS"] = "Kansas"
$mStates["KY"] = "Kentucky"
$mStates["LA"] = "Louisiana"
$mStates["ME"] = "Maine"
$mStates["MD"] = "Maryland"
$mStates["MA"] = "Massachusetts"
$mStates["MI"] = "Michigan"
$mStates["MN"] = "Minnesota"
$mStates["MS"] = "Mississippi"
$mStates["MO"] = "Missouri"
$mStates["MT"] = "Montana"
$mStates["NE"] = "Nebraska"
$mStates["NV"] = "Nevada"
$mStates["NH"] = "New Hampshire"
$mStates["NJ"] = "New Jersey"
$mStates["NM"] = "New Mexico"
$mStates["NY"] = "New York"
$mStates["NC"] = "North Carolina"
$mStates["ND"] = "North Dakota"
$mStates["OH"] = "Ohio"
$mStates["OK"] = "Oklahoma"
$mStates["OR"] = "Oregon"
$mStates["PA"] = "Pennsylvania"
$mStates["RI"] = "Rhode Island"
$mStates["SC"] = "South Carolina"
$mStates["SD"] = "South Dakota"
$mStates["TN"] = "Tennessee"
$mStates["TX"] = "Texas"
$mStates["UT"] = "Utah"
$mStates["VT"] = "Vermont"
$mStates["VA"] = "Virginia"
$mStates["WA"] = "Washington"
$mStates["WV"] = "West Virginia"
$mStates["WI"] = "Wisconsin"
$mStates["WY"] = "Wyoming"

; ...

; to use it:

Local $sCode = "DC"
ConsoleWrite("The code " & $sCode & " is for: " & $mStates[$sCode] & @LF)

 

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

  • Moderators

Exit,

You need the Beta to use Maps.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

I don't get that issue:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\jc\Documents\AutoMAT\tmp\t.au3" /UserParams    
+>19:38:14 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0)  Keyboard:0000040C  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:040C)  CodePage:65001  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\jc\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\jc\AppData\Local\AutoIt v3\SciTE 
>Running:(3.3.15.4):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\jc\Documents\AutoMAT\tmp\t.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
The code for Iowa is: IA
The code DC is for: District of Columbia
+>19:38:15 AutoIt3.exe ended.rc:0
+>19:38:15 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.549

Using F5, it runs with /prod

Using the beta is that:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /beta /ErrorStdOut /in "C:\Users\jc\Documents\AutoMAT\tmp\t.au3" /UserParams    
+>19:40:48 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0)  Keyboard:0000040C  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:040C)  CodePage:65001  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\jc\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\jc\AppData\Local\AutoIt v3\SciTE 
>Running:(3.3.15.4):C:\Program Files (x86)\AutoIt3\Beta\autoit3.exe "C:\Users\jc\Documents\AutoMAT\tmp\t.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
The code for Iowa is: IA
The code DC is for: District of Columbia
+>19:40:49 AutoIt3.exe ended.rc:0
+>19:40:49 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.535

and then it uses the switch /beta

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

Oops, it seems that I permanently installed a beta supplied by jpm for test purpose. Sorry for the noise.

It remains that the Map support in beta has been stable for long and that no issue has been reported yet. Given the power Maps give us I see no reason not to use them when they offer  a benefit.

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

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