Sign in to follow this  
Followers 0
Dura

Array problem

5 posts in this topic

I have a problem with a function and array, it worked for a while and then stoped and now gives me: Subscript used with non-Array variable

I've looked at this code over and over now and i must miss something so i'll post it and see if someone can help me.

Func _CountIni()
    Local $ini = @ScriptDir & "\settings.ini"
    Local $counter = 0
    If Not @error Then
        $sections = IniReadSectionNames($ini)
        For $i = 1 To $sections[0]
            $section = IniReadSection($ini, $sections[$i])
            $counter = $counter + $section[0][0]
        Next
    EndIf
    Return $counter
EndFunc

I get:

Subscript used with non-Array variable

$counter = $counter + $section[0][0]

$counter = $counter + $section^ ERROR

IniReadSection() is returning 2D array, im blind here please give me some pointers

Share this post


Link to post
Share on other sites



it should be

$counter = $counter + $section[0]

Why do you say it is returning a 2D array?

Also, your counter doesn't make sense. Each time it loops through add the number of sections there are? If you have 5 sections the counter will go 0 5 10 15 20...


Regards,Josh

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

"Success: Returns a 2 dimensional array where element[n][0] is the key and element[n][1] is the value. "

Total values under that section is element[0][0]

my function do just as you say it counts the total of values in the ini over all sections

This should make it more logical:

Func _CountIni()
    Local $ini = @ScriptDir & "\settings.ini"
    Local $counter = 0
    If Not @error Then
        $sections = IniReadSectionNames($ini)
        For $i = 1 To $sections[0]
            $values = IniReadSection($ini, $sections[$i])
            $counter = $counter + $values[0][0]
        Next
    EndIf
    Return $counter
EndFunc

Edit:

Found my problem , IniReadSection() returns error hence not giving array

Edited by Dura

Share this post


Link to post
Share on other sites

Sorry I read the original code you posted wrong :P But yeah, works fine for me with a dummy INI, I bet your INI is messed up


Regards,Josh

Share this post


Link to post
Share on other sites

yeah found the fault, had one section with no values, really need to get better at error management =)

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