Sign in to follow this  
Followers 0
kor

array loop problem. if array 1 is smaller than array 2 I get subscript errors

4 posts in this topic

This code is working great

IF array "genesis" has more rows than array "ad"

If array "genesis" has less rows then I get errors "Array variable has incorrect number of subscripts or subscript dimension range exceeded"

on lines 68-73 which is my "add" loop.

I've spent an entire day trying to figure out what the problem is, but I'm at a loss. Can anyone point me in the right direction?

You can use the sample arrays I've provided.

#include <File.au3>
#include <Array.au3>

Global $aRaw, $aGenesis, $aAD

_FileReadToArray(@ScriptDir & "genesis.txt", $aRaw)
Global $aGenesis[$aRaw[0] + 1][5]
$aGenesis[0][0] = $aRaw[0]
For $i = 1 to $aRaw[0]
    $aTemp = StringSplit(StringRegExpReplace($aRaw[$i], """", ""), ",", 2)
    For $n = 0 To 4
        $aGenesis[$i][$n] = $aTemp[$n]
    Next
    Assign($aTemp[2], $i, 1)
Next
;_ArrayDisplay($aGenesis, "genesis")
ConsoleWrite("genesis has " & UBound($aGenesis, 1) & " rows" & @CR)

_FileReadToArray(@ScriptDir & "ad-lessrows.txt", $aRaw)
Global $aAD[$aRaw[0] + 1][5]
$aAD[0][0] = $aRaw[0]
For $i = 1 To $aRaw[0]
    Local $aTemp = StringSplit($aRaw[$i], ",", 2)
    For $n = 0 To 4
        $aAD[$i][$n] = $aTemp[$n]
    Next
Next
;_ArrayDisplay($aAD, "ad")
ConsoleWrite("ad has " & UBound($aAD, 1) & " rows" & @CR)

$a1 = $aAD
$a2 = $aGenesis
$aClean = _Compare($a1, $a2)
_ArrayDisplay($aClean)

Func _Compare(ByRef $aAD, ByRef $aGenesis)
    Local $aRaw[UBound($aAD, 1) - 1][6], $iRaw = 0, $sTemp
    For $i = 1 To $aAD[0][0]
        $sTemp = $aAD[$i][2]
        If IsDeclared($sTemp) Then
            $sTemp = Eval($sTemp)
            For $n = 0 To 4
                If $aAD[$i][$n] <> $aGenesis[$sTemp][$n] Then
                    $aRaw[$iRaw][0] = $aGenesis[$sTemp][0]
                    $aRaw[$iRaw][1] = $aGenesis[$sTemp][1]
                    $aRaw[$iRaw][2] = $aGenesis[$sTemp][2]
                    $aRaw[$iRaw][3] = $aGenesis[$sTemp][3]
                    $aRaw[$iRaw][4] = $aGenesis[$sTemp][4]
                    $aRaw[$iRaw][5] = "Move"
                    $iRaw += 1
                    ExitLoop
                EndIf
            Next
            $aGenesis[$sTemp][2] = ""
        Else
            $aRaw[$iRaw][0] = $aAD[$i][0]
            $aRaw[$iRaw][1] = $aAD[$i][1]
            $aRaw[$iRaw][2] = $aAD[$i][2]
            $aRaw[$iRaw][3] = $aAD[$i][3]
            $aRaw[$iRaw][4] = $aAD[$i][4]
            $aRaw[$iRaw][5] = "Delete"
            $iRaw += 1
        EndIf
    Next
    For $i = 1 to $aGenesis[0][0]
        If $aGenesis[$i][2] Then
            $aRaw[$iRaw][0] = $aGenesis[$i][0]  ;<<<<<<<<<< problem area
            $aRaw[$iRaw][1] = $aGenesis[$i][1]
            $aRaw[$iRaw][2] = $aGenesis[$i][2]
            $aRaw[$iRaw][3] = $aGenesis[$i][3]
            $aRaw[$iRaw][4] = $aGenesis[$i][4]
            $aRaw[$iRaw][5] = "Add"
            $iRaw += 1
        EndIf
    Next
    ReDim $aRaw[$iRaw][6]
    Return $aRaw
EndFunc   ;==>_Compare

genesis.txt

ad-lessrows.txt

ad-morerows.txt

Share this post


Link to post
Share on other sites



I'm not exactly sure what this thing is supposed to do, but I found the array mismatch and used @error to bypass the loop if you run out of data to read.

#include <File.au3>
#include <Array.au3>

Global $aRaw, $aGenesis, $aAD

_FileReadToArray(@ScriptDir & "genesis.txt", $aRaw)
Global $aGenesis[$aRaw[0] + 1][5]
$aGenesis[0][0] = $aRaw[0]
For $i = 1 to $aRaw[0]
    $aTemp = StringSplit(StringRegExpReplace($aRaw[$i], """", ""), ",", 2)
    For $n = 0 To 4
        $aGenesis[$i][$n] = $aTemp[$n]
        If @error Then  ; Begin my modifications
         ExitLoop
         EndIf
      Next
      If not @error Then ; End my modifications
    Assign($aTemp[2], $i, 1)
    EndIf
Next
;_ArrayDisplay($aGenesis, "genesis")
ConsoleWrite("genesis has " & UBound($aGenesis, 1) & " rows" & @CR)

_FileReadToArray(@ScriptDir & "ad-lessrows.txt", $aRaw)
Global $aAD[$aRaw[0] + 1][5]
$aAD[0][0] = $aRaw[0]
For $i = 1 To $aRaw[0]
    Local $aTemp = StringSplit($aRaw[$i], ",", 2)
    For $n = 0 To 4
        $aAD[$i][$n] = $aTemp[$n]
    Next
Next
;~ _ArrayDisplay($aAD, "ad")
ConsoleWrite("ad has " & UBound($aAD, 1) & " rows" & @CR)

$a1 = $aAD
$a2 = $aGenesis
$aClean = _Compare($a1, $a2)
_ArrayDisplay($aClean)

Func _Compare(ByRef $aAD, ByRef $aGenesis)
    Local $aRaw[UBound($aAD, 1) - 1][6], $iRaw = 0, $sTemp
    For $i = 1 To $aAD[0][0]
        $sTemp = $aAD[$i][2]
        If IsDeclared($sTemp) Then
            $sTemp = Eval($sTemp)
            For $n = 0 To 4
                If $aAD[$i][$n] <> $aGenesis[$sTemp][$n] Then
                    $aRaw[$iRaw][0] = $aGenesis[$sTemp][0]
                    $aRaw[$iRaw][1] = $aGenesis[$sTemp][1]
                    $aRaw[$iRaw][2] = $aGenesis[$sTemp][2]
                    $aRaw[$iRaw][3] = $aGenesis[$sTemp][3]
                    $aRaw[$iRaw][4] = $aGenesis[$sTemp][4]
                    $aRaw[$iRaw][5] = "Move"
                    $iRaw += 1
                    ExitLoop
                EndIf
            Next
            $aGenesis[$sTemp][2] = ""
        Else
            $aRaw[$iRaw][0] = $aAD[$i][0]
            $aRaw[$iRaw][1] = $aAD[$i][1]
            $aRaw[$iRaw][2] = $aAD[$i][2]
            $aRaw[$iRaw][3] = $aAD[$i][3]
            $aRaw[$iRaw][4] = $aAD[$i][4]
            $aRaw[$iRaw][5] = "Delete"
            $iRaw += 1
        EndIf
    Next
    For $i = 1 to $aGenesis[0][0]

        If $aGenesis[$i][2] Then
            $aRaw[$iRaw][0] = $aGenesis[$i][0]  ;<<<<<<<<<< problem area
            $aRaw[$iRaw][1] = $aGenesis[$i][1]
            $aRaw[$iRaw][2] = $aGenesis[$i][2]
            $aRaw[$iRaw][3] = $aGenesis[$i][3]
            $aRaw[$iRaw][4] = $aGenesis[$i][4]
            $aRaw[$iRaw][5] = "Add"
            $iRaw += 1
        EndIf
    Next
    ReDim $aRaw[$iRaw][6]
    Return $aRaw
EndFunc   ;==>_Compare

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

interesting solution but it doesn't seem to solve the problem is either array is substantially different in size than the other. If the arrays are different by maybe 10 rows or so it works, but if either array is lets say 500 rows bigger or smaller than the other array I'm still getting dimension errors during the "add" loop.

I tried to use your @error solution in that loop but no luck.

Share this post


Link to post
Share on other sites

I believe I have found the problem. I noticed that this code on line 35

Local $aRaw[UBound($aAD, 1) - 1][6]

was defining the final raw array as the size of the AD array. If this array were smaller than the Genesis array by any substantial margin then I would get dimension errors. I also tried changing it to use the size of the Genesis array. But I noticed that if that array were substantially bigger or smaller than the AD array I would get the same error.

This I am using this code. I figure since I am ReDim'ing the array at the end anyway, who cares how big it is while I'm working with it.

Local $aRaw[(UBound($aGenesis, 1) + (UBound($aAD, 1))) - 1][6]

This way it will be the size of both arrays combined, so it won't matter how big either array is since the new raw array will always have enough room to work with whatever data might come from either array.

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
Sign in to follow this  
Followers 0