Jump to content

InireadSection


Recommended Posts

Hi,

I use a code that read one by one my value in an ini but I was wondering if I could do the same thing while using InireadSection. I got 50 variables in my ini file so ONLY the code to read all those variables is +1000 lines longs and it's almost only copy/paste.I want to shorter my code alot.

;[SQUARE 1]
            $Read1 = IniRead(@SCRIPTDIR & "\Config.ini", "Grid", "1","")
            
            If $Read1 = @ScriptDir & "\Images\1.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "1")
            ElseIf $Read1 = @ScriptDir & "\Images\2.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "2")
            ElseIf $Read1 = @ScriptDir & "\Images\3.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "3")
            ElseIf $Read1 = @ScriptDir & "\Images\4.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "4")
            ElseIf $Read1 = @ScriptDir & "\Images\5.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "5")
            ElseIf $Read1 = @ScriptDir & "\Images\6.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "6")
            ElseIf $Read1 = @ScriptDir & "\Images\7.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "7")
EndIf

;[SQUARE 2]
;.....Same things as Square 1 except it's S2

[INI FILE]
[Grid]
1=C:\Documents and Settings\Images\1.bmp
2=C:\Documents and Settings\Images\4.bmp
3=C:\Documents and Settings\Images\3.bmp
4=C:\Documents and Settings\Images\2.bmp
5=C:\Documents and Settings\Images\3.bmp
6=C:\Documents and Settings\Images\6.bmp
7=C:\Documents and Settings\Images\5.bmp
8=C:\Documents and Settings\Images\7.bmp
9=C:\Documents and Settings\Images\4.bmp
10=C:\Documents and Settings\Images\1.bmp
...

I could do Ini read and copy this code for the 50 variables but it's very very long. Can I shorter my code using Inireadsection?

Edited by J0ker
Link to comment
Share on other sites

The function IniReadSection() already returns the data in a 2D array. You only need to know how to use the array:

$Read1 = IniReadSection(@SCRIPTDIR & "\Config.ini", "Grid")
If @error = 0 And $avRead1[0][0] > 0 Then
    For $n = 1 To $avRead1[0][0]
        MsgBox(64, "Contents of [Grid]", $av[$n][0] & " = " & $av[$n][1], 5)
    Next
Else
    MsgBox(16, "Error", "Error reading INI file.")
EndIf

:shocked:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I knew that it use a 2D array but I dont know how to set it so it will convert this:

If $Read1 = @ScriptDir & "\Images\1.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "1")
            ElseIf $Read1 = @ScriptDir & "\Images\2.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "2")
            ElseIf $Read1 = @ScriptDir & "\Images\3.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "3")
            ElseIf $Read1 = @ScriptDir & "\Images\4.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "4")
            ElseIf $Read1 = @ScriptDir & "\Images\5.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "5")
            ElseIf $Read1 = @ScriptDir & "\Images\6.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "6")
            ElseIf $Read1 = @ScriptDir & "\Images\7.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "7")

into an array using my 50 variables.

Link to comment
Share on other sites

In your example, the key names (1, 2, 3, etc.) match up to the key names you want to write (S1, S2, S3, etc.) and the data. So just use the key name to format your value to write:

$avRead1 = IniReadSection(@ScriptDir & "\Config.ini", "Grid")
If @error = 0 And $avRead1[0][0] > 0 Then
    For $n = 1 To $avRead1[0][0]
        IniWrite(@ScriptDir & "\Config.ini", "Variables", "S" & $avRead1[$n][0], $avRead1[$n][0])
    Next
Else
    MsgBox(16, "Error", "Error reading INI file.")
EndIf

:shocked:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Yep but I used this as an example... my fault :shocked:

If $Read1 = @ScriptDir & "\Images\1.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Home")
            ElseIf $Read1 = @ScriptDir & "\Images\2.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Building")
            ElseIf $Read1 = @ScriptDir & "\Images\3.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Flower")
            ElseIf $Read1 = @ScriptDir & "\Images\4.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Car")
            ElseIf $Read1 = @ScriptDir & "\Images\5.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Dog")
            ElseIf $Read1 = @ScriptDir & "\Images\6.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Pillow")
            ElseIf $Read1 = @ScriptDir & "\Images\7.bmp" Then
                IniWrite(@SCRIPTDIR & "\Config.ini", "Variables", "S1", "Food")
EndIf

That's the real one, it's why Im all messed up ^^

Edited by J0ker
Link to comment
Share on other sites

You need another array (or some such device) to list the associations between these bmp file names and nouns. Then for each key read from the ini file you do a search for the bmp file, and read out its associated noun, writing it to the second ini file:

; Array of associations between BMP files and nouns
;   (These associations could be read from another INI file section)
Global $avBmpAssoc[4][2] = [[0, "1.bmp", "2.bmp", "3.bmp"], [0, "Home", "Building", "Flower"]]
$avBmpAssoc[0][0] = UBound($avBmpAssoc) - 1 ; count

; Array of data read from Grid section
Global $avRead1[1][2]

; Read the section
$avRead1 = IniReadSection(@ScriptDir & "\Config.ini", "Grid")
If @error = 0 And $avRead1[0][0] > 0 Then
    For $n = 1 To $avRead1[0][0]
        ; Find it's association
        For $a = 1 To $avBmpAssoc[0][0]
            If $avRead1[$n][1] = $avBmpAssoc[$a][0] Then
                ; Write the association out to an INI file
                IniWrite(@ScriptDir & "\Config.ini", "Variables", "S" & $avRead1[$n][0], $avRead1[$a][1])
                ExitLoop
            EndIf
        Next
    Next
Else
    MsgBox(16, "Error", "Error reading INI file.")
EndIf

:shocked:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks, I will try this out !

EDIT:

I'm getting "Array variable has incorrect number of subscripts or subscript dimension range exceeded"

If I change it to

Global $avBmpAssoc[4][4] = [[0, "1.bmp", "2.bmp", "3.bmp"], [0, "Home", "Building", "Flower"]]

It's writting in my Ini file but each S's are followed by the filepath of my first image. There's no association with the nouns.

[Ini File]

S1=C:\Documents and Settings\Images\1.bmp
S2=C:\Documents and Settings\Images\1.bmp
S3=C:\Documents and Settings\Images\1.bmp
S4=C:\Documents and Settings\Images\1.bmp
S5=C:\Documents and Settings\Images\1.bmp
S6=C:\Documents and Settings\Images\1.bmp
;...

Why do Im getting this array error?

Edited by J0ker
Link to comment
Share on other sites

Why do Im getting this array error?

Because your Array variable has an incorrect number of subscripts or you have exceeded the subscript dimension range.

The first part of the error means that you have referenced an array with the wrong number of dimensions, calling a 2d array with only one subscript, for example.

The second means that you referenced an element of an array that does not exist, like the 4th element of a vector that only has three elements.

A common noob mistake is to underdimension your array by one element. Check out Ubound() and the common practice of returning / storing the number of elements in $array[0] to ensure that you don't do stuff like this.

You can also try initializing your arrays in a loop or line by line to visualize where your problem is.

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

But If I'm using this, where do my error came from?

Global $avBmpAssoc[4][2] = [[0, "1.bmp", "2.bmp", "3.bmp"], [0, "Home", "Building", "Flower"]]
$avBmpAssoc[0][0] = UBound($avBmpAssoc) - 1 ; count

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Global $avBmpAssoc[4][2] = [[0, "1.bmp", "2.bmp", "3.bmp"], [0, "Home", "Building", "Flower"]]

Global $avBmpAssoc[4][2] = [[0, "1.bmp", ^ ERROR

Edited by J0ker
Link to comment
Share on other sites

But If I'm using this, where do my error came from?

Global $avBmpAssoc[4][2] = [[0, "1.bmp", "2.bmp", "3.bmp"], [0, "Home", "Building", "Flower"]]
$avBmpAssoc[0][0] = UBound($avBmpAssoc) - 1 ; countoÝ÷ Ûú®¢×iÔ²Ö±Z­².×r¥zg§¶íæ«­¬xZ+{¦¦WºÚ"µÍÛØ[  ÌÍØ^VÍVÌBÜ    ÌÍÚHHÈÂÜ  ÌÍÚHÈB  ÌÍØ^VÉÌÍÚWVÉÌÍÚHHÏÏÏÂ^^

Hint... What if you dimension your array as a 4x4???

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

; Array of associations between BMP files and nouns
;   (These associations could be read from another INI file section)
Global $avBmpAssoc[4][2] = [[0, "1.bmp", "2.bmp", "3.bmp"], [0, "Home", "Building", "Flower"]]
$avBmpAssoc[0][0] = UBound($avBmpAssoc) - 1 ; count

; Array of data read from Grid section
Global $avRead1[1][2]

; Read the section
$avRead1 = IniReadSection(@ScriptDir & "\Config.ini", "Grid")
If @error = 0 And $avRead1[0][0] > 0 Then
    For $n = 1 To $avRead1[0][0]
        ; Find it's association
        For $a = 1 To $avBmpAssoc[0][0]
            If $avRead1[$n][1] = $avBmpAssoc[$a][0] Then
                ; Write the association out to an INI file
                IniWrite(@ScriptDir & "\Config.ini", "Variables", "S" & $avRead1[$n][0], $avRead1[$a][1])
                ExitLoop
            EndIf
        Next
    Next
Else
    MsgBox(16, "Error", "Error reading INI file.")
EndIf

I'm associating a filepath with an nouns.

If I dimension my array 4x4, Im not getting an error but it's not writting the good things in my ini file.

Little background about my code:

It get the the value in a ini file. Depending on the value it return, it write something in another section of the ini file. It's why I need to associate my values with a nouns but it's not working.

Edited by J0ker
Link to comment
Share on other sites

I don't think you're putting your array elements where you think you are.

Iterate your array as I showed you earlier -- use consolewrite() or msgbox() to show you exactly what is being placed where.

Just because you stopped calling invalid elements in the array _ by making the array bigger ==> ("the error went away") doesn't mean that your logic is correct, or that you know where you put each element in the array.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

I tried but Im pretty new to Array. All my scripts were made without array. I cant seem to see where the problem came from and I dont know where to use a msgbox() to detect my error. Can you help me a little bit more?

Should I write:

Global $avBmpAssoc[4][2] = [[0, @ScriptDir "\Images\1.bmp", @ScriptDir "\Images\2.bmp", @ScriptDir "\Images\3.bmp" ], [0, "Home", "Building", "Flower"]]
Link to comment
Share on other sites

As you are admittedly new at this; I would suggest the following:

Use Enum to define the multi-dimensionality of the array. This is how au3 simulates

what are known as "associative arrays", so you don't have to remember which dimension everything is in.

VERY handy if you have a lot of properties.

Enum $key=0,$value=1,$num_propertiesoÝ÷ Ø8¦z{"¢{ayªëk,".µ«,  â§Øö¥¹ëºÚ"µÍ[H   ÌÍØVÍVÉÌÍÛ[WÜÜY×oÝ÷ Øú)ºV­zØ^jºÚÊë"¥¢l¢ØZ·*.z0«yëÞ¯+ax"±«­¢+Ø)½ÈÀÌØí¤ôÀѼÌ(ÀÌØílÀÌØí¥ulÀÌØí­åtôíÝ¡ÑÙÈ­äå½ÔÌäíÉÉ¥¹(ÀÌØílÀÌØí¥ulÀÌØíÙ±ÕtôìÝ¡ÑÙÈÙ±Õ¥ÌÍͽ¥ÑÝ¥Ñ Ñ¡­ä(µÍ½à À°ÀÌØílÀÌØí¥ulÀÌØí­åt°ÀÌØílÀÌØí¥ulÀÌØíÙ±Õt졽ÜѼ¥ÍÁ±äÝ¡ÐÌäíÌ¥¸Ñ¡ÉÉä¸)¹áÐ(
Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Enum $key=0,$value=1,$num_properties
dim $a[4][$num_properties]

for $i = 0 to 3
  $a[$i][$key] = [@ScriptDir "\Images\1.bmp", @ScriptDir "\Images\2.bmp", @ScriptDir "\Images\3.bmp"]
  $a[$i][$value] =  ["Home", "Building", "Flower"]
  msgbox(0,$a[$i][$key],$a[$i][$value]) ; how to display what's in the array.
next

Thanks for the structure, but how do I write it after each array because atm, Im getting an error...

Edited by J0ker
Link to comment
Share on other sites

If I compare my values using another section of my Ini file, It work!

; Array of data read from Grid section
            Global $avRead1[5][5]
            Global $avRead2[5][5]

            ; Read the section
            $avRead2 = IniReadSection(@ScriptDir & "\Config.ini", "Try")
            $avRead1 = IniReadSection(@ScriptDir & "\Config.ini", "Grid")
            If @error = 0 And $avRead1[0][0] > 0 Then
                For $n = 1 To $avRead1[0][0]
                    ; Find it's association
                    For $a = 1 to $avRead2[0][0]
                        If $avRead1[$n][1] = $avRead2[$a][0] Then
                            ; Write the association out to an INI file
                            IniWrite(@ScriptDir & "\Config.ini", "Variables", "S" & $avRead1[$n][0], $avRead2[$a][1])
                            ExitLoop
                    EndIf
                    Next
                Next
            Else
                MsgBox(16, "Error", "Error reading INI file.")
            EndIf

How can I do the same thing but using an array and 1 Iniread instead of having 2 IniRead?

I want to replace $avRead2 by an array.

I need to convert that into an array :

INI FILE
[Try]
C:\Documents and Settings\Images\1.bmp = Home
C:\Documents and Settings\Images\2.bmp = Dog
C:\Documents and Settings\Images\3.bmp = Flowers
C:\Documents and Settings\Images\4.bmp = Pillow
C:\Documents and Settings\Images\5.bmp = Food

How can I do it?

Link to comment
Share on other sites

You are beginning to get monotonous. You don't understand what your syntax is actually doing, and are beginning to seem unwilling to learn.

Display your created array, and figure out where you are putting stuff, so that you can make your needed adjustments.

An exercise for the student:

Create , populate, and display using MsgBox or ConsoleWrite() a two dimensional array that contains 4 numbers in one dimension, and their squares in the other. (i.e 1 squared = 1, 2 squared = 4....)

When you have that working, you will be able to understand how to figure out where you are putting stuff, and how to get it back.

You have more than enough info in this thread to figure this out.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Sorry 'bout not testing my reply example. I still don't have time to fully test it but this fixes some obvious mistakes I made, like how the pre-fill data was put in the array. Note that it might be easier for you to create an INI file, or a section in a file that contains the associations and read them in to $avBmpAssoc with IniReadSection() also:

; 2D array of associations between BMP files and nouns
;   (These associations could be read from another INI file section)
Global $avBmpAssoc[4][2] = [[0, 0], ["1.bmp", "Home"], ["2.bmp", "Building"], ["3.bmp", "Flower"]]
$avBmpAssoc[0][0] = UBound($avBmpAssoc) - 1 ; count

; 2D array for data read from Grid section
Global $avRead1[1][2]

; Read the section
$avRead1 = IniReadSection(@ScriptDir & "\Config.ini", "Grid")
If @error = 0 And $avRead1[0][0] > 0 Then
    ; For each BMP file
    For $n = 1 To $avRead1[0][0]
        ; Find it's association
        For $a = 1 To $avBmpAssoc[0][0]
            If $avRead1[$n][1] = $avBmpAssoc[$a][0] Then
                ; Write the matching association out to an INI file
                IniWrite(@ScriptDir & "\Config.ini", "Variables", "S" & $avRead1[$n][0], $avBmpAssoc[$a][1])
                ExitLoop
            EndIf
        Next
    Next
Else
    MsgBox(16, "Error", "Error reading INI file.")
EndIf

:">

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...