Jump to content

Write to 2D Array from .ini File?


4b0082
 Share

Go to solution Solved by mikell,

Recommended Posts

Can someone explain why this script won't work? I keep getting the error "==> Missing subscript dimensions in "Dim" statement.:" in relation to $tArray[0][0].

.au3:

#include <Array.au3>
#include <File.au3>
#include <IE.au3>
#include <Inet.au3>
#include <MsgBoxConstants.au3>

Global $tPage = (_FileCountLines ("settings.ini")/3)
Global $tArray[$tPage][2]

For $cPage = 0 to $tPage
   Global $tArray[$cPage][0] = IniRead("settings.ini", "Search" & $cPage, "Name", "Default")
   Global $tArray[$cPage][1] = IniRead("settings.ini", "Search" & $cPage, "Number", "Default")
   $cPage += 1
Next

_ArrayDisplay($tArray)

.ini:

[Search0]
Name=Mike
Number=4
[Search1]
Name=Tom
Number=15
[Search2]
Name=Jill
Number=92
[Search3]
Name=Hilda
Number=5
Link to comment
Share on other sites

  • Solution

It's because in the For loop you redeclare (then reinitialize) $tArray

Remove these Globals and it will work

Edit

BTW remove also $cPage += 1

BTW2

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

Global $tPage = (_FileCountLines ("settings.ini")/3)
Global $tArray[$tPage][2]

For $cPage = 0 to $tPage-1
   $tArray[$cPage][0] = IniRead("settings.ini", "Search" & $cPage, "Name", "Default")
   $tArray[$cPage][1] = IniRead("settings.ini", "Search" & $cPage, "Number", "Default")
Next

_ArrayDisplay($tArray)

:)

Edited by mikell
Link to comment
Share on other sites

  • Moderators

That's an odd way of doing what "it looks like" you're attempting, I think I'd use the built in functions myself.

Subscript error generally means you don't have enough memory (dimensions) allocated.

Your example shows you're going to the max of $tPage, you always need one more dimension in the declaration of the array.

So either this:

Global $tArray[$tPage][2]

Should be:

Global $tArray[$tPage + 1][2] 

Or this:

For $cPage = 0 to $tPage

Should be:

For $cPage = 0 to $tPage - 1 

Dividing the lines by 3, you're bound to get an odd number of lines, and not the results you would expect.

Example (run this):

Local $n_lines = 11
Local $n_dimensions = $n_lines / 3
Local $a_array[$n_dimensions]
ConsoleWrite("n_dimensions = " & $n_dimensions & @CRLF & _
    "Ubound of array dimensions = " & UBound($a_array) & @CRLF)
Edited by SmOke_N
Way too slow

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Obviously the number of lines must be a multiple of 3

To avoid troubles the best way should be to use the integrated ini funcs

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

Global $sections = IniReadSectionNames("settings.ini")
Global $tArray[$sections[0]][2]

For $i = 1 to $sections[0]
   $tArray[$i-1][0] = IniRead("settings.ini", $sections[$i], "Name", "Default")
   $tArray[$i-1][1] = IniRead("settings.ini", $sections[$i], "Number", "Default")
Next

_ArrayDisplay($tArray)
Edited by mikell
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...