Jump to content

Ini to Array


ripdad
 Share

Recommended Posts

Two scripts to get an ini into an array

Updated: April 12, 2010

- Fixed several issues -

; ini to array v2.5 (row or column mode) Two scripts in one.
; by ripdad, feb. 17, 2010 - first release date
;   Updated: apr. 12, 2010 - fixed several issues
;
#include <Array.au3>
$input = @WindowsDir & '\win.ini'; example ini
If Not FileExists($input) Then
    MsgBox(48, 'Error', ' File Not Found ')
    Exit
EndIf
;
Global $output[1][1]; init ini array
;
_Ini_Array_Rows()
;
Func _Ini_Array_Rows()
    Local $ct, $sn, $rs; common declares
    Local $MaxKeyCt = 0; init max key counter
    Local $sn_tick = 0; init section counter
    $sn = IniReadSectionNames($input); read sections
    While 1; loop 1
        $sn_tick += 1; advance counter
        $rs = IniReadSection($input, $sn[$sn_tick]); read keys
        If @error = 1 Then ContinueLoop; If empty section then continue counting
        If $rs[0][0] > $MaxKeyCt Then $MaxKeyCt = $rs[0][0]; get max keys
        If $sn_tick = $sn[0] Then ExitLoop; exit loop when total reached
    WEnd
    ReDim $output[$sn[0]][$MaxKeyCt + 1]; resize needed array
    $sn_tick = 0; reset section counter
    While 1; loop 2
        $sn_tick += 1; advance counter
        $rs = IniReadSection($input, $sn[$sn_tick]); read keys and values
        If @error = 1 Then; If empty section
            $output[$sn_tick - 1][0] = $sn[$sn_tick]; then insert section name
            ContinueLoop
        EndIf
        For $ct = 1 To $rs[0][0]; loop 3
            $output[$sn_tick - 1][$ct] = $rs[$ct][1]; arrange array
        Next
        $output[$sn_tick - 1][0] = $sn[$sn_tick]; add section name to column 0
        If $sn_tick = $sn[0] Then ExitLoop; exit when done with last section
    WEnd
    Return $output
EndFunc
_ArrayDisplay($output, "Result"); display ini array (rows)
;
Global $output[1][1]; reset array
;
_Ini_Array_Columns()
;
Func _Ini_Array_Columns()
    Local $ct, $sn, $rs
    Local $MaxKeyCt = 0
    Local $sn_tick = 0
    $sn = IniReadSectionNames($input)
    While 1
        $sn_tick += 1
        $rs = IniReadSection($input, $sn[$sn_tick])
        If @error = 1 Then ContinueLoop
        If $rs[0][0] > $MaxKeyCt Then $MaxKeyCt = $rs[0][0]
        If $sn_tick = $sn[0] Then ExitLoop
    WEnd
    ReDim $output[$MaxKeyCt + 1][$sn[0]]
    $sn_tick = 0
    While 1
        $sn_tick += 1
        $rs = IniReadSection($input, $sn[$sn_tick])
        If @error = 1 Then
            $output[0][$sn_tick - 1] = $sn[$sn_tick]
            ContinueLoop
        EndIf
        For $ct = 1 To $rs[0][0]
            $output[$ct][$sn_tick - 1] = $rs[$ct][1]
        Next
        $output[0][$sn_tick - 1] = $sn[$sn_tick]
        If $sn_tick = $sn[0] Then ExitLoop
    WEnd
    Return $output
EndFunc
_ArrayDisplay($output, "Result"); display ini array (columns)
Exit
;
Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

BugFix,

Not sure I understand your comment ... but,

This script is super fast and efficient.

That is, if you use ini's. I still do. :wink:

Just type the path and filename to your ini .. and it will do the rest.

Remember, that the first section must have the greatest amount of keys (or equal to the greatest section).

All my keys are equal -- so I don't have to worry about it.

Perhaps, I'll modify it for odd number keys one day.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Question,

I tried this against a small .ini file and received the following error...

Array variable has incorrect number of subscripts or subscript dimension range exceeded.

from this line...

$output[$sn_tick - 1][$ct] = $rs[$ct][1]; arrange array

Any thoughts?

Link to comment
Share on other sites

Hello jbinc1,

Yes, your first section has less keys than the rest of the sections in your ini

For example:

[First Section] <---- This section must have at least two more keys to match the [second Section] or more.

Key1=1

Key2=0

[second Section] <--- 4 keys

Key1=1

Key2=0

Key3=1

Key4=0

[Third Section] <--- 3 keys

Key1=1

Key2=0

Key3=1

---

you can add dummy keys to fill up your first section if you want.

example: Key4=Blank

I can see, that i'll have to add another code or two to solve that.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

I recycled some of this script code and have a solution for the high key count.

It required a separate function. Will post an update in a day or two, as soon

as I make sure no bugs exist.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

okay folks,

I've updated this script to get max key count and added some error checking/redirection

This is waaaay more code than I need -- so this will be the last update on my part.

By the way -- I only need the values, not key names.

It might need to be modified if you need something else from it.

Cheers!

.

1) * Made a few more changes

2) * Combined both scripts to one

.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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