Jump to content

Read INI file to Lisbox?


Recommended Posts

I have searched and searched and can't find an up-to-date example of how to accomplish this. All threads I found were over 3 years old and used GUIList.au3 in the includes with the _GUICtrlListAddItem function.

I have the following code using GUIListbox.au3 in my includes:

Func _populateRemedy()
    $READ = IniReadSection(@ScriptDir & "\remedy_assistant.ini", "SUMMARY")
    $READ2 = IniReadSection(@ScriptDir & "\remedy_assistant.ini", "NOTES")

For $i = 1 To $READ[0][0]
   _GUICtrlListBox_AddString($lstSummary, $READ[$i][1])
Next

For $i = 1 To $READ2[0][0]
   _GUICtrlListBox_AddString($lstNotes, $READ2[$i][1])
Next


EndFunc

The code simply looks at 2 separate sections of an INI file and reads them into two different listboxes. However, it keeps throwing the error "Subscript used with non-Array variable." on this line:

For $i = 1 To $READ[0][0]
. What am I doing wrong here?

Also my example INI is in the following format:

[SUMMARY]
# Item 1
# Item 2

[NOTES]
# item1
# item2

Any help is extremely appreciated.

Link to comment
Share on other sites

im not sure because i never do it, but i think the array must be 1D and not 2D

[/code]
EDITED: Sorry my bad... i will look into...

Your INI Format is wrong... you need to format this way.
[code][SUMMARY]
Item = 1
Item = 2

Now in the $read array you have

$read[0][0] = 2
$read[0][1] = 
$read[1][0] = Item
$read[1][1] = 1
$read[2][0] = Item
$read[2][1] = 2

$READ = IniReadSection(@ScriptDir & "\test.ini", "SUMMARY")

For $i = 0 To $READ[0][0]
    for $e = 0 to $READ[0][0] -1
   consolewrite("$read["&$i&"]["&$e&"] = "&$READ[$i][$e]&@CRLF)
   next
Next
Edited by monoscout999
Link to comment
Share on other sites

Your .ini file is formatted incorrectly. The format for an ini file is:

[section name] <- Note the brackets

Key1=Value1 <- Note the Equals sign and unique key names

Key2=Value2

[section2 name]

Key1=Value1 <-Key names can be duplicated but ONLY in a different section

Key2=Value2

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ok, I corrected my .ini file structure, but and still getting the same error. I don't need 2D array here. All I need is to get the items to the right of the = sign in the ini file and add them to the listbox. Thanks.

Link to comment
Share on other sites

Ok, I corrected my .ini file structure, but and still getting the same error. I don't need 2D array here. All I need is to get the items to the right of the = sign in the ini file and add them to the listbox. Thanks.

You may not need a 2D array, but IniReadSection returns one anyways. :)

Your example in your first post should work to do what you need, if your ini file is being read correctly. I would stick an _ArrayDisplay after each read line to see if the $Read and $Read2 arrays are being populated correctly, if nothing shows up then they're not arrays. If they display properly then I'm at a loss, unless your ini file is still malformed.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 2 weeks later...

I am still getting

Subscript used with non-Array variable
error message using this code:
Func _populateRemedy()
    $READ = IniReadSection("C:\Program Files\remedy_assistant.ini", "SUMMARY")
    $READ2 = IniReadSection("C:\Program Files\remedy_assistant.ini", "NOTES")

For $i = 0 To $READ[0][0] 
    For $e = 0 to $READ [0][0] -1
         consolewrite("$read["&$i&"]["&$e&"] = "&$READ[$i][$e]&@CRLF)
  ; _GUICtrlListBox_AddString($lstSummary, $READ[$i][1])
    Next
Next

For $i = 0 To $READ2[0][0] -1
  For $e = 0 to $READ [0][0] -1
       consolewrite("$read["&$i&"]["&$e&"] = "&$READ[$i][$e]&@CRLF)
   ;_GUICtrlListBox_AddString($lstNotes, $READ[$i][)
    Next
Next

Error is happening on

$READ[0][0]
on the
For $i = 0 To $READ[0][0]
line. I did not think it would be this hard to populate a listbox. I have done this in VB6 in the past with much more ease. Any help appreciated.
Link to comment
Share on other sites

$READ[0][0] will be the actual data in your ini file. I think you need to use Ubound to determine the size of the array. Something like Ubound($READ[0][0]).

I think there is an easier way. It's not as fancy as arrays, but it works:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <GUIListBox.au3>


$Form1 = GUICreate("Form1", 319, 164, -1, -1)
$List1 = GUICtrlCreateList("", 8, 8, 289, 97)
$Button1 = GUICtrlCreateButton("Populate", 216, 120, 75, 25)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _populateRemedy()
    EndSwitch
WEnd


Func _populateRemedy()
$i = 1
Do
    $tempRead1 = IniRead("remedy_assistant.ini", "SUMMARY", "value" & $i, "NOTFOUND")
    $tempRead2 = IniRead("remedy_assistant.ini", "NOTES", "value" & $i, "NOTFOUND")
    If $tempRead1 = "NOTFOUND" Then
        ExitLoop
    EndIf
    _GUICtrlListBox_AddString($List1, $tempRead1 & " - " & $tempRead2)
    $i += 1
Until $tempRead1 = "NOTFOUND"
EndFunc

Also, this is how I formatted the ini file:

[SUMMARY]
value1=Item 1
value2=Item 2

[NOTES]
value1=item1
value2=item2
Edited by sleepydvdr

#include <ByteMe.au3>

Link to comment
Share on other sites

So your IniReadSection() is failing. Make sure the path is correct and that the section is there.

Look in the helpfile for what IniReadSection() returns on failure so you can prevent crashing like this.

Some other things:

$i = 0 To $READ2[0][0] -1

What's with the -1?

For $e = 0 to $READ [0][0] -1

I don't see what that loop is supposed to do.

You know that $e can only be 0-1, and if you didn't you would use UBound() to find out. $READ[0][0] doesn't do anything here.

I did not think it would be this hard to populate a listbox. I have done this in VB6 in the past with much more ease. Any help appreciated.

It's not hard. I don't know why you're having so much trouble.

With all this trouble, maybe now would be time to start being serious about getting help. Post a reproducer (a short complete fully working example, including the ini-file) so we know what you're doing.

Guessing can be fun, but it doesn't really help the situation.

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