Jump to content
tk1

I don't understand why this works for my array

Recommended Posts

tk1

Hello,

I created a small script to figure out how to keep only the last 5 lines of a regularly updated text file.  I have it working exactly how I want it to, however there's one piece that I don't understand WHY it's working because it's not doing what I would have expected it to.  Would someone explain to me why it does work?  First, the whole script for context..

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Fileversion=0.0.0.1
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <Constants.au3>

Global $sCheck = "C:\Data\_Check.txt"

Opt("GUIOnEventMode", 1)

Global $gTest = GUICreate("Array test", 254, 150, -1, 100, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "fuClose")
GUICtrlSetBkColor(-1, "-2")
Global $sStatus = GUICtrlCreateLabel("Click the button to exit.", 14, 100, 150, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
Global $btnExit = GUICtrlCreateButton("Exit", 162, 110, 78, 29, -1, -1)
GUICtrlSetOnEvent(-1, "fuClose")
GUICtrlSetFont(-1, 10, 400, 0, "Arial")

GUISetState(@SW_SHOW, $gTest)


While 1
    fuTest()
WEnd

Func fuTest()
    Local $sTime
    Local $sLineNumber = 0

    While 1
        $sTime = @HOUR & ":" & @MIN & ":" & @SEC & @TAB & @YEAR & "." & @MON & "." & @MDAY & _
                @TAB & "OK"

        If FileExists($sCheck) Then
            Local $aArray = FileReadToArray($sCheck)
            If @extended > 5 Then
                $sLineNumber = @extended - 4 ; Get line number of the 4th to last line.
            Else
                $sLineNumber = 1
            EndIf
            FileDelete($sCheck)
            If UBound($aArray) <5 Then
                FileWriteLine($sCheck, $aArray[0])
            EndIf
            For $i = $sLineNumber To UBound($aArray) - 1
                FileWriteLine($sCheck, $aArray[$i])
            Next
        Else
        EndIf
        FileWriteLine($sCheck, $sTime)
        Sleep(10000)

    WEnd
EndFunc   ;==>fuTest

Func fuClose()
    Exit
EndFunc   ;==>fuClose

 

Now, for the part that's working but I don't understand why:

If UBound($aArray) <5 Then
                FileWriteLine($sCheck, $aArray[0])
            EndIf

 

My expectation is that this If/Then would only write the first element in the array, however it seems to be writing the first three elements (elements 0, 1, and 2) if they exist.

Would someone mind explaining this?  I looked at the array tutorial in the Wiki, but IMHO it didn't go into detail about how arrays and their indices work, and quickly moved into more about how to program around the array.  (Apologies if that opinion offends anyone, it's not meant to.)

 

Thanks,

tk1

Share this post


Link to post
Share on other sites
Jos
5 minutes ago, tk1 said:

My expectation is that this If/Then would only write the first element in the array, however it seems to be writing the first three elements (elements 0, 1, and 2) if they exist.

How have you determined that this is the case as I see no debugging statements?

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
tk1
Posted (edited)

Hi Jos,

Thank you for the fast reply!  I'm a sort of hobbyist programmer (the kind you guys probably don't care for because I probably only know enough to be dangerous!).

Honestly, I don't know or understand (I don't think) debugging statements.  I based my conclusion on trial and error (many) , and the use of console writes/msgboxes.  I cleaned those out of the script to make it easier (?) to read.

Thanks,

tk1

Edited by tk1

Share this post


Link to post
Share on other sites
Subz

The loop below it writes the additional lines, comment out the FileWriteLines and you'll see that its working as expected, in fact if comment out the line you posted you'll find that it displays exactly the same behaviour, so isn't required or maybe I'm missing something 8 a.m. I need to get some sleep before work at 9:30, cya.

Share this post


Link to post
Share on other sites
tk1

Turns out I didn't put consolewrites and msgboxes in that particular section (don't ask why, I don't know), but instead put them in other areas at different times.  I just now added a consolewrite(UBound($aArray)) to that section, and I get "1234" with the 10 sleep seconds between each digit.  I changed it to $aArray[0], and I only ever get the first 6 characters of the first (?) element (e.g.  "13:57:" ).

I get the 1, 2, 3, and 4, because they're all less than 5.  What I don't get what is written for element 0.  I thought the first element, 0, was for the count of elements in the array, and if that's the case it would have only given me a number, not the contents of the variable $sTime as the first element.  If that makes any sense, that is what is confusing me (even if it doesn't make sense, it still confuses me! haha).

Just a side note of humor, I wrote this whole script completely on my own, not taking it from anywhere or anyone, and yet I still don't understand this piece of it.

 

Thanks,

tk1

Share this post


Link to post
Share on other sites
tk1
Posted (edited)
5 minutes ago, Subz said:

...in fact if comment out the line you posted you'll find that it displays exactly the same behaviour, so isn't required....

Funny you should mention that, I did that when writing my last response to Jos just to see if it was even needed because I was thinking maybe it wasn't, too.  (I must have gotten your mind transmission.)  When I did that, it stopped working correctly and would only ever write one line of the array in the _Check.txt file.

Thanks,

tk1

Edited by tk1

Share this post


Link to post
Share on other sites
wolflake
On ‎6‎/‎14‎/‎2018 at 3:58 PM, tk1 said:

I thought the first element, 0, was for the count of elements in the array, and if that's the case it would have only given me a number, not the contents of the variable $sTime as the first element. 

According to the help file FileReadToArray doesn't put the number of elements read in the array but in the @extended which you use but you seem to also expect it in the array which may be why you are confused.

Hope this helps. :)

Share this post


Link to post
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

×