Jump to content

ReDim 2D Array


Recommended Posts

I have a 2D array that I need to dynamically enlarge as it loops through some data.. The array is always going to have 2 columns, unknown number of rows..

Dim $servers[0]
While 1 ; list of all servers
    Local $server_name, $server_name_override, $server_id


    If @error Then ExitLoop

    $server_id = json_get($file_json_data, '.servers[' & $a & '].id')
    $server_name = json_get($file_json_data, '.servers[' & $a & '].name')
    $server_name_override = json_get($file_json_data, '.servers[' & $a & '].nameoverride')
    If ($server_name_override <> "") Then $server_name = $server_name_override

    If ($server_name <> "") Then
        $Bound = UBound($servers)
        ReDim $servers[$Bound + 1]
        $servers[$Bound] = $server_name
    EndIf

    $a += 1
WEnd
_ArrayDisplay($servers)

1D array works fine.. However this doesn't..

Dim $servers[0][0]
While 1 ; list of all servers
    Local $server_name, $server_name_override, $server_id


    If @error Then ExitLoop

    $server_id = json_get($file_json_data, '.servers[' & $a & '].id')
    $server_name = json_get($file_json_data, '.servers[' & $a & '].name')
    $server_name_override = json_get($file_json_data, '.servers[' & $a & '].nameoverride')
    If ($server_name_override <> "") Then $server_name = $server_name_override

    If ($server_name <> "") Then
        $Bound = UBound($servers)
        ReDim $servers[$Bound + 1][0]
        $servers[$Bound][0] = $server_id
        $servers[$Bound][1] = $server_name
    EndIf

    $a += 1
WEnd
_ArrayDisplay($servers)

 

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Link to comment
Share on other sites

You're making the array N rows but zero column. Change to this:

Local $servers[0][2]        ; <<=== 2 columns
Local $server_name, $server_name_override, $server_id
Local $a                ; must be declared and possibly initialized

While 1 ; list of all servers
;~     If @error Then ExitLoop  ; pointless test
    $server_id = json_get($file_json_data, '.servers[' & $a & '].id')
    $server_name = json_get($file_json_data, '.servers[' & $a & '].name')
    $server_name_override = json_get($file_json_data, '.servers[' & $a & '].nameoverride')
    If ($server_name_override <> "") Then $server_name = $server_name_override

    If ($server_name <> "") Then
        $Bound = UBound($servers)
        ReDim $servers[$Bound + 1][2]       ; <<=== 2 columns
        $servers[$Bound][0] = $server_id
        $servers[$Bound][1] = $server_name
    EndIf
    $a += 1
WEnd
_ArrayDisplay($servers)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

6 minutes ago, jchd said:

You're making the array N rows but zero column. Change to this:

Local $servers[0][2]        ; <<=== 2 columns
Local $server_name, $server_name_override, $server_id
Local $a                ; must be declared and possibly initialized

While 1 ; list of all servers
;~     If @error Then ExitLoop  ; pointless test
    $server_id = json_get($file_json_data, '.servers[' & $a & '].id')
    $server_name = json_get($file_json_data, '.servers[' & $a & '].name')
    $server_name_override = json_get($file_json_data, '.servers[' & $a & '].nameoverride')
    If ($server_name_override <> "") Then $server_name = $server_name_override

    If ($server_name <> "") Then
        $Bound = UBound($servers)
        ReDim $servers[$Bound + 1][2]       ; <<=== 2 columns
        $servers[$Bound][0] = $server_id
        $servers[$Bound][1] = $server_name
    EndIf
    $a += 1
WEnd
_ArrayDisplay($servers)

 

Worked. I thought I had tried that combination but didn't use local.. Would Dim have caused this? Also noticed I had placed the locals inside the while.. opps. mistook it for a function.

Also the line "If @error Then ExitLoop ; pointless test" is crucial. Without it, it never ends the loop when its done parsing all the json data.

Edited by zone97

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Link to comment
Share on other sites

I knew you had some way to exit the loop but this test is misplaced. You should head first test if there is another entry and test @error right after that.

$server_id = json_get($file_json_data, '.servers[' & $a & '].id')
If @error Then ExitLoop

Local (or Global if required) is preferable to Dim but that doesn't make a difference in this snippet. AFAICT there is only one situation where Dim is useful: when you pass a ByRef variable to a function and this variable will have to become some kind of array before return. Only Dim will allow you to make certain the variable has the correct declaration. Artificial example:

Local $Pi = 3.1415926
; do something here and forget that $Pi used to hold a float

_RandRand($Pi)
_ArrayDisplay($Pi)

; loads variable passed with an array of a random number of random numbers
Func _RandRand(ByRef $a)
    Static $iLimit = 20
    Local $iNb = Random(1, $iLimit, 1)
    Dim $a[$iNb]    ; <<==== Dim is the only direct way to make $a an array (not something else) with the correct dimension(s)
    For $i = 1 To $iNb
        $a[$i - 1] = Random(1, $iLimit, 1)
    Next
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

On 4/25/2018 at 11:18 PM, jchd said:

 

Dim $a[$iNb]    ; <<==== Dim is the only direct way to make $a an array (not something else) with the correct dimension(s)
   

 

Hi,

As a matter of interest learning, wouldn't 

Local $a[$iNb] 

achieve same result?

Link to comment
Share on other sites

Definitely no. That would create a local variable with no relationship with the ByRef variable.

One can still avoid Dim in this case at the price of creating a distinct local variable and assigning it to the ByRef variable before return. That uselessly doubles the memory footprint of the function (important when ByRef variable is going to hold large array) and that wastes cycles w/o any benefit.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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