Deleting duplicates variables in a loop from an .ini file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
XMCQCX
Posts: 230
Joined: 14 Oct 2020, 23:44

Deleting duplicates variables in a loop from an .ini file

11 Jun 2021, 18:40

Hi,
How to delete duplicates variables in a loop from an .ini file
Go from this:

Code: Select all

[Section]
Var1=Apple
Var2=Apple
Var3=Peach
Var4=Banana
Var5=Apple
To this:

Code: Select all

[Section]
Var1=Apple
Var2=
Var3=Peach
Var4=Banana
Var5=

Code: Select all

Loop 5
{
IniRead, Var%A_Index%, Settings.ini, Section, Var%A_Index%

	When a duplicate is found empty the variable and Write to .ini file

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

Re: Deleting duplicates variables in a loop from an .ini file

11 Jun 2021, 18:55

I would like to suggest that you simply delete the key. If you then try to find the key later, its value will still be null. You can also write that key again at any time.

Code: Select all

ini := A_ScriptDir "\test.ini", arr := []
IniRead, ttext, %ini%, Section
For lineNum, line in StrSplit(ttext, "`n") {
 RegExMatch(line, "(.+)=(.*)", part)
 If arr.HasKey(part2)
  IniDelete, %ini%, Section, %part1%
 Else arr[part2] := True
}
MsgBox, 64, Done, Done!
XMCQCX
Posts: 230
Joined: 14 Oct 2020, 23:44

Re: Deleting duplicates variables in a loop from an .ini file

11 Jun 2021, 22:10

mikeyww wrote:
11 Jun 2021, 18:55
I would like to suggest that you simply delete the key. If you then try to find the key later, its value will still be null. You can also write that key again at any time.
Great suggestion ! I will use it in my script. I would prefer to keep the empty key like in my initial post. So we don't have to type the first part when we want to manually write a value. How to modify the code to keep the first part ? I tried a few things, but wasn't able to make it work. Thanks for the help.
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Deleting duplicates variables in a loop from an .ini file

12 Jun 2021, 05:09

Keeping the key is the opposite of deleting it! In any case, it seems that you might want the following.

Code: Select all

ini := A_ScriptDir "\test.ini", arr := [], new := ""
IniRead, ttext, %ini%, Section
For lineNum, line in StrSplit(ttext, "`n") {
 RegExMatch(line, "(.+)=(.*)", part), new .= "`n" part1 "="
 If !arr.HasKey(part2)
  new .= part2, arr[part2] := True
}
IniWrite, % SubStr(new, 2), %ini%, Section
MsgBox, 64, Done, Done!
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Deleting duplicates variables in a loop from an .ini file  Topic is solved

12 Jun 2021, 05:24

A bit simpler:

Code: Select all

Vars := "`n"
Loop 5
{
   IniRead, Var%A_Index%, Settings.ini, Section, Var%A_Index%
   ; 	Add missing keys
   If (Var%A_Index% = "ERROR") {
      IniWrite, % "", Settings.ini, Section, Var%A_Index%
      Var%A_Index% := ""
   }
   ; 	When a duplicate is found empty the variable and Write to .ini file
   Else If (Var%A_Index% <> "") {
      If !InStr(Vars, "`n" . Var%A_Index% . "`n")
         Vars .= Var%A_Index% . "`n"
      Else {
         IniWrite, % "", Settings.ini, Section, Var%A_Index%
         Var%A_Index% := ""
      }
   }
}
Vars := ""
XMCQCX
Posts: 230
Joined: 14 Oct 2020, 23:44

Re: Deleting duplicates variables in a loop from an .ini file

12 Jun 2021, 14:19

mikeyww wrote:
12 Jun 2021, 05:09
Keeping the key is the opposite of deleting it! In any case, it seems that you might want the following.
just me wrote:
12 Jun 2021, 05:24
A bit simpler:
Thanks for help guys, Both codes are working well.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Fredrik F, maxkill, RandomBoy, ShatterCoder and 376 guests