Jump to content

Array scan - what am I doing wrong?


ahha
 Share

Recommended Posts

Okay I know this will be one of those - how stupid can I be when I see the answer but I'm baffled at the current time.

#include <Debug.au3>
Local $aArray[11][2] = [ [10,10],[1,5],[2,0],[3,0],[4,"M"],[5,0],[6,0],[7,0],[8,"M"],[9,0],[10,2] ]

_DebugArrayDisplay($aArray, "$aArray")

Local $iCount = 0       ;init
Local $i
For $i = 1 to $aArray[0][0]
    If $aArray[$i][1] = "M" Then
        $iCount = $iCount + 1
        ;debug
        ;MsgBox($MB_OK + $MB_TOPMOST, "Debug", "$aArray[$i][0] = " & $aArray[$i][0] & @CRLF & "$aArray[$i][1] = " & $aArray[$i][1])
    EndIf
Next
MsgBox($MB_OK + $MB_TOPMOST, "Info", "M's found = " & $iCount)

When I run this it states there are 8 M's in the array.

Link to comment
Share on other sites

== definitely works.

Here's my confusion (other than having used = all the time) the documentation says: If <expression> Then statement

The expression can contain the boolean operators of And, Or, and Not as well as the logical operators <, <=, >, >=, =, ==, and <> grouped with parentheses as needed.

I equated the logical operator = to be an operator NOT an assignment. 

And the Language Reference - Operators specifically states:

  Comparison operators (case-insensitive if used with strings except for ==)
 

= Tests if two values are equal. e.g. If $vVar = 5 Then (true if $vVar equals 5). Case-insensitive when used with strings. See below about comparing with mixed datatypes.

 

and

 

 

== Tests if two strings are equal. Case-sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case-sensitive.

So the question remains - why is it not working?

 

Edited by ahha
added == definition
Link to comment
Share on other sites

But your comparing two different datatypes, 

Comparing different datatypes

Care is needed if comparing mixed datatypes, as unless the case-sensitive (==) string operator is used, mixed comparisons are usually made numerically. Most strings will be evaluated as 0 and so the result may well not be the one expected. It is recommended to force the items being compared into the same datatype using Number()/String() before the comparison.

Link to comment
Share on other sites

So is the array datatype by definition one of numeric?

I ask because the Global example uses: Local $aArray_1[12] = [3, 7.5, "string"], $aArray_1[5] = [8, 4, 5, 9, 1]

Local $aGrid[2][4] = [["Paul", "Jim", "Richard", "Louis"], [485.44, 160.68, 275.16, 320.00]]

so my assumption was that it autotyped based on the entry type.

Link to comment
Share on other sites

If you need to find the datatype of a variable, you can use IsString () or isNumber () or isFloat () functions (among others - look help file in Variables and Conversions section for list of all the Is* functions).

Link to comment
Share on other sites

Tested on my system without any issues, so not sure why it's not working for you.  Each array item can be a different datatype, if you look at your array, you're testing if 0 = "M", since they're two different datatypes, "M" is converted to 0 so it equals true. String(0) = "M" will comparing both strings and will return false.

Link to comment
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
 Share

×
×
  • Create New...