Jump to content

array only saves the last updated element, others seems lost


Recommended Posts

Hello dear humans, this is a continuation of a failed thread of mine (I tried something out of quick idea, and later i realized that it was broken.. but nvm that).

I have to script a program for automatization lets call it program A. However during this process A needs generated codes (11number strings) that are generated by another program, we can call it B. I thought that I will create a function that run program B, generate the code needed  (it can only generate them one at a time), and save it in a global array, so the other functions can use it when they will be needed. Keep in mind that all these codes are useable only once, I will have to delete them from the array after they are used. So I got to the point that my array grows dynamicly while a function that initiate B, and starts to generate the codes, in every loop the array grows, but here is my problem right now. As in the title the array lose the previus elements, and has only the new one at the end, just to lose that when a new loop runs again.

What I get in the console is this:

$arr[1]:=36036797433
$arr[1]:=
$arr[2]:=36036397523
$arr[1]:=
$arr[2]:=
$arr[3]:=36036397655
$arr[1]:=
$arr[2]:=
$arr[3]:=
$arr[4]:=36036397655
 
and my code is: Edit (Global $iMax = 1)
Func Adattoltes()
    Local $StartFileSelectFolder = FileSelectFolder("Adja meg az UD_uszi.exe helyét!", "") ;program B, can be different places on differnet user  PCs (just like A)
    Run($StartFileSelectFolder & "\UD_uszi.exe")
    Sleep(3000)
    WinWaitActive("UD ÜSZI")
    ControlSetText("TLoginForm", "UD ÜSZI", "TEdit2", "BALAZSGE")
    Send("{TAB}BALAZSGE")
    Sleep(50)
    Send("{ENTER}")
    WinWaitActive("UD Üszi - verzió")
    Local $handle = WinGetHandle("UD Üszi - verzió")
    Local $1 = 1
    While $1 = 1
        ControlClick($handle, "", "[CLASSNN:TAdvBitBtn13; TEXT:Teszt - Útdíj fizetés adhoc előkészítés]")
        WinWaitActive("Útdíj fizetés adatok")
        Local $handle2 = WinGetHandle("Útdíj fizetés adatok")
        WinMove($handle2, "", 541, 210)
        ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn4]")
        Sleep(200)
        ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn3]")
        Sleep(4000) ;After this button the B program works a bit, dont know better solution for that... anyidea?
        ControlClick($handle2, "", "[CLASSNN:TDEdit8]", "", 1, 320, 10)
        Sleep(200)
        ControlClick($handle2, "", "[CLASSNN:TDEdit8]", "", 1, 300, 15)
        Sleep(200)
        Send("{DOWN}")
        Sleep(200)
        Send("{ENTER}")
        Sleep(200)
        ControlFocus($handle2, "", "TDEdit4") ;here is where the code is generated
        Sleep(1000)
        Send("{CTRLDOWN}c{CTRLUP}") ;this somehow doesn't seems to do the trick sometimes, I think a regex can be implemented to check if its a x pices of number or not, ControlGetText seems to be worst
        Sleep(200)
        Global $VJszam = ClipGet()

        If StringRegExp($VJszam, "(\d{11})", $STR_REGEXPMATCH) = 0 Then ;to check if it worked correctly since the method isnt that reliable
            WinActivate($handle2)
            Sleep(200)
            ControlClick($handle2, "", "[CLASSNN:TDEdit4]", "", 2)
            Send("{CTRLDOWN}c{CTRLUP}")
            Sleep(400)
            Global $VJszam = ClipGet()
        EndIf

        ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn1]")
        WinWaitActive("Kérdés", "", 5)
        If WinWaitActive = 0 Then
            WinActivate($handle2)
            ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn1]")
            WinWaitActive("Kérdés", "", 5)
        EndIf
        Send("{TAB 4}{ENTER}")
        Sleep(200)
        WinActivate("UD Üszi - verzió") ;basicly close the window where the code is generated (so next time it can be reopened and generated again)
;and here is my array:
        Global $arr[$iMax] = [0]
        $iMax = UBound($arr)
        For $i = 1 To $iMax
            ; Check that the array is big enough
            If UBound($arr) = $i Then
                ; Resize the array when $i is equal to the element count in the array to prevent subscript error
                ReDim $arr[$arr[0] + $iMax]
            EndIf

            _ArrayAdd($arr, $VJszam)
            $arr[0] = $i ; update the index count for future reference
        Next

        ; Adjust the array size. This time it is probably downward to the size of
        ; $arr[0] + 1 (remember the first item is $arr[0])
        ReDim $arr[$arr[0] + 1]

        For $i = 1 To $arr[0]
            ConsoleWrite("$arr[" & $i & "]:=" & $arr[$i] & @LF)
        Next
        $iMax = $iMax + 1
    WEnd
EndFunc   ;==>Adattoltes

Thank you for your insight!

Edited by SorryButImaNewbie
Link to comment
Share on other sites

I don't really get it master JohnOne, how can I use _ArrayPop? I tried with _ArrayInsert, but that only lead to this:

$arr[1]:=
$arr[1]:=36036298499
$arr[2]:=36036298499
$arr[1]:=36036698532
$arr[2]:=36036698532
$arr[3]:=36036698532
$arr[1]:=36036198699
$arr[2]:=36036198699
$arr[3]:=36036198699
$arr[4]:=36036198699
 
But with arraypop I would just delete the last one (I'm talking about my first post), and then my array would be completly empty, maybe after a coffee I realize something what you would want to use it  but not yet :)
Link to comment
Share on other sites

After a coffee and a tea I think _arraypop will be very usefull to get the codes and delete them from the array, when I start to use them up in other functions, but see no possible implementation in this problem right now.

Edit : On the original post I see I left out $iMax origins, its a global with the value of 1 for start

Edited by SorryButImaNewbie
Link to comment
Share on other sites

I've only had quick look, but it seems to me that elements of your code might be unnecessarily complex.

And in two lines, you seem to be using $variables before assigning any value to them.

Global $arr[$iMax] = [0]

$iMax doesn't appear to have been assigned or have any value at this point.

Which then makes it the same for $arr as far as a value, in the subsequent line. But I may just be missing something?

$iMax = UBound($arr)
Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Yeah, sorry about the $iMax i just edited when you posted about it :) I'm sure that it can be less complex I worked from a code on the wiki, and I'm not that good that my code shold be simple, but where or how would you make them simplier?

On the array $arr I decleare it just before the for next cyle (I think I should declare it with $iMax at the start of the script (like most globals i use)) but I havent done that yet since It isn't necessary, but most likely now that i think about it I will put it to the beginning of the script

Edited by SorryButImaNewbie
Link to comment
Share on other sites

Use tests, to see if your variables have values at every point - MsgBox, IsArray, _ArrayDisplay, etc.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Thx Saint! I still need time for getting debugging to my blood (I think less and less about my education ^^ ) 

Mycode right now with Global $iMax = 1 Global $arr[$iMax] = [0] at the beginning

Func Adattoltes()
    Local $StartFileSelectFolder = FileSelectFolder("Adja meg az UD_uszi.exe helyét!", "") ;program B, can be different places on differnet user  PCs (just like A)
    Run($StartFileSelectFolder & "\UD_uszi.exe")
    Sleep(3000)
    WinWaitActive("UD ÜSZI")
    ControlSetText("TLoginForm", "UD ÜSZI", "TEdit2", "BALAZSGE")
    Send("{TAB}BALAZSGE")
    Sleep(50)
    Send("{ENTER}")
    WinWaitActive("UD Üszi - verzió")
    Local $handle = WinGetHandle("UD Üszi - verzió")
    Local $1 = 1
    While $1 = 1
        ControlClick($handle, "", "[CLASSNN:TAdvBitBtn13; TEXT:Teszt - Útdíj fizetés adhoc előkészítés]")
        WinWaitActive("Útdíj fizetés adatok")
        Local $handle2 = WinGetHandle("Útdíj fizetés adatok")
        WinMove($handle2, "", 541, 210)
        ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn4]")
        Sleep(200)
        ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn3]")
        Sleep(4000) ;After this button the B program works a bit, dont know better solution for that... anyidea?
        ControlClick($handle2, "", "[CLASSNN:TDEdit8]", "", 1, 320, 10)
        Sleep(200)
        ControlClick($handle2, "", "[CLASSNN:TDEdit8]", "", 1, 300, 15)
        Sleep(200)
        Send("{DOWN}")
        Sleep(200)
        Send("{ENTER}")
        Sleep(200)
        ControlFocus($handle2, "", "TDEdit4") ;here is where the code is generated
        Sleep(1000)
        Send("{CTRLDOWN}c{CTRLUP}") ;this somehow doesn't seems to do the trick sometimes, I think a regex can be implemented to check if its a x pices of number or not, ControlGetText seems to be worst
        Sleep(200)
        Global $VJszam = ClipGet()

        If StringRegExp($VJszam, "(\d{11})", $STR_REGEXPMATCH) = 0 Then ;to check if it worked correctly since the method isnt that reliable
            WinActivate($handle2)
            Sleep(200)
            ControlClick($handle2, "", "[CLASSNN:TDEdit4]", "", 2)
            Send("{CTRLDOWN}c{CTRLUP}")
            Sleep(400)
            Global $VJszam = ClipGet()
        EndIf

        ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn1]")
        WinWaitActive("Kérdés", "", 5)
        If WinWaitActive = 0 Then
            WinActivate($handle2)
            ControlClick($handle2, "", "[CLASSNN:TAdvBitBtn1]")
            WinWaitActive("Kérdés", "", 5)
        EndIf
        Send("{TAB 4}{ENTER}")
        Sleep(200)
        WinActivate("UD Üszi - verzió") ;basicly close the window where the code is generated (so next time it can be reopened and generated again)
        ;and here is my array:
        $iMax = UBound($arr)
        _ArrayDisplay($arr)
        For $i = 1 To $iMax
            ; Check that the array is big enough
            If UBound($arr) = $i Then
                ; Resize the array when $i is equal to the element count in the array to prevent subscript error
                ReDim $arr[$arr[0] + $iMax]
                _ArrayDisplay($arr)
            EndIf

            _ArrayAdd($arr, $VJszam)
            ;Local $new = _ArrayPop($arr)
            ;_ArrayInsert($arr, $i, $VJszam)
            _ArrayDisplay($arr)
            ;_ArrayInsert($arr, 1, $VJszam)
            ;_ArrayPop($arr
            $arr[0] = $i ; update the index count for future reference
        Next
        ReDim $arr[$arr[0] + 1]
        ;   _ArrayAdd($arr, $VJszam)
        ;_ArrayInsert($arr, 1, $VJszam)
        ;_ArrayPop
        ; Adjust the array size. This time it is probably downward to the size of
        ; $arr[0] + 1 (remember the first item is $arr[0])

        For $i = 1 To $arr[0]
            ConsoleWrite("$arr[" & $i & "]:=" & $arr[$i] & @LF)
        Next
        $iMax = $iMax + 1
    WEnd
EndFunc   ;==>Adattoltes

This seems to fill up the array the way I want (I realized that I redecleare the array in the loop basicly with Global $arr[$iMax] = [0]) but _ArrayDisplay seems to work like it overgrow when it enters a new element. I mean that if it was a 4 elements array before it grows to 8 with the last 4 are the same elements, then it gets reduced to 5, then next loop is 10 hich gets reduced to 6) I dont plan to use more then 50 code even in an extrem case so I dont think that this will be anymayor problem, but in other situations this could cause serious problems with bigger data .

My basic question however is: Could my problem originated that I declered the array again in every loop? so I basicly deleted them and only the last added remined?

Edit: and thank you for the input Mister!!!

Edited by SorryButImaNewbie
Link to comment
Share on other sites

Thx Saint! I still need time for getting debugging to my blood (I think less and less about my education ^^ ) 

Mycode right now with Global $iMax = 1 Global $arr[$iMax] = [0] at the beginning

Is that outside the function, presuming yes, because I don't see it?

I think it is good behavior, if you are going to use Global, that you do so for all necessary variables, at the beginning of the script. They can then be accessed from all functions, else use Local, if you don't want that. During the lifetime of your running script, you can keep assigning them the values you need. I like to keep things simple, so I rarely mix declaration with assignment.

Keep testing, using suggested methods, and follow the logic stream, so you can take note of where things change. That will then give you clues about what might need changing or investigating. You can even use writing to an INI file (IniWrite), if you don't want a MsgBox (etc) to take the active window focus ... the values written can be checked later or at suitable program moments.

You should be able to get an understanding of what you questioned, and you may be right in that regard.

Happy to help, in my small way, and we are all learners here ... it is an ongoing process.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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