ahha Posted January 29, 2020 Posted January 29, 2020 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.
AdamUL Posted January 29, 2020 Posted January 29, 2020 "M" is getting converted to 0. That is why it is showing 8 M's, when it is actually 8 0's. Use the following instead for a case sensitive string compare. If $aArray[$i][1] == "M" Then Adam
Subz Posted January 29, 2020 Posted January 29, 2020 For case insensitive If String($aArray[$i][1]) = "m" Then
ahha Posted January 29, 2020 Author Posted January 29, 2020 (edited) == 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 January 29, 2020 by ahha added == definition
Subz Posted January 29, 2020 Posted January 29, 2020 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.
ahha Posted January 29, 2020 Author Posted January 29, 2020 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.
Nine Posted January 29, 2020 Posted January 29, 2020 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). “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
ahha Posted January 29, 2020 Author Posted January 29, 2020 45 minutes ago, Subz said: If String($aArray[$i][1]) = "m" Then Even this yields M's = 8. Putting lowercase "m" into the array has the same result. So how do I use Number()/String() before the comparison?
ahha Posted January 29, 2020 Author Posted January 29, 2020 (edited) Just carefully read Subz If String($aArray[$i][1]) = "m" Then I had missed the String part. Thanks all. Edited January 29, 2020 by ahha
Nine Posted January 29, 2020 Posted January 29, 2020 Yes exactly what you said : ; example Local $a = 1, $b = "m" If IsString ($b) then if String ($a) = $b then ;do something endif endif “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted January 29, 2020 Posted January 29, 2020 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.
ahha Posted January 29, 2020 Author Posted January 29, 2020 (edited) Thanks all. My stupid mistake as I suspected Edited January 29, 2020 by ahha
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