Jump to content

Error: Subscript used with non-Array variable


Jekkyl
 Share

Recommended Posts

Hello all,

Total newbie Autoit user here and I know this script is currently way out of my depth. I have had to make some date changes else where in the script to get it to work and all runs fine on my test box and on the server I run this on....but only if I am logged in! I need to be able to schedule this to run and cant for the life of me figure this out.

I have trawlled through the help files and read a heap of posts about this error and trying to use @error to debug it but I just cannot seem to solve the problem.

The error is at the line

Local $array[$fArray[0]][10]

Local $array[$fArray^ERROR

Error: Subscript used with non-Array variable

Any help at all would be much apprecited!

Dim $fArray

    _FileReadToArray($spoolFileName, $fArray)

    Local $array[$fArray[0]][10]

    $useLine = 0

    For $x = 1 To $fArray[0] Step 1
        $line = $fArray[$x]
        $line = StringStripWS($line, 7)
        $lineArray = StringSplit($line, " ")

        If StringLeft($lineArray[1], 2) = "02" Then

            _ArrayDelete($lineArray, 0)

            $dateTime = $lineArray[1] & " " & $lineArray[2]

            $lineArray[1] = $dateTime

            ConsoleWrite("Date: " & $lineArray[1] & " Time: " & $lineArray[2] & " $dateTime: " & $dateTime & " $lineArray[1] = " & $lineArray[1] & @CRLF)

            For $y = 2 To UBound($lineArray) - 2 Step 1

                $lineArray[$y] = $lineArray[$y + 1]
            Next

            _ArrayDelete($lineArray, UBound($lineArray))

            For $z = 0 To UBound($lineArray) - 1 Step 1
                $array[$useLine][$z] = $lineArray[$z]
            Next
            $useLine = $useLine + 1
        EndIf

    Next




    FileClose($queryFile)
    FileDelete($queryFileName)
    FileDelete($spoolFileName)


    $numRows = UBound($array)
    $numCols = UBound($array, 2)

    FileWriteLine($file, "CARD NUMBER,ENTRY UNIT,ENTRY TIME,EXIT UNIT,EXIT TIME,STAY TIME,PAY UNIT,PAY AMOUNT")
Link to comment
Share on other sites

  • Moderators

Jekkyl,

Welcome to the AutoIt forum. :x

The error is telling you that $fArray is not an array. That would imply that the _FileReadToArray function is not returning an array - so you need to see if that is in fact the case (just check @error immediately after the function call) and then see why it failed.

Once you have that information (either the file could not be opened or it could not be split) we can look at why. Obvious things to check are: is the path of the file correct and does it exist; does it have lines to read; does it have peculiar line endings. :shifty:

Looking a little further into your script, I can see another problem looming. If you want to delete elements from an array while looping through it, you must begin at the end and move upwards - otherwise your array becomes smaller than the original size and you will run into an error when you try to access elements which no longer exist. But let us leave that until we have an array to loop through! :nuke:

Let us know how you get on with finding a solution to the first problem. :P

M23

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

Thanks for the reply Melba23!

So looking at what you were saying I tracked the problem down....but I dont know what is causing it......

The start of my Script before the array is

#include "Array.au3"
#include "Date.au3"
#include "File.au3"


MainFunc()

Func MainFunc()

    ; Subtract 1 day from today
    $mday = _DateAdd('d', -1, _NowCalcDate())
    $mday = StringReplace($mday, "/", ".")
    $mday = StringRight($mday, 2) & '.' & StringMid($mday, 6, 2) & '.' & StringLeft($mday, 4)




    $outputName = "CardTrack_" & $mday & ".csv"

    $file = FileOpen("C:\CardTrack\" & $outputName, 2)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to create file.", 5)
        Exit
    EndIf

    $queryFileName = "C:\CardTrack\query.sql"
    $spoolFileName = "C:\CardTrack\output.txt"

    $queryFile = FileOpen($queryFileName, 10)

    $sql_query = "SELECT CEPAN, XTARIFFTIME, STRANSTYPE, SDEVICE, LTOTALAMOUNT FROM LOGGVORGANG WHERE XTARIFFTIME >= TO_DATE('" & $mday & "', 'DD-MM-YY') ORDER BY CEPAN, XTARIFFTIME ASC;"


    $line1 = "Alter Session set NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS';"
    $line2 = "Spool " & $spoolFileName & ";"
    $line3 = "" & $sql_query
    $line4 = "Spool off;"
    $line5 = "quit;"

    ShellExecute("sqlplus", "db/db")
    Sleep(3000)
    Send($line1)
    Send("{ENTER}")
    Sleep(3000)
    Send($line2)
    Send("{ENTER}")
    Sleep(3000)
    Send($line3)
    Send("{ENTER}")
    Sleep(60000)
    Send($line4)
    Send("{ENTER}")
    Sleep(3000)
    Send($line5)
    Send("{ENTER}")

So what appears to be happening is that after the shellexecute runs and the shell opens none of the $line commands then get sent to the shell unless someone is logged in!

Does anyone know of a way around this problem?

Edited by Jekkyl
Link to comment
Share on other sites

I frequently get this error when I run my scripts by double clicking them. This is due to me moving my "includes" folder. If I run them from Scite, they run fine.

As an experiment to find if this is the case, copy all your "includes" into your script folder and try to run it. If it runs OK then your configuration is messed up.

Link to comment
Share on other sites

I don't know what the problem is, but the following line is incorrect:

_ArrayDelete($lineArray, UBound($lineArray))

If you want to delete the last element of an array you need to subtract 1 from the Ubound value: because the first element is zero and not 1.

_ArrayDelete($lineArray, UBound($lineArray) -1)

So what appears to be happening is that after the shellexecute runs and the shell opens none of the $line commands then get sent to the shell unless someone is logged in!

I'm guessing a bit here because I'm not familiar with sqlplus and I really don't know what you are trying to do. Perhaps using Send is not going to work in this situation. ConsoleWrite might be what you need. I'm not really sure about it though. Edited by czardas
Link to comment
Share on other sites

  • 3 weeks later...

I frequently get this error when I run my scripts by double clicking them. This is due to me moving my "includes" folder. If I run them from Scite, they run fine.

As an experiment to find if this is the case, copy all your "includes" into your script folder and try to run it. If it runs OK then your configuration is messed up.

The problem is actually the opposite of this, when I manually click it to run it the script works perefectly fine. Its when I schedule the script to run and I am not directly logged onto the server. The SQLPlus window opens but the commands do not appear to get sent to it. I did try this anyway but no joy!

I don't know what the problem is, but the following line is incorrect:

_ArrayDelete($lineArray, UBound($lineArray))

If you want to delete the last element of an array you need to subtract 1 from the Ubound value: because the first element is zero and not 1.

_ArrayDelete($lineArray, UBound($lineArray) -1)

I changed this and it hasn't helped...saying that this line comes after the actual problem.

I'm guessing a bit here because I'm not familiar with sqlplus and I really don't know what you are trying to do. Perhaps using Send is not going to work in this situation. ConsoleWrite might be what you need. I'm not really sure about it though.

I tried ConsoleWrite but it dosn't send anything to the window so I assume it doesnt accept STDOUT streams. Basically all I am doing is opening a sqlplus window and pulling data from an oracle database, then creating a CSV with the information. Works 100% when I am logged into the server just not when I schedule it so it is very frustrating!
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...