Replace INI Value from some Section to another Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Replace INI Value from some Section to another

Post by HiSoKa » 26 Nov 2022, 11:02

Hello,
I have this INI file.
[MySection 1]
portion=p1
num=1

[MySection 2]
portion=p2
num=1

[MySection 3]
portion=p1
num=2

[MySection 4]
portion=p1
num=3

[MySection 5]
portion=p2
num=2

[MySection 6]
portion=p1
num=4

[MySection 7]
portion=p1
num=5


And I have this code that I wrote with the help of Mikey :salute:
To get all Sections which containing portion=p1 (Sections to which the following operation should be applied)..

Code: Select all

ini          := A_Scriptdir . "\config.ini"
portion      := "p1"
oldValue     := "num=4" ; IN [MySection 6]
NewValue     := "num=3" ; IN [MySection 4]


IniRead, sectionNames, %ini%
For each, section in StrSplit(sectionNames, "`n")
 {
  IniRead, text, %ini%, %section%, portion
  if (text = portion) && sec := section
   Section_Has_This_Value .= Sec "`n"
 }
MsgBox, % Section_Has_This_Value
How can I replace the value in the variable oldValue which are in [MySection 6] with the value in the variable NewValue which are in the [MySection 4]

I want a NewValue to be incremented to the new value as if we were doing a ++ to it.
In my example num=3 should be num=4
And then do the same thing for all the values that are after the new value, and then here we get a gap that is num=3. Here we should put oldValue (num=4 should place instead of gap => num=3)...
I hope the question is clear.. Thank you very much.
Please see the picture for further clarification
reindex.png
reindex.png (72.66 KiB) Viewed 539 times

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Replace INI Value from some Section to another

Post by mikeyww » 26 Nov 2022, 12:08

Hi,

381 posts.... Would have a stab at IniWrite. It may help to create distinct coding lines that represent each step in your thinking.

Code: Select all

oldSection := ...
oldKey := ... 
newSection := ...
newKey := ...
That helps to break down the process and match your thinking along the way. You've started it, but keep going. Some of the variables might need to be defined inside your loop.

The issue with your issue isn't the coding. It's that you have not clearly identified the steps in your mind, and then also "on paper" or somehow in an organized written manner. It's just a matter of doing it. You could take your diagram, and one example, and then write each of the steps that should happen with that example. How is the new section defined? How is the new key defined?

Once you have each written step, it can be directly translated into the AHK code.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Replace INI Value from some Section to another

Post by HiSoKa » 26 Nov 2022, 12:44

Hi Mikey,
I believe its a shame i have 381 posts. I can't solve a simple problem like this,
Perhaps its reliance on define some variables only and knowing how to deal with them, With some familiarity with IniWrite and IniRead
I will try to do it myself and follow the steps you mentioned to me, Thank you very much for your encouragement

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Replace INI Value from some Section to another

Post by mikeyww » 26 Nov 2022, 12:56

I wouldn't say it's ashame, just means that you are capable of defining the steps that you need!

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Replace INI Value from some Section to another

Post by HiSoKa » 26 Nov 2022, 13:10

I get it,
Sorry Sometimes google translate make mistakes, And give me strange words that shouldn't be said :mrgreen:

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Replace INI Value from some Section to another

Post by mikeyww » 26 Nov 2022, 13:19

People can sometimes do more than they think!

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Replace INI Value from some Section to another

Post by HiSoKa » 27 Nov 2022, 01:49

hi @mikeyww ,
Sorry for the inconvenience, now I am just trying to get all the Sections in which the value of the key num is greater than the value stored in the variable oldValue. I tried a lot and searched a lot, but to no avail, and I did not find similar questions..
This is my attempt:

Code: Select all

ini          := A_Scriptdir . "\config.ini"
portion      := "p1"
oldValue     := "num=4"

IniRead, sectionNames, %ini%
For each, section in StrSplit(sectionNames, "`n")
 {
  IniRead, text, %ini%, %section%, portion
  if (text = portion) && sec := section ; Here I got all Sections that contain this value "p1"
	Section_Has_This_Value .= Sec "`n"

	  IniRead, number, %ini%, %sec%, num
  if (number > oldValue) && sec := section ; after that here I am trying to get all the Sections in which the value of "num" is greater than the value stored in the variable "oldValue"
     Target_Section .= Sec "`n"
 }
MsgBox, % Section_Has_This_Value ; correct result
MsgBox, % Target_Section ; blank MsgBox

Esc::ExitApp

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Replace INI Value from some Section to another  Topic is solved

Post by mikeyww » 27 Nov 2022, 08:23

IniRead:
A standard ini file looks like:
[SectionName]
Key=Value
Thus, to compare to a value that is read, why not oldValue := 4 :?:

A simple debugging strategy is to display the values of some of your key variables in the script. It enables quickly pinpointing problems like this.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Replace INI Value from some Section to another

Post by HiSoKa » 27 Nov 2022, 08:46

wow, Now I'm able to solve problem because i can get the Sections that contain values greater than 4...
Thank you very much, Mikey for this point out :thumbup:

Post Reply

Return to “Ask for Help (v1)”