Jardz Posted January 12, 2024 Posted January 12, 2024 Hello all, Is there a method within the For...In...Next loop that can give me the 'key' value of the Map datatype? The iteration gives you the 'value' as standard, but I also need the 'Key' I know I can extract all keys into a 1D array with MapKeys(), however I'm trying to avoid this as the function is called many times and potentially with large maps. Global $mMap[] For $x = 1 To 10 $mMap["key" & $x] = $x Next For $iVal In $mMap ConsoleWrite("$iVal = " & $iVal & @CRLF) ;How to get 'Key' here? ;$sKey = $mMap.first Next Many thanks for your help sjardz
Nine Posted January 12, 2024 Posted January 12, 2024 Look in help file for MapKeys... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
paw Posted January 12, 2024 Posted January 12, 2024 Global $mMap[] For $x = 1 To 2^16 $mMap["key" & $x] = $x Next Global $hTimer = TimerInit() Global $vVal ; ~278ms For $sKey In MapKeys($mMap) $vVal = $mMap[$sKey] Next ; ~790ms ;~ For $iVal In $mMap ;~ $vVal = $iVal ;~ Next Global $fDeltaT = TimerDiff($hTimer) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Delta Time=' & $fDeltaT & 'ms' & @CRLF) also, iterating over the array from MapKeys this way is faster and you get the key
Jardz Posted January 13, 2024 Author Posted January 13, 2024 Hi Nine and Paw, many thanks for your input 👍. The function that will be using this is called many times, so I was keen not to copy all the keys (as an array) to the heap every call, hence my avoidance of MapKeys(). I have a script that uses an array, I was wondering if a map would be more performant, but as Paw has demonstrated above, arrays are still faster in certain areas. While re-writing to replace the array with a map I can across this problem with me implementation, was hoping there may be a hidden undocumented method to get the key. Many thanks again for taking the time to look at this Have a great 2024 🎉
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