Jump to content

Array Value to Number


Recommended Posts

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

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
    Next

Second, 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 by danielkza
Link to comment
Share on other sites

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

post-21557-12612587994569_thumb.jpg

Edited by ScriptCrafter
Link to comment
Share on other sites

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?

Shouldnt it write the file if

$n = 2

2600 + $n = 2602

Actual value = 2602

I have it going from 0 - 4

Edited by ScriptCrafter
Link to comment
Share on other sites

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

  • Moderators

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

M23

Edit: 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
Next

You 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. :evil:

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

M23

Edited by Melba23

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

 

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

  • Recently Browsing   0 members

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