ripdad Posted February 17, 2010 Posted February 17, 2010 (edited) Two scripts to get an ini into an arrayUpdated: April 12, 2010- Fixed several issues -expandcollapse popup; 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 April 12, 2010 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
BugFix Posted February 17, 2010 Posted February 17, 2010 But, ...what sense make it? Best Regards BugFix
ripdad Posted February 18, 2010 Author Posted February 18, 2010 (edited) 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 February 18, 2010 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
jbinc1 Posted February 18, 2010 Posted February 18, 2010 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 arrayAny thoughts?
ripdad Posted February 18, 2010 Author Posted February 18, 2010 (edited) Hello jbinc1,Yes, your first section has less keys than the rest of the sections in your iniFor example:[First Section] <---- This section must have at least two more keys to match the [second Section] or more.Key1=1Key2=0[second Section] <--- 4 keysKey1=1Key2=0Key3=1Key4=0[Third Section] <--- 3 keysKey1=1Key2=0Key3=1---you can add dummy keys to fill up your first section if you want.example: Key4=BlankI can see, that i'll have to add another code or two to solve that. Edited February 18, 2010 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
ripdad Posted February 18, 2010 Author Posted February 18, 2010 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
ripdad Posted February 19, 2010 Author Posted February 19, 2010 (edited) okay folks,I've updated this script to get max key count and added some error checking/redirectionThis 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 April 8, 2010 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now