Jump to content

Question...


Recommended Posts

I generally am pretty good at figuring things out... but I'm stumped and hoping someone might be able to assist...

 

i have an ini file with tons of 'keys' in one 'section'. Is it possible to store them all in the script. Now the answer is yes... but here is the tricky part...

I need to scan through them to see if one key matches a random key I generate to see if it exists... if it does, then I need to call the value else ignore... well for now at least. 

 

So the question, how can one query a list stored in say a function? Would maybe creating a combined string... using say ":" or some symbol as a method to split, and then create an array to split them, so in essence they are an array. This would eliminate the value but I probably can figure that part out after I get the key in place.

 

Thoughts ???

Link to comment
Share on other sites

  • Moderators

IniReadSection creates a 2D array of the sections keys and values, it stores the sections keys in [n][0] and the subsequent keys values in [n][1]

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

You could store your values in an array and loop through the array looking for a match to your random value.

Global $ranValue = "5"
Global $aValues[10] = ["1","2","3","4","5","6","7","8","9","10"]

For $i = 0 to Ubound($aValues) - 1
    If $aValues[$i] = $ranValue Then
        Consolewrite($aValues[$i] & Chr(32) & "Match Found" & @CRLF)
        ExitLoop
    EndIf
Next
Link to comment
Share on other sites

ArronCombs,

Re reading your post, I realized I hadn't fully understood it yesterday, and only provided a partial solution.

As I stated yesterday, storing the ini file in an array and then looping through it looking for a match, in my opinion would be the way to go if you want to include the key=value pairs in the ini file in your script.
My example was incorrect for your problem as it has a 1 dimensional array. A 2 dimemsional array would be needed instead.

New example

Global $aKeysValues[10][10]
    $aKeysValues[0][0]="MyKey1"
    $aKeysValues[0][1]="Value1"
    $aKeysValues[1][0]="MyKey2"
    $aKeysValues[1][1]="Value2"
    $aKeysValues[2][0]="MyKey3"
    $aKeysValues[2][1]="Value3"
    $aKeysValues[3][0]="MyKey4"
    $aKeysValues[3][1]="Value4"
    $aKeysValues[4][0]="MyKey5"
    $aKeysValues[4][1]="Value5"
    $aKeysValues[5][0]="MyKey6"
    $aKeysValues[5][1]="Value6"
    $aKeysValues[6][0]="MyKey7"
    $aKeysValues[6][1]="Value7"
    $aKeysValues[7][0]="MyKey8"
    $aKeysValues[7][1]="Value8"
    $aKeysValues[8][0]="MyKey9"
    $aKeysValues[8][1]="Value9"
    $aKeysValues[9][0]="MyKey10"
    $aKeysValues[9][1]="Value10"

Global $sRandomKey = "MyKey10"

For $i = 0 To UBound($aKeysValues) - 1
    ;ConsoleWrite($aKeysValues[$i][0] & "=" & $aKeysValues[$i][1] & @CRLF)

    If $aKeysValues[$i][0] = $sRandomKey Then
        ConsoleWrite("Key Found." & @CRLF)
        ConsoleWrite("Key value is:" & Chr(32) & $aKeysValues[$i][1] & @CRLF)
        ExitLoop
    EndIf
Next

Additionally, I created for you for demonstration purposes two functions.

 - The first function creates a one section in file with 10 key/ value pairs for use in the second function.

 - The second function converts the one section ini file created with the first function into an Autoit syntax array. The code is saved to a txt file. You could then copy the code into a script.

; Creates a single section ini file.
; File is saved in the same folder as the script.
Func _CreateIniFile()
Local $iKeyCount = 1, $iValueCount = 1
Local $sSectionName = "MyFirstSection"

    For $i = 1 To 10
        IniWrite("MyFile.ini", $sSectionName, "MyKey" & $iKeyCount, "Value" & $iValueCount)
        $iKeyCount += 1
        $iValueCount += 1
    Next
EndFunc ; ==> _CreateIniFile()

_CreateIniFile()


; Converts a single section ini file,
; into an Autoit Syntax 2-Dimensional Array. saved to a text file.
; Ini file values are writen as strings only. Does not set as integers.
; File is saved in the same folder as the script.
Func _ConvertIniFileToAutoitArray()
    Local $aIniFile, $sAutoitArraySyntax

    Local $sIniFilePathName = @ScriptDir & "\" & "MyFile.ini"
    Local $sIniFileSectionName = "MyFirstSection"

    $aIniFile = IniReadSection($sIniFilePathName, $sIniFileSectionName)
    $sAutoitArraySyntax = "$aKeysValues[" & $aIniFile[0][0] & "]" & "[" & $aIniFile[0][0] & "]" & @CRLF

    For $i = 1 To $aIniFile[0][0]
        $sAutoitArraySyntax &= "$aKeysValues[" & $i-1 & "]" & "[0]=" & """" & $aIniFile[$i][0] & """" & @CRLF
        $sAutoitArraySyntax &= "$aKeysValues[" & $i-1 & "]" & "[1]=" & """" & $aIniFile[$i][1] & """" & @CRLF
    Next

    ;ConsoleWrite($AutoitArraySyntax)
    Local $hFile = FileOpen(@ScriptDir & "\" & "Ini_Section_To_Autoit_2D_Array.txt", 2)
    FileWrite($hFile, $sAutoitArraySyntax)
    FileClose($hFile)
EndFunc ;==> _ConvertIniFileToAutoitArray()


_ConvertIniFileToAutoitArray()
Edited by AndrewG
Link to comment
Share on other sites

If you want to store the keys and values within your script, in a function perhaps, then just store them in a variable, using the pipe symbol as delimiter between each pair, then use another delimiter (i.e. backslash) between key and value.

To access these keys and values, just use the StringSplit command twice ... once to get an array for all keys & values, then again to separate a key and its value in another small two element array.

NOTE - Once inside your script though, you cannot rename either key or value permanently, at a later date, once it has been compiled.

P.S. Nobody sought to address that part of your initial question, so I thought I would, even though you seem to have an acceptable solution already. Never hurts to educate.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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...