SkysLastChance Posted October 11, 2021 Share Posted October 11, 2021 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 More sharing options...
Moderators Solution Melba23 Posted October 11, 2021 Moderators Solution Share Posted October 11, 2021 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 SkysLastChance and Earthshine 2 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jchd Posted October 11, 2021 Share Posted October 11, 2021 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. Earthshine and SkysLastChance 2 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
SkysLastChance Posted October 11, 2021 Author Share Posted October 11, 2021 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 More sharing options...
jchd Posted October 11, 2021 Share Posted October 11, 2021 Maps are simpler and faster: expandcollapse popupGlobal $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. SkysLastChance and Earthshine 2 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
ViciousXUSMC Posted October 11, 2021 Share Posted October 11, 2021 (edited) 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 October 11, 2021 by ViciousXUSMC SkysLastChance 1 Link to comment Share on other sites More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 7 minutes ago, jchd said: Maps are simpler and faster: But runs only in beta version! App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
jchd Posted October 11, 2021 Share Posted October 11, 2021 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 6 minutes ago, jchd said: Wrong! ==> Variable subscript badly formatted.: Global $mCodes[] Global $mCodes[^ ERROR App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
jchd Posted October 11, 2021 Share Posted October 11, 2021 @Exitare you using the latest release? Should the OP or anyone else need the reverse lookup, just use that: expandcollapse popupGlobal $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) SkysLastChance 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 5 minutes ago, jchd said: @Exitare you using the latest release? App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 11, 2021 Moderators Share Posted October 11, 2021 Exit, You need the Beta to use Maps. M23 Exit 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 24 minutes ago, Exit said: But runs only in beta version! That was what I said some posts above. App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
jchd Posted October 11, 2021 Share Posted October 11, 2021 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 Exit 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 3 minutes ago, jchd said: >Running:(3.3.15.4): And this is the BETA !!! App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 Just insert ConsoleWrite(@AutoItVersion & @CRLF) as first line App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
jchd Posted October 11, 2021 Share Posted October 11, 2021 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. Exit 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Exit Posted October 11, 2021 Share Posted October 11, 2021 I also think MAPS is an excellent addition to AutoIt. Maybe @Jon will give us a Christmas present this year. App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now