Sign in to follow this  
Followers 0
BitByteBit

IniReadSection and IniRead (Array problem)

6 posts in this topic

#1 ·  Posted (edited)

So, Hi there, thanks for opening my thread firstly.

The setup is this: a user can record a certain pixel colour to a .ini file by press F1.

I have it currently set up so that it is saved like this:

[Colour]

1=0xBlah

2=0xBlah

And so on.

This can then be loaded later, when the program asks the user which colour they would like to load they must key in the key value, in this case one or two.

However ideally i would like is so that user instead of selecting a number, can in fact type the colour. I figured this would rely on using IniReadSection, so instead the .ini would look like this:

[blue]

1=0xBlah

[Yellow]

1=0xBlah

This is the code I have so far:

$name = IniReadSectionNames("C:\fv.ini")
MsgBox(4096, "", $name[1])
;Global $Name[10]
#include <Misc.au3>
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $name[0]
        MsgBox(4096, "", $name[$i])
    Next
EndIf
HotKeySet("^!d","TheQuestion")
if not @error then
$L1 = IniRead("C:\FV.ini", $Name[1], "1", "Not Found")
MsgBox(0,"",$L1)
$L2 = IniRead("C:\FV.ini", $Name[2], "2", "Not Found")
MsgBox(0,"",$L2)
$L3 = IniRead("C:\FV.ini", $Name[3], "3", "Not Found")
$L4 = IniRead("C:\FV.ini", $Name[4], "4", "Not Found")
$L5 = IniRead("C:\FV.ini", $Name[5], "5", "Not Found")
$L6 = IniRead("C:\FV.ini", $Name[6], "6", "Not Found")
$L7 = IniRead("C:\FV.ini", $Name[7], "7", "Not Found")
$L8 = IniRead("C:\FV.ini", $Name[8], "8", "Not Found")
$L9 = IniRead("C:\FV.ini", $Name[9], "9", "Not Found")

Func WriteIni() ;Write Results to .ini
    If $L1 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "1", $Colour)
    Else
        Write2()
    EndIf
EndFunc   ;==>WriteIni
Func Write2()
    If $L2 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "2", $Colour)
    Else
        Write3()
    EndIf

EndFunc   ;==>Write2
Func Write3()
    If $L3 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "3", $Colour)
    Else
        Write4()
    EndIf

EndFunc   ;==>Write3
Func Write4()
    If $L4 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "4", $Colour)
    Else
        Write5()
    EndIf
EndFunc   ;==>Write4
Func Write5()
    If $L5 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "5", $Colour)
    Else
        Write6()
    EndIf
EndFunc   ;==>Write5
Func Write6()
    If $L6 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "6", $Colour)
    Else
        Write7()
    EndIf
EndFunc   ;==>Write6
Func Write7()
    If $L7 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "7", $CropColour)
    Else
        Write8()
    EndIf
EndFunc   ;==>Write7
Func Write8()
    If $L8 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "8", $Colour)
    Else
        Write9()
    EndIf
EndFunc   ;==>Write8
Func Write9()
    If $L9 = "Not Found"Then
        IniWrite("C:\FV.ini", $Name, "9", $Colour)
    EndIf
EndFunc   ;==>Write9
EndIf

In theory this is correct, however when it tries to read a section that doesnt exsist i get a "Array variable has incorrect number of subscripts or subscript dimension range exceeded." I know why, but I do not know how to fix it, any pointers will be greatly appreciated.

Thank you for your time.

Edited by BitByteBit

Share this post


Link to post
Share on other sites



So, Hi there, thanks for opening my thread firstly.

The setup is this: a user can record a certain pixel colour to a .ini file by press F1.

I have it currently set up so that it is saved like this:

[Colour]

1=0xBlah

2=0xBlah

And so on.

This can then be loaded later, when the program asks the user which colour they would like to load they must key in the key value, in this case one or two.

However ideally i would like is so that user instead of selecting a number, can in fact type the colour. I figured this would rely on using IniReadSection, so instead the .ini would look like this:

[blue]

1=0xBlah

[Yellow]

1=0xBlah

I suggest that you could make things simpler if you structured your ini file something like this.

[COLOURS]
Red=0xFF0000
Blue=0x0000FF
Green=0x00FF00
Yellow=0xFFFF00

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

That was my original plan, however I had trouble with that because at the beginning of my script I loaded the values of the keys into memory using $L1 = IniRead("C:\FV.ini", "Colour" , "1", "Not Found")

How would I load the value if it don't the what the name of the key I'm reading is?

Share this post


Link to post
Share on other sites

Ok so here is somthing you could cut up and get what you want. When you press f1 it gets the pixel color your pointing at with your mouse and allows you to name it. When you press f2 it gives you a list of colors that you saved. When you select it there you can do whatever you want with the Hex color. I just made a msgbox with the Hex code have fun.

#include <Misc.au3>
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


HotKeySet('{F1}', 'SaveColor')
HotKeySet('{F2}', 'RecallColor')
HotKeySet('{esc}', 'End')
Global $Location = @DesktopDir & '\myfile.ini'



While 1
    Sleep(10)
WEnd

Func SaveColor()
    $POS = MouseGetPos()
    $PIX = PixelGetColor($POS[0], $POS[1])
    $Name = InputBox('Color Namer', 'Name of this color?')
    IniWrite($Location, $Name, 'Colors', Hex($PIX, 6))
EndFunc   ;==>SaveColor

Func End()
    Exit
EndFunc   ;==>End

Func RecallColor()

    $Sections = IniReadSectionNames($Location)
    If @error Then
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    Else
        $Maybe = GUICreate('Colors', 80, 200, @DesktopWidth - 590, 308)

        $mylist = GUICtrlCreateList('Colors', 0, 0, 78, 200)
        $Sections = IniReadSectionNames($Location)

        For $x = 1 To $Sections[0]
            GUICtrlSetData($mylist, $Sections[$x])
        Next
        GUISetState(@SW_SHOW, $Maybe)

        While 1
            $msg = GUIGetMsg()
            If $msg = $mylist Then
                $HexC = IniReadSection($Location, GUICtrlRead($mylist))
                If @error Then
                    MsgBox(4096, "", "Error occurred, probably no INI file.")
                Else
                    MsgBox(0, "", $HexC[1][1]) ;Gives you the Hex of what color you selected or what ever you got planned for it
                EndIf

            EndIf
            If $msg = $GUI_EVENT_CLOSE Then
                GUIDelete($Maybe)
            EndIf
        WEnd
    EndIf

EndFunc   ;==>RecallColor

[Cheeky]Comment[/Cheeky]

Share this post


Link to post
Share on other sites

Ok so here is somthing you could cut up and get what you want. When you press f1 it gets the pixel color your pointing at with your mouse and allows you to name it. When you press f2 it gives you a list of colors that you saved. When you select it there you can do whatever you want with the Hex color. I just made a msgbox with the Hex code have fun.

Just wanted to say thank you for taking the time to help me with this and show me that example, I now have this function working exactly as I wanted.

Cheers man,

I'll make sure karma pays you back some how.

Peace x.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You could also try using IniReadSection. It returns a 2D array with the key name and value in pairs.

This way, you can use the ini structure proposed by Bowmore.

Edited by omikron48

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