Jump to content

Recommended Posts

Posted

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

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.

  • Moderators
Posted

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

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! :P

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

 

Posted (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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted

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

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! :shifty:

M23

It was the Zero's causing the problem. :P

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!

  • Moderators
Posted

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

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

 

Posted

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

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...