Moderators SmOke_N Posted August 6, 2012 Moderators Posted August 6, 2012 (edited) This is a utility I created for a current project I'm working on. I'm aware other hash table and associative array udfs are out there, but I needed a few specific things. What started as just a function or two, I decided to just take a couple of hours and put together something I could utilize with other projects a bit more robustly. This relies on the SQLite.au3. So you'll have to adhere to whatever initialization rules it has ( such as #including the SQLiteDLL.au3 if you don't have your own version of sqlite3.dll you're using and utilizing _SQLite_Startup or _hashlite_Initialize ). One of the things I needed was the ability to be able to do 1 hash field or many hash fields for my query's. Now I haven't the foggiest idea on the data adding speed comparison of other like UDFs, that wasn't my concern or my goal. My goals were simple: - Create a lookup table that I could use multiple hash fields with - Create a method that once data was inserted into the hashfield that it could be retrieved quickly - Maintain the data typing of the variables that were being passed This is barely tested, if you find issues (there's bound to be plenty) or see optimization options, please let me know, I'll get to them as soon as I can. Here are some single field hash examples: expandcollapse popup#include "HashLite.au3" #include <Array.au3> _hashlite_Initiate("") ; create a single field hash table, remember you can have up to 100 hash fields ; default number of hash fields is only 1 as we'll show below ; hash fields are only for storage and lookup purposes ; return arrays/sorts/uniques will not return the original hash values associated with them Global $ga_ptrHash = _hashlite_Create() If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit -101 EndIf ; add some values to the newly created single field hash table _hashlite_AddValue($ga_ptrHash, Binary("some random value1"), "apple") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 1 EndIf _hashlite_AddValue($ga_ptrHash, "some random value2", "orange") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 2 EndIf _hashlite_AddValue($ga_ptrHash, 1, "grape") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 3 EndIf _hashlite_AddValue($ga_ptrHash, 2.5, "mango") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 4 EndIf _hashlite_AddValue($ga_ptrHash, WinGetHandle(""), "pear") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 5 EndIf ; retrieve some values from the single field hash table ; show on success that data type is kept in tact Global $gs_Value = _hashlite_GetValue($ga_ptrHash, "apple") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit -1 Else ConsoleWrite("Type: "& VarGetType($gs_Value) & " : " & $gs_Value & @CRLF) EndIf $gs_Value = _hashlite_GetValue($ga_ptrHash, "orange") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit -2 Else ConsoleWrite("Type: "& VarGetType($gs_Value) & " : " & $gs_Value & @CRLF) EndIf $gs_Value = _hashlite_GetValue($ga_ptrHash, "grape") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit -3 Else ConsoleWrite("Type: "& VarGetType($gs_Value) & " : " & $gs_Value & @CRLF) EndIf $gs_Value = _hashlite_GetValue($ga_ptrHash, "mango") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit -4 Else ConsoleWrite("Type: "& VarGetType($gs_Value) & " : " & $gs_Value & @CRLF) EndIf $gs_Value = _hashlite_GetValue($ga_ptrHash, "pear") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit -5 Else ConsoleWrite("Type: "& VarGetType($gs_Value) & " : " & $gs_Value & @CRLF) EndIf ; create an array from the single field hash table Global $ga_Trippn1 = _hashlite_TableToArray($ga_ptrHash) _ArrayDisplay($ga_Trippn1, "TableToArray - 1") ; delete a value from the single field hash table _hashlite_Delete($ga_ptrHash, "grape") ; show array after deleted item, show it's been deleted Global $ga_Trippn2 = _hashlite_TableToArray($ga_ptrHash) _ArrayDisplay($ga_Trippn2, "TableToArray - 2") ; add a couple of repeating values to single field hash table _hashlite_AddValue($ga_ptrHash, 3.5, "fruit") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 6 EndIf _hashlite_AddValue($ga_ptrHash, 3.5, "fruitfiles") If @error Then ConsoleWrite("Err: " & @error & @CRLF & _hashlite_GetLastErrorMsg() & @CRLF) Exit 7 EndIf ; show table after add value and before sort Global $ga_Trippn3 = _hashlite_TableToArray($ga_ptrHash) _ArrayDisplay($ga_Trippn3, "TableToArray - 3") ; show table after ascending sort of all legal hash stored items in table Global $ga_SortedAsc = _hashlite_Sort($ga_ptrHash, False, 0) _ArrayDisplay($ga_SortedAsc, "Sorted Ascending") ; show table after descending sort of all legal hash stored items in table Global $ga_SortedDesc = _hashlite_Sort($ga_ptrHash, True, 0) _ArrayDisplay($ga_SortedDesc, "Sorted Descending") ; show unique items of all legal hash stored items in table Global $ga_Unique = _hashlite_Unique($ga_ptrHash) _ArrayDisplay($ga_Unique, "Unique") HashLite.au3 HashLite.au3 I'm probably missing something... Edited August 6, 2012 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
czardas Posted August 6, 2012 Posted August 6, 2012 This looks very interesting, though I have to admit my knowledge of this subject is extremely limited. I'll have to study your code. Thanks for sharing this. operator64 ArrayWorkshop
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