storme Posted December 17, 2010 Posted December 17, 2010 G'day all I've got a larger program that adds to an array when elements are missing. So I do and _arraysearch before adding new rows. The trouble is the search always comes beack element found. I know I'm doing something wrong but I just can't see it. Anyway here is a piece of test code I put together. The 2 tests should return -1 and an @error but they return 0 (IE found at row 0) #include <Array.au3> Dim $array1[1][2] $array1[0][0] = "test00" $array1[0][1] = 0 _ArrayDisplay($array1) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _ArraySearch($array1, "FRED", 0, 0, 0, 0, 1, 0) = ' & _ArraySearch($array1, "FRED", 0, 0, 0, 0, 1, 1) & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _ArraySearch($array1, "FRED") = ' & _ArraySearch($array1, "FRED") & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console Dim $array2[2][2] $array2[0][0] = "test00" $array2[0][1] = 0 $array2[1][0] = "test10" $array2[1][1] = 0 _ArrayDisplay($array2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _ArraySearch($array2, "FRED", 0, 0, 0, 0, 1, 0) = ' & _ArraySearch($array2, "FRED", 0, 0, 0, 0, 1, 1) & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _ArraySearch($array2, "FRED") = ' & _ArraySearch($array2, "FRED") & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console Thanks in advance for any help you can offer. Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E
wakillon Posted December 17, 2010 Posted December 17, 2010 (edited) _ArraySearch($array1, "FRED") doesn't permit to search in a 2D arrays Edited December 17, 2010 by wakillon AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts
Moderators Melba23 Posted December 17, 2010 Moderators Posted December 17, 2010 storme,It is a known bug when searching for a string in an array with numeric zero in an element - see Trac tickets #1569 and #1142. It will apparently be fixed in the next release. I would point out that the Help file does say:"However, it is NOT ADVISABLE to mix different datatypes in an Array" so you cannot say that you were not warned! M23 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
iamtheky Posted December 17, 2010 Posted December 17, 2010 (edited) Put your 0 in quotes.for some reason it appears that a 0 matches any Alpha characters, that is beyond me..#include <Array.au3> Dim $array1[1][2] $array1[0][0] = "test00" $array1[0][1] = "0" _ArrayDisplay($array1) $index0 = _ArraySearch($array1, "FRED") If @ERROR Then msgbox (0, '' , 'not there') Else msgbox (0, '' , $index0) Endif $index1 =_ArraySearch($array1, "FRED", 0, 0, 0, 0, 1, 1) If @ERROR Then msgbox (0, '' , 'not there') Else msgbox (0, '' , $index1) Endif Edited December 17, 2010 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
storme Posted December 17, 2010 Author Posted December 17, 2010 storme, It is a known bug when searching for a string in an array with numeric zero in an element - see Trac tickets #1569 and #1142. It will apparently be fixed in the next release. I would point out that the Help file does say: "However, it is NOT ADVISABLE to mix different datatypes in an Array" so you cannot say that you were not warned! M23 It was the Zero's causing the problem. I'll just have to write my own array search. I don't mix different types in the same collumn as that would be asking for trouble. But having different types in different columns should be safe. Thanks for the help! Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E
Moderators Melba23 Posted December 17, 2010 Moderators Posted December 17, 2010 storme,You could save the numeric zeros as strings by using String($iValue) or something similar - and a corresponding Number($sValue) when you extract it. It is a pain, but less so than writing your own search function - although there was a suggestion on how it might be solved in one of the trac tickets... M23 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
storme Posted December 17, 2010 Author Posted December 17, 2010 storme, You could save the numeric zeros as strings by using String($iValue) or something similar - and a corresponding Number($sValue) when you extract it. It is a pain, but less so than writing your own search function - although there was a suggestion on how it might be solved in one of the trac tickets... M23 Actually, in my case the search function simplified the whole thing. This is the function I came up with. Func _ArraySearchAndAdd2d(ByRef $avArray, $skey, $sValue = "") ; search for Key ; if found update second column ; If not found add new row If IsArray($avArray) Then If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, -1) ; Array NOT 2 dimentional EndIf For $element = 0 To UBound($avArray) - 1 If $skey = $avArray[$element][0] Then $avArray[$element][1] = $sValue Return $element EndIf Next Return _ArrayAdd2d($avArray, $skey, $sValue) EndFunc ;==>_ArraySearchAndAdd2d and this is my 2D array add function, to complete the picture. Func _ArrayAdd2d(ByRef $avArray, $sVar0 = "", $sVar1 = "", $sVar2 = "", $sVar3 = "", $sVar4 = "", $sVar5 = "", $sVar6 = "", $sVar7 = "", $sVar8 = "", _ $sVar9 = "", $sVar10 = "", $sVar11 = "", $sVar12 = "") ;add row to 2 dimentional array ; If a standard variable is passed then it is converted to an array and the row is added to it ; Converted from standard UDF If IsArray($avArray) Then If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, -1) ; Array NOT 2 dimentional EndIf Local $iUBound If IsArray($avArray) Then $iUBound = UBound($avArray) ReDim $avArray[$iUBound + 1][UBound($avArray, 2)] Else ;Convert varaible to array $iUBound = 0 Dim $avArray[1][@NumParams - 1] EndIf For $i = 0 To @NumParams - 2 $vTmp = Eval("sVar" & $i) $avArray[$iUBound][$i] = $vTmp Next Return $iUBound EndFunc ;==>_ArrayAdd2d Thanks again for your help. If you hadn't pointed me in the right direction I never would have thought up this more elegant solution than the bloat I had there. Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E
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