ScriptCrafter Posted December 19, 2009 Share Posted December 19, 2009 Hello, Farily new to the whole scripting scene, but I have found a problem that has kept me stuck and I need help. I have extracted values from a column in an excel file and call it $array3 Here is what I am trying to do. Case $msg = $B1 Global $sMatches = "PH" For $n = 0 to UBound($array2) - 1 If StringInStr($array2[$n], $sMatches) Then If (2600 + $n) = $array3[$n] FileWrite ($MAD01, "Item [" & $n & "] is a match: " & $array1[$n] & @CRLF) Else FileWrite ($MAD01, "200" & $n & @CRLF) EndIf EndIf Next The values listed in $array3 are clearly 2601, 2602, 2603, and so on so I know I have a match, but I believe the problem is that I am trying to match a string value of the number "2603" in the array. I need that array to match the numerical value that I pull from the column. Any help would be much appreciated. Link to comment Share on other sites More sharing options...
danielkza Posted December 19, 2009 Share Posted December 19, 2009 (edited) First, you should wrap your code around [autoit] tags, and indent it properly so it's readable. Like this:Case $msg = $B1 Global $sMatches = "PH" For $n = 0 to UBound($array2) - 1 If StringInStr($array2[$n], $sMatches) Then If (2600 + $n) = $array3[$n] Then FileWrite ($MAD01, "Item [" & $n & "] is a match: " & $array1[$n] & @CRLF) Else FileWrite ($MAD01, "200" & $n & @CRLF) EndIf EndIf NextSecond, AutoIT is a dynamically typed language, and has the capability to convert values automatically for comparisons. The '=' operator does that by default, so "2600" = 2600 yields True. But this won't work if your string has other invalid characters. You should check that your values are indeed what you expect them to be by using something like _ArrayDisplay for debugging.If you want a numeric comparison, without automatic conversion, you must 'cast' both values to integers, and use the strict equality operator (==), like this. Notice that you need not cast the expression on the left: it's safe to assume both 2600 and $n are integers in this particular case.If (2600 + $n) == Int($array3[$n]) Then ; ... Edited December 19, 2009 by danielkza Link to comment Share on other sites More sharing options...
ScriptCrafter Posted December 19, 2009 Author Share Posted December 19, 2009 (edited) Hello, I have attached the _ArrayDisplay results I have edited my code as you mentioned to. Case $msg = $B1 Global $sMatches = "PH" For $n = 0 to UBound($array2) - 1 If StringInStr($array2[$n], $sMatches) Then If (2600 + $n) == Int($array3[$n]) Then _ArrayDisplay ($array3, "TEST") FileWrite ($MAD01, "Item [" & $n & "] is a match: " & $array1[$n] & @CRLF) Else _ArrayDisplay ($array3, "TEST") FileWrite ($MAD01, "200" & $n & @CRLF) EndIf EndIf Next It still does not match the numerical values pulled from the array and write to the file. Am I doing something wrong still? Thanks again for your help Edited December 19, 2009 by ScriptCrafter Link to comment Share on other sites More sharing options...
Fubarable Posted December 19, 2009 Share Posted December 19, 2009 I have no idea if this is significant or not, but you seem to be using both $array2 and $array3. Would things go smoother if you just stuck with $array3? Link to comment Share on other sites More sharing options...
danielkza Posted December 19, 2009 Share Posted December 19, 2009 The code is behaving like it should. Look, for instance, at element #1. $n = 1 2600 + $n = 2601 Actual value = 2602 Should your values start at 2602 as they do, or is it not intended? Link to comment Share on other sites More sharing options...
ScriptCrafter Posted December 19, 2009 Author Share Posted December 19, 2009 (edited) The code is behaving like it should. Look, for instance, at element #1.$n = 12600 + $n = 2601Actual value = 2602Should your values start at 2602 as they do, or is it not intended?Shouldnt it write the file if$n = 22600 + $n = 2602Actual value = 2602I have it going from 0 - 4 Edited December 19, 2009 by ScriptCrafter Link to comment Share on other sites More sharing options...
ScriptCrafter Posted December 19, 2009 Author Share Posted December 19, 2009 I have no idea if this is significant or not, but you seem to be using both $array2 and $array3. Would things go smoother if you just stuck with $array3? I even tried it your way and no luck!!! I know its something tiny I must be missing Case $msg = $B1 For $n = 0 to UBound($array3) - 1 Global $sMatches = "PH" Global $math = (2600 + $n) If StringInStr(Int(($array3[$n])), $math) Then If StringInStr($array2[$n], $sMatches) Then ;_ArrayDisplay ($array3, "TEST") FileWrite ($MAD01, "Item [" & $n & "] is a match: " & $array1[$n] & @CRLF) Else ;_ArrayDisplay ($array3, "TEST") FileWrite ($MAD01, "200" & $n & @CRLF) EndIf EndIf Next If anybody sees my mistake I would be eternally gratefull Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 20, 2009 Moderators Share Posted December 20, 2009 (edited) ScriptCrafter,What exactly are you trying to do here? If you want to match, say, (2600 + n) to a similar value anywhere in your $array3 then you are not even close.If you want to see whether the $array3[n] element is set to (2600 + n) then you are closer, but going about it the wrong way.Please explain the logic behind the code and then we can set about making it work. M23Edit: Here is short code snippet which shows how to do the 2 options above:Global $array3[3] $array3[0] = "2602" $array3[1] = "2601" $array3[2] = "2600" MsgBox(0, "First Section", "A match is when" & @CRLF & @CRLF & "$array3[$n] = (2600 + $n)") For $n = 0 To UBound($array3) - 1 Global $sMatches = "PH" Global $math = (2600 + $n) If Number($array3[$n]) = $math Then MsgBox(0, "Match", "We found a match with $array3 element " & $n & @CRLF & @CRLF & "when $math = " & $math) EndIf Next MsgBox(0, "Second Section", "A match is when" & @CRLF & @CRLF & "(2600 + $n) is found in the $array3") For $n = 0 To UBound($array3) - 1 Global $sMatches = "PH" Global $math = (2600 + $n) For $i = 0 To UBound($array3) - 1 If Number($array3[$i]) = $math Then MsgBox(0, "Match", "We found a match with $array3 element " & $i & @CRLF & @CRLF & "when $math = " & $math) EndIf Next NextYou will see that danielkza was on the right track, but you were using String functions, so you would have needed to convert both sides to strings, not numbers. Here we are using normal aritmetic comparisons and so we correctly convert the array elements to numbers.AutoIt tries to type variables to the best of its ability, but it cannot read your mind and it is best to ensure programatically (?) that you are have correct types when making any comparisons. As an aside, the == operator is not a "strict equality operator" as suggested above. It is only used to compare strings when you need a case sensitive comparison - from the Help file:== 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 for string comparisons that need to be case sensitive.I hope this helps. Ask if anything is unclear or if these are not the logic you need. M23 Edited December 20, 2009 by Melba23 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 Link to comment Share on other sites More sharing options...
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