Jump to content

Issue trying to get data from an array


 Share

Go to solution Solved by Geir1983,

Recommended Posts

Hi, guys - I've been trying to get this piece of code to work and I simply cannot figure out why this error is occuring, so I'd appreciate a second pair of eyes if you have time.

I'm trying to write a script that will parse an XML file for timecodes and add a specified number of seconds to each timecode, but I can't access the data that's in the array in order to do the math. I've re-read the Array UDF a dozen times and I just can't see what I'm doing wrong.

Right now I have the script read the XML into an array, and I'm using StringRegExp to find the timestamps and display those in a new 1d array with each set of digits in a different row of the array - and that all works beautifully. But when I try to actually use the data in the array, I'm getting the dreaded "Subscript used on non-accessible variable" error. I'm declaring all of the variables outside of the function, so I know that's not the issue. I also can't ReDim the array, getting the error "ReDim" used without an array variable," even though the variable is declared and is used in the statement just before it.

The errors start in Row 48. Any ideas?

#include <File.au3>

#include <Array.au3> ; Just for display <<<<<<<<<<<<<<<<<<<<<

#include <Excel.au3>

#include <MsgBoxConstants.au3>

HotKeySet("{ESC}", "Terminate")

Func Terminate()
    Exit
EndFunc   ;==>Terminate

$path = InputBox("Input Path", "Enter Path to Files:" & @CRLF & "Press ESC at any time to cancel the script.")
$pathexists = FileExists($path)

If $pathexists = 0 Then
   MsgBox($MB_SYSTEMMODAL, "Path Error", "File Path Does Not Exist.")
   Exit
EndIf

Global $xmlfiles = _FileListToArray($path, "*.xml", 1)
Global $aArray[0]
Global $aInTC[0]
Global $aInHr
Global $aInMin
Global $aInSec
Global $aInFr

For $b = 1 To UBound($xmlfiles) - 1
    switch $xmlfiles[$b]
      case StringInStr($xmlfiles[$b], ".xml") = 0
         MsgBox($MB_SYSTEMMODAL, "Error", "No .xml files found in:" & @CRLF & $path)
            Exit
         Case Else
            _TimecodeAdd()
         EndSwitch
Next

Func _TimecodeAdd()
    _FileReadToArray($path & "\" & $xmlfiles[$b], $aArray)
    _ArrayDisplay($aArray,"")

    For $z = 1 To $aArray[0] - 1
        $aInTC = StringRegExp($aArray[$z], 'InTC="([0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{2})"',3)
        _ArrayDisplay($aInTC, "") ;<--This displays the proper array of digits so long as nothing tries to call a cell after this
        ;ReDim $aInTC[4] ;<-- Causes "ReDim Used without variable error"

        MsgBox($MB_SYSTEMMODAL, "Does It Work?", $aInTC[0]) ;<--Causes Subscript error
        ;ConsoleWrite($ainTC[0] & ":" & $ainTC[1] & ":" & $ainTC[2] & ":" & $ainTC[3]) ;<--Causes Subscript error
    Next

EndFunc
Edited by yevlar
Link to comment
Share on other sites

  • Solution

Try to check the return from StringRegExp for error before you continue

#include <File.au3>

#include <Array.au3> ; Just for display <<<<<<<<<<<<<<<<<<<<<

#include <Excel.au3>

#include <MsgBoxConstants.au3>

HotKeySet("{ESC}", "Terminate")

Func Terminate()
    Exit
EndFunc   ;==>Terminate

$path = InputBox("Input Path", "Enter Path to Files:" & @CRLF & "Press ESC at any time to cancel the script.")
$pathexists = FileExists($path)

If $pathexists = 0 Then
   MsgBox($MB_SYSTEMMODAL, "Path Error", "File Path Does Not Exist.")
   Exit
EndIf

Global $xmlfiles = _FileListToArray($path, "*.xml", 1)
Global $aArray[0]
Global $aInTC[0]
Global $aInHr
Global $aInMin
Global $aInSec
Global $aInFr

For $b = 1 To UBound($xmlfiles) - 1
    switch $xmlfiles[$b]
      case StringInStr($xmlfiles[$b], ".xml") = 0
         MsgBox($MB_SYSTEMMODAL, "Error", "No .xml files found in:" & @CRLF & $path)
            Exit
         Case Else
            _TimecodeAdd()
         EndSwitch
Next

Func _TimecodeAdd()
    _FileReadToArray($path & "\" & $xmlfiles[$b], $aArray)
    _ArrayDisplay($aArray,"")

    For $z = 1 To $aArray[0] - 1
        $aInTC = StringRegExp($aArray[$z], 'InTC="([0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{2})"',3)
    IF @error Then ContinueLoop
        _ArrayDisplay($aInTC, "") ;<--This displays the proper array of digits so long as nothing tries to call a cell after this
        ReDim $aInTC[4] ;<-- Causes "ReDim Used without variable error"

        MsgBox($MB_SYSTEMMODAL, "Does It Work?", $aInTC[0]) ;<--Causes Subscript error
        ConsoleWrite($ainTC[0] & ":" & $ainTC[1] & ":" & $ainTC[2] & ":" & $ainTC[3]) ;<--Causes Subscript error
    Next

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