Jump to content

Recommended Posts

Posted

I'm trying to make an extensible function to read a comma-delimited file into a 2d array. Trouble is, it appears as though the runtime error checking does not allow for a variable to determine the size of the array. I get the error "Array variable has incorrect number of subscripts or subscript dimension range exceeded."

I originally passed the array in with ByRef to be memory efficient, but ReDim gave me the error. Now I have the variables explicitly defined in the function, to no avail.

My script works when I define the array with ample elements and no ReDim, and when I use constants within ReDim. I have even tried filling a variable with a number first, and that didn't work. Any ideas? Here's the code.

Func ArrayTo2d($sFileName)
    Local $aTempArray
    Local $aNewArray[1][1]
    Local $iCounter, $iPos, $iTempsize
    
    If Not _FileReadToArray($sFileName, $aTempArray) Then
        SetError( 1 )
        Return 0
    EndIf
    $iTempsize = $aTempArray[0]
    ReDim $aNewArray[$iTempsize][2]
    $aNewArray[0][0] = $aTempArray[0]
    For $iCounter = 1 To $aTempArray[0]
        $iPos = StringInStr($aTempArray[$iCounter], ",")
        $aNewArray[$iCounter][0] = StringMid($aTempArray[$iCounter], 1, $iPos - 1)
        $aNewArray[$iCounter][1] = StringMid($aTempArray[$iCounter], $iPos + 1)
    Next
    Return $aNewArray
    
EndFunc

Any help would be appreciated.

-Thanks,

Paul

Posted (edited)

Any help would be appreciated.

Paul,

_FileReadToArray uses StringSplit() internally. StringSplit() returns an array that contains the delimited strings AND it sets $array[0] to the number of strings found, NOT the array size! So, the size of your array $aTempArray is $aTempArray[0] + 1 !!

Now, you use the value of $aTempArray[0] to redim $aNewArray. Then you use a for loop to iterate over all array values. However, this will trigger an error when $iCount reaches $aTempArray[0] (==3). Reason and Example:

$aTempArray[0] = 3

$aTempArray[1] = "xyz"

$aTempArray[2] = "xyz"

$aTempArray[3] = "xyz"

ReDim $aNewArray[$iTempsize][2] gives $aNewArray[3][2], so we have:

$aNewArray[0][]

$aNewArray[1][]

$aNewArray[2][]

As you see there is no $aNewArray[3][], which causes the error when $iCounter reaches the value 3 ( == $aTempArray[0]).

Solution: Redim your new array with $aTempArray[0]+1 ==> ReDim $aNewArray[$iTempsize+1][2]

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Posted

_FileReadToArray uses StringSplit() internally. StringSplit() returns an array that contains the delimited strings AND it sets $array[0] to the number of strings found, NOT the array size! So, the size of your array $aTempArray is $aTempArray[0] + 1 !!

Solution: Redim your new array with $aTempArray[0]+1 ==> ReDim $aNewArray[$iTempsize+1][2]

Cheers

Kurt

<{POST_SNAPBACK}>

Kurt,

Thank you. It did not occur to me that the number in the zero element was the number of strings, thank you for the outstanding explanation. I made that quick mod, and it ran flawlessly.

Thanks for your time,

Paul

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
  • Recently Browsing   0 members

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