Sign in to follow this  
Followers 0
boelensman1

Problem when trying to accessing a array

5 posts in this topic

Hi everybody,I have the following code:

Local $ini = IniReadSectionNames("settings.ini")
Global $program[$ini[0]+1][10]
If @error Then
MsgBox(4096, "", "Error occurred, while reading settings.ini")
Else
For $i = 1 To $ini[0]
if IniRead ( "settings.ini", $ini[$i], "main", "false" )=='true' Then
$main_program=$i
EndIf
Local $var=IniReadSection("settings.ini",$ini[$i])
For $a = 1 To $var[0][0]
$program[$i][$var[$a][0]]=$var[$a][1]
Next
Next
EndIf

MsgBox(4096, "", "Key: " & "path" & @CRLF & "Value: " & $program[$main_program]["path"])
MsgBox(4096, "", "Key: " & "asdf" & @CRLF & "Value: " & $program[$main_program]["asdf"])

Wich reads the following ini:

[NOTEPAD]
path=C:\Windows\system32\notepad.exe
ontop=true
exit=false
main=true
restart=true
[NOTEPAD++]
path=C:\Program Files (x86)\Notepad++
ontop=true
exit=true
restart=true

The problem is that both messageboxes return "true" while the first should return "C:\Windows\system32\notepad.exe" and the second key (asdf) doesn't even exist in the ini.

I have been trying to find out why it doesn't work for the past half an hour. It's probably something real simple. Thanks in advance :)

Share this post


Link to post
Share on other sites



I think you need to read the Array wiki, because this $program[$main_program]["asdf"] is wrong since the second index should be an integer and not a string.

#include <Array.au3>

Local $aINISections = IniReadSectionNames(@ScriptDir & 'Example.ini')
Local $aPrograms[$aINISections[0] + 1][10], $iMainProgram = 0, $vVar = 0 ; Don't declare variables in loops.
If @error Then
    MsgBox(4096, '', 'Error occurred, while reading Example.ini')
Else
    For $i = 1 To $aINISections[0]
        $aPrograms[0][0] += 1
        $aPrograms[$i][0] = $aINISections[$i]

        If IniRead(@ScriptDir & 'Example.ini', $aINISections[$i], 'main', 'False') = 'True' Then
            $iMainProgram = $i
        EndIf

        $vVar = IniReadSection(@ScriptDir & 'Example.ini', $aINISections[$i])
        For $j = 1 To $vVar[0][0]
            $aPrograms[$i][$j] = $vVar[$j][1]
        Next

    Next
EndIf
_ArrayDisplay($aPrograms)
;~ MsgBox(4096, '', 'Key: ' & 'Path' & @CRLF & 'Value: ' & $aPrograms[$iMainProgram]['path'])
;~ MsgBox(4096, '', 'Key: ' & 'ASDF' & @CRLF & 'Value: ' & $aPrograms[$iMainProgram]['asdf'])

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

It looks to me in the MsgBox calls as if you are trying to index the arrays using strings. If you want to use an associative array, use Scripting.Dictionary.

Share this post


Link to post
Share on other sites

If they do decide to use an associative array, may I recommend at least looking at the Array Wiki.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

For a Dictionary with case insensitive keys my wrapper functions simplify construction and destruction. If construction fails @error is set to 1.

;use Scripting.Dictionary object for simple associative arrays
Func _AssocArray()
Local $aArray = ObjCreate("Scripting.Dictionary")
If @error Then
Return SetError(1, 0, 0)
EndIf
$aArray.CompareMode = 1
Return $aArray
EndFunc ;==>_AssocArray

Func _AssocArrayDestroy(ByRef $aArray)
If Not IsObj($aArray) Then
Return False
EndIf
$aArray.RemoveAll()
$aArray = 0
Return True
EndFunc ;==>_AssocArrayDestroy

For methods such as Exists("key"), see the docs here:

http://msdn.microsoft.com/en-us/library/x4k5wbx4(v=vs.84).aspx

It's pretty vanilla

Edited by MilesAhead

Share this post


Link to post
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
Sign in to follow this  
Followers 0