Jump to content
Sign in to follow this  
Uten

[SOLVED] Could you verify this code for me (arrays)

Recommended Posts

Uten

Why does an array element containing a 0 evaluate to true when compared to a hardcoded string?

I would appreciate If someone could take the time to figure out if I have done something wrong before I make a fool out of my self and report a bug.

Thanks..^_^

My run log in scite.

>"F:\portableapps\devtools\scite\..\autoit3tools\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "F:\Documents\code\au3\foo-adrian.au3" /autoit3dir "F:\portableapps\devtools\scite\..\autoit-v3.3.0.0" /UserParams  
+>20:10:44 Starting AutoIt3Wrapper v.1.10.1.14  Environment(Language:0409  Keyboard:00000414  OS:WIN_2000/Service Pack 4  CPU:X86  ANSI)
>Running AU3Check (1.54.14.0)  from:F:\portableapps\devtools\scite\..\autoit-v3.3.0.0
+>20:10:47 AU3Check ended.rc:0
>Running:(3.3.0.0):F:\portableapps\devtools\scite\..\autoit-v3.3.0.0\autoit3.exe "F:\Documents\code\au3\foo.au3"    

(21) : (0)(0) CardType = CardType Evaluates to: True
(33) : (0)(0) $i:=2
(21) : (0)(0) 0 = CardType Evaluates to: True
(43) : (0)(0) $i:=1
(44) : (0)(0) ERROR: Uhh, no error? $ret:=0

+>20:10:53 AutoIT3.exe ended.rc:0
+>20:10:54 AutoIt3Wrapper Finished
>Exit code: 0   Time: 18.646

The code

#include-once

AutoitSetOption('MustDeclareVars',1)

Func dbg($msg, $line=@ScriptLineNumber, $err=@error, $ext=@extended)
    Local $out =  '(' & $line & ') : (' & $err & ')(' & $ext & ') ' & $msg 
    If Not @Compiled AND ProcessExists('SciTE.exe') Then 
        ConsoleWrite( $out & @CRLF)
    Else
        DllCall("kernel32.dll", "none", "OutputDebugString", "str", $out)
    EndIf
EndFunc
;
Func _ArrayScan(ByRef $arr, $line=@ScriptLineNumber)
    ; Return quiz type
    ;text|option|checkbox|mathdrill|mathformula
    Local $i = 0
    If not IsArray($arr) Then Return SetError(1,0,'ERROR')
    For $i = 1 to $arr[0][0]
        If $arr[$i][0] = 'CardType' Then
            dbg($arr[$i][0] & ' = CardType Evaluates to: ' & ($arr[$i][0] = 'CardType'))
            ExitLoop
        EndIf
    Next
    If $i > $arr[0][0] Then Return SetError(2,0, $i)
    dbg('$i:=' & $i, $line)
    Return $arr[$i][1]
EndFunc
;
Func test__ArrayScan()
    Local $ret
    Local $arr[4][2] = [[3,0],  ['question', 'Can you ride on a horse?'],['CardType','text'], ['answer', '[yes]|[you can]|[ofcource]']]
    $ret = _ArrayScan($arr)
    If Not @error = 0 Then dbg('Uhh, did not expect error?')
    If Not ($ret = 'text') Then dbg( 'Unexpected return. Expected text got' & $ret)
    
    ;
    Local $arr2a[4][2] = [[3,0],[1,2],[3,4],[5,6]]
    $ret = _ArrayScan($arr2a)
    If Not @error = 2 Then dbg('Uhh, no error? $ret:=' & $ret)
    ;
    Local $arr2b[4][2] =[[3,0],[0,0],[0,0],[0,0]]; [[3,0],[1,2],[3,4],[5,6]]
    $ret = _ArrayScan($arr2b)
    If not @error = 2 Then dbg('ERROR: Uhh, no error? $ret:=' & $ret)
    ;
    Local $arr3 = 3
    $ret = _ArrayScan($arr3)
    If Not @error = 1 Then dbg('Uhh, no error? $ret:=' & $ret)
    
EndFunc

; ============================================
test__ArrayScan()

EDIT: Added solved in title

Edited by Uten

Share this post


Link to post
Share on other sites
Uten

Good catch..;)

Unfortunately it does not change anything ^_^ I get the same runlog if I wrap it up (@error = X). I would think it should make a diffrence, so I'm pussled.

Usually I have a error detecting function rather than the check against @error

Edit:

This is the important line in the run log:

(21) : (0)(0) 0 = CardType Evaluates to: True
Edited by Uten

Share this post


Link to post
Share on other sites
Jos

Change your test to:

If $arr[$i][0] == 'CardType' Then

comparing a number with a string will give a true when the number is zero.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×