Jump to content

INI file to multi-dimensional associative array


LWC
 Share

Go to solution Solved by LWC,

Recommended Posts

The forum offers various solutions for converting INI files and for multi-dimensional and associative arrays. But there's no topic that discusses all three at the same time.

INI file:

[person1]
name=Mikey
age=4

[person2]
name=Becky
age=46

Needed array:

[person1]
      => [name] = Mikey
         [age] = 4
[person2]
      => [name] = Becky
         [age] = 46

So is it possible? If so, can you make something like if isset($array["person2"]["name"])?

Thanks!

Edited by LWC
Link to comment
Share on other sites

  • Moderators

The example in the helpfile _ArrayUnique may give you some direction.

What in the world does that have to do with the question?

Edit:

http://www.autoitscript.com/forum/index.php?showtopic=70955

http://www.autoitscript.com/forum/index.php?showtopic=93489

Edited by SmOke_N

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

What in the world does that have to do with the question?

Edit:

http://www.autoitscript.com/forum/index.php?showtopic=70955

http://www.autoitscript.com/forum/index.php?showtopic=93489

I looked at the example in the helpfile and it showed a multi dimensional array. I thought it may help.
Link to comment
Share on other sites

I've done something like this where I use a 2 dimensional array to hold things, $iniData[1][2].

First I read in the ini file's section names:

$sectionNames = IniReadSectionNames($DataFileName)

Then I reDim the iniData array above so that the size of the first index matches the section name count:

ReDim $iniData[$sectionNames[0]][2]

I then read each section's data into an array via IniReadSection, and then place each array obtained into the master

$iniData array's second slot (the first holding the section name:
        For $i = 1 To $sectionNames[0]
            ;; read in key value pairs in the current section. section data holds all key/val pairs in 2 d array
            $sectionData = IniReadSection($DataFileName, $sectionNames[$i])

            ;; section name goes into ini data array [][0]
            $iniData[$i-1][0] = $sectionNames[$i]

            ;; section data array goes into ini data array [][1]
            $iniData[$i-1][1] = $sectionData
        Next

To get stuff out of the array, I do this:

Func getSectionKeyValue($iniDataParam, $sectionNameParam, $keyString)

    ;; get index section in ini data array
    $sectionIndex = _ArraySearch($iniDataParam, $sectionNameParam)

    ;; get section key/value array held there. Note that [0] holds the section name
    $sectionDataLocal = $iniDataParam[$sectionIndex][1]

    ;; get index of key/value row corresponding to key string
    $retValueIndex = _ArraySearch($sectionDataLocal, $keyString)

    ;; get the value associated with the key. key is at [0], value at [1]
    $retValue = $sectionDataLocal[$retValueIndex][1]
    Return $retValue
EndFunc

An example,....

#include <GUIConstantsEx.au3>
#include <file.au3>
#include <array.au3>

;; CONSTANTS
Const $Version = "1.1"
Const $DataFileName = @ScriptDir & "\auData.ini"  ; name of the ini data file
Const $ProgramTitle = "Foo Utilities, Version " & $Version

;; CONSTANTS TO MATCH INI FILE SECTIONS AND KEYS
Const $ProviderSection = "provider"
Const $AssistantSection = "assistant"
Const $ApplicationSection = "application"
Const $OpenLettersSection = "open letters"
Const $NewFlagSection = "new flag"
Const $PhoneNoteSection = "phone note"

Const $ProviderLNameKey = "Last Name"
Const $ProviderFNameKey = "First Name"
Const $ProviderTitleKey = "Title"
Const $AssistantLNameKey = "Last Name"
Const $AssistantFNameKey = "First Name"
Const $AssistantTitleKey = "Title"
Const $ApplicationKey = "Application"
Const $OpenLetterKey = "Open Letter Script"
Const $NewFlagScript1Key = "New Flag Script1"
Const $NewFlagScript2Key = "New Flag Script2"
Const $PhoneNoteScript1Key = "Phone Note Script1"
Const $PhoneNoteScript2Key = "Phone Note Script2"
Const $PhoneNoteScript3Key = "Phone Note Script3"
Const $PhoneNoteScript4Key = "Phone Note Script4"

;; GLOBAL APPLICATION VARIABLES
Dim $iniData[1][2] ;; holds data from ini file
Dim $mainwindow ;; main window for application
;; below filled by the fillGlobalVars method
Dim $app        ; application name
Dim $providerLName  ; doc's last name
Dim $providerFName  ; doc's first name
Dim $providerTitle  ; doc's title
Dim $assistantLName ; rn's last name
Dim $assistantFName ; rn's first name
Dim $assistantTitle ; rn's title
Dim $openLetterScript ; file name for open letter script
Dim $newFlagScript1 ; file name for new flag script, first part
Dim $newFlagScript2 ; file name for new flag script, second part
Dim $phoneNoteScript1 ; file name for new phone note script, first part
Dim $phoneNoteScript2 ; file name for new phone note script, second part
Dim $phoneNoteScript3 ; file name for new phone note script, third part
Dim $phoneNoteScript4 ; file name for new phone note script, fourth part

initializeNonGui()

displayDataInIniData() ;; test that arrays working
;;;initializeGui()



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; non-GUI section.  Fills variables and set us.

;; set up all non-gui
Func initializeNonGui()
    ;; get the ini file section names
    $sectionNames = IniReadSectionNames($DataFileName)

    If @error Then
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    Else
        ;; set up array to hold section names and section data, the latter in an array
        ReDim $iniData[$sectionNames[0]][2]

        For $i = 1 To $sectionNames[0]
            ;; read in key value pairs in the current section. section data holds all key/val pairs in 2 d array
            $sectionData = IniReadSection($DataFileName, $sectionNames[$i])

            ;; section name goes into ini data array [][0]
            $iniData[$i-1][0] = $sectionNames[$i]

            ;; section data array goes into ini data array [][1]
            $iniData[$i-1][1] = $sectionData
        Next
    EndIf

    fillGlobalVars()

EndFunc

;; uses Strings from ini file sections and key
Func fillGlobalVars()
    ;; main App
    $app = getSectionKeyValue($iniData, $ApplicationSection, $ApplicationKey)

    ;; Provider section
    $providerLName = getSectionKeyValue($iniData, $ProviderSection, $ProviderLNameKey)
    $providerFName = getSectionKeyValue($iniData, $ProviderSection, $ProviderFNameKey)
    $providerTitle = getSectionKeyValue($iniData, $ProviderSection, $ProviderTitleKey)

    ;; Assistant section
    $assistantLName = getSectionKeyValue($iniData, $AssistantSection, $AssistantLNameKey)
    $assistantFName = getSectionKeyValue($iniData, $AssistantSection, $AssistantFNameKey)
    $assistantTitle = getSectionKeyValue($iniData, $AssistantSection, $AssistantTitleKey)

    ;; Get open Letters Script file name; then prepend the script directory path
    $openLetterScript = getSectionKeyValue($iniData, $OpenLettersSection, $OpenLetterKey)
    $openLetterScript = @ScriptDir & "\" & $openLetterScript

    ;; Get new Flags Script file name; then prepend the script directory path
    $newFlagScript1 = getSectionKeyValue($iniData, $NewFlagSection, $NewFlagScript1Key)
    $newFlagScript2 = getSectionKeyValue($iniData, $NewFlagSection, $NewFlagScript2Key)
    $newFlagScript1 = @ScriptDir & "\" & $newFlagScript1
    $newFlagScript2 = @ScriptDir & "\" & $newFlagScript2

    ;; Get new Phone Note Script file name; then prepend the script directory path
    $phoneNoteScript1 = getSectionKeyValue($iniData, $PhoneNoteSection, $PhoneNoteScript1Key)
    $phoneNoteScript2 = getSectionKeyValue($iniData, $PhoneNoteSection, $PhoneNoteScript2Key)
    $phoneNoteScript3 = getSectionKeyValue($iniData, $PhoneNoteSection, $PhoneNoteScript3Key)
    $phoneNoteScript4 = getSectionKeyValue($iniData, $PhoneNoteSection, $PhoneNoteScript4Key)
    $phoneNoteScript1 = @ScriptDir & "\" & $phoneNoteScript1
    $phoneNoteScript2 = @ScriptDir & "\" & $phoneNoteScript2
    $phoneNoteScript3 = @ScriptDir & "\" & $phoneNoteScript3
    $phoneNoteScript4 = @ScriptDir & "\" & $phoneNoteScript4

EndFunc

;; get value from key/value pair held by section
Func getSectionKeyValue($iniDataParam, $sectionNameParam, $keyString)

    ;; get index section in ini data array
    $sectionIndex = _ArraySearch($iniDataParam, $sectionNameParam)

    ;; get section key/value array held there. Note that [0] holds the section name
    $sectionDataLocal = $iniDataParam[$sectionIndex][1]

    ;; get index of key/value row corresponding to key string
    $retValueIndex = _ArraySearch($sectionDataLocal, $keyString)

    ;; get the value associated with the key. key is at [0], value at [1]
    $retValue = $sectionDataLocal[$retValueIndex][1]
    Return $retValue
EndFunc


Func displayDataInIniData()
    $sectionNames = IniReadSectionNames($DataFileName)

    For $i = 1 To $sectionNames[0]
        $foo = _ArraySearch($iniData, $sectionNames[$i])
        $fooArray = $iniData[$foo][1]
        _ArrayDisplay($fooArray, $iniData[$foo][0])
    Next
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GUI section.  initializes and runs GUI

Func initializeGui()
    ;;......... code deleted
EndFunc

;;.... gui code deleted...

My ini file looks like this:

; last modified 8/22/2010 by Fubarable
; auData.ini is the "active" ini file

[provider]
Last Name = Johson
First Name = Frank
Title = MD

[assistant]
Last Name = Smith
First Name = Sally
Title = RN

[application]
Application = Centricity EMR

[open letters]
Open Letter Script = openLetterFJohnson.txt

[new flag]
New Flag Script1 = newFlagAction1.txt
New Flag Script2 = newFlagAction2.txt

[phone note]
Phone Note Script1 = newPhoneNoteAction1.txt
Phone Note Script2 = newPhoneNoteAction2.txt
Phone Note Script3 = newPhoneNoteAction3.txt
Phone Note Script4 = newPhoneNoteAction4.txt

Note that I'm no AutoIt expert, but rather a novice, so take anything I say with a grain of salt...

Edited by Fubarable
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

×
×
  • Create New...