Ini read and output the Key name that has largest amount Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Ini read and output the Key name that has largest amount

27 Nov 2017, 02:54

Hey people!
I would like to get "C" because it has the largest value in "INFO" selection.. How can I do that?
I would note that values are continuously changing

[INFO]
A=1
B=3
C=5
[INFOB]
D=20
gregster
Posts: 3598
Joined: 30 Sep 2013, 06:48

Re: Ini read and output the Key name that has largest amount

27 Nov 2017, 03:32

IniRead can read a specific section and deliver a linefeed (`n) delimited list. I would loop-parse that list into single lines and apply StrSplit with a '=' delimiter to each line, leaving me with pairs of a letter and a number, of which I would look for that one with the highest number. And repeat...
Odlanir
Posts: 576
Joined: 20 Oct 2016, 08:20

Re: Ini read and output the Key name that has largest amount  Topic is solved

27 Nov 2017, 07:04

Code: Select all

IniRead, allSect, myIni.ini, INFO
maxValue := 0
loop, parse, allSect, `n
{
   RegExMatch(a_loopfield, "O)(.*)=(.*)",v)
   if ( v[2] > maxValue ) 
      maxValue := v[2], key := v[1]
}
MsgBox % "The key " key "`nHolds the max value : " maxValue
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Ini read and output the Key name that has largest amount

27 Nov 2017, 07:07

Thanks a lot! :D
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Ini read and output the Key name that has largest amount

27 Nov 2017, 09:42

Hi HIAC,

If you expect only positive integers, this also can be achieved in-line:

Code: Select all

IniRead, pairs, % iniFile, INFO
MsgBox % (Object(StrSplit("0`n" . pairs . "`n", ["=", "`n"])*)).maxIndex()
[EDIT]
Looks like I misread the title... in order to retrieve the key name instead, and without using RegExMatch:

Code: Select all

IniRead, pairs, ini.ini, INFO
max := (Object(StrSplit("0=" . (pairs:=StrReplace(pairs, "`n", "=")) . "=", "=")*)).maxIndex()
str := SubStr(pairs, 1, InStr(pairs, "=" . max . "=") - 1)
MsgBox % SubStr(str, InStr(str, "=", false, 0) + 1)
Last edited by A_AhkUser on 01 Dec 2017, 16:17, edited 2 times in total.
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 14:49

@A_AhkUser Thanks.

@Odlanir, is it possible to output last 3 with max value?
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 16:17

@A_AhkUser: This is nice, a nice hack that you've done to get the maximum number in a list. Here's a version done on a comma-separated list instead of some ini data.

WARNING: Both these scripts are out of interest, they are not general methods, and can only handle integers (and where: max >= 0).

Code: Select all

q:: ;number list get maximum
;1,3,5 -> 1,,3,,5, -> {1:"",3:"",5:""}
;5 is MaxIndex, 5 is Length

vText := "1,3,5"
oArray := Object(StrSplit(StrReplace(vText, ",", ",,") ",")*)
MsgBox, % oArray.MaxIndex()

;one-liner:
MsgBox, % Object(StrSplit(StrReplace(vText, ",", ",,") ",")*).MaxIndex()
return
[EDIT:] Here's a version done on ini data.

Code: Select all

q:: ;ini data get maximum value
;a=1,c=3,e=5 -> a,1,c,3,e,5 -> 0,a,1,c,3,e,5, -> {0:"a",1:"c",3:"e",5:""}
vText := "
(
a=1
c=3
e=5
)"
oArray := Object(StrSplit("0," vText ",", [",","="])*)
MsgBox, % oArray.MaxIndex()
return
Last edited by jeeswg on 01 Dec 2017, 16:29, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 16:21

Could you kindly make me one like you did previously to read .ini file but outputting last 3 instead :))
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 16:53

Hi HIAC,

Using the method above (btw I edit my first reply which was wrong):

Code: Select all

IniRead, pairs, ini.ini, INFO
o := Object(StrSplit("0`n" . pairs . "`n", ["=", "`n"])*)
maxToRetieve := 3
Loop % maxToRetieve {
var%a_index% := o.maxIndex()
o.removeAt(o.MaxIndex())
}
MsgBox % var1
MsgBox % var2
MsgBox % var3
[EDIT]
Note: again, this will work only if you expect only positive integers.
@A_AhkUser: This is nice, a nice hack that you've done to get the maximum number in a list. Here's a version done on a comma-separated list instead of some ini data.
Thanks jeeswg. Btw it relies on THE hack of Helgef to convert ini data to an object

[EDIT]

And in order to retrieve the key names...

Code: Select all

IniRead, pairs, ini.ini, INFO
pairs := "0=" . StrReplace(pairs, "`n", "=") . "="
o := Object(StrSplit(pairs, "=")*)
maxToRetieve := 3
Loop % maxToRetieve {
MsgBox % SubStr(pairs, InStr(pairs, (str:="=" . o.maxIndex() . "=")) - 1, 1)
o.removeAt(o.MaxIndex())
}
Last edited by A_AhkUser on 01 Dec 2017, 17:43, edited 1 time in total.
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 17:01

Thank you, how can i divide them in 3 different variables?
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 17:46

HIAC wrote:Thank you, how can i divide them in 3 different variables?

You can use for example a pseudo-array (I edited my last post with an example of a pseudi-array and also once again added an example which retrieve key names instead...)
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Ini read and output the Key name that has largest amount

01 Dec 2017, 18:32

Thanks a lot! It works, but there is another problem -.-

A=30.000000
B=15
C=5
D=8
E=11.800000
F=1

it won't consider numbers with decimals in it, so result would be 15, 8,5 instead of 30,11,15
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Ini read and output the Key name that has largest amount

02 Dec 2017, 11:33

HIAC wrote:it won't consider numbers with decimals in it, so result would be 15, 8,5 instead of 30,11,15
That's what I said. Here's a more general solution:

Code: Select all

maxToRetieve := 3
IniRead, sections, ini.ini, INFO
o := {}, s := ""
Loop, parse, % sections, `n
	i := InStr(A_LoopField, "="), l := SubStr(A_LoopField, 1, i), m := SubStr(A_LoopField, i), o[m] := l, s .= m
var := LTrim(s, "=")
Sort, var, N D=
o := StrSplit(var, "=")
Loop % maxToRetieve {
MsgBox % o[o.length()]
o.pop()
}
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Ini read and output the Key name that has largest amount

02 Dec 2017, 11:43

A_AhkUser wrote:
HIAC wrote:it won't consider numbers with decimals in it, so result would be 15, 8,5 instead of 30,11,15
That's what I said. Here's a more general solution:

Code: Select all

maxToRetieve := 3
IniRead, sections, ini.ini, INFO
o := {}, s := ""
Loop, parse, % sections, `n
{
	i := InStr(A_LoopField, "=") ; what's the position of '='  in the current field ('A_LoopField') from 'sections' retrieved by parsing it using `n as delimiter ?
	l := SubStr(A_LoopField, 1, i) ; left part
	m := SubStr(A_LoopField, i) ; right part
	o[m] := l ; key and value are reversed: one can use the value to retrieve the key later
	s .= m ; appends '=value' to 's'
}
var := LTrim(s, "=") ; remove the trailing '=' from the begining of 's'
Sort, var, N D= ; sort 'var' numerically using = as delimiter
o := StrSplit(var, "=") ; creates an array by splitting var using '=' as delimiter
Loop % maxToRetieve
	MsgBox % o.pop() ; removes and returns the last array element.

[EDIT] Added comments
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Ini read and output the Key name that has largest amount

02 Dec 2017, 21:07

Here are two other ideas:

Code: Select all

q::
vText := " ;continuation section
(
A=30.000000
B=15
C=5
D=8
E=11.800000
F=1
)"
Sort, vText, F SortPairAsc
MsgBox, % vText
return

SortPairAsc(vText1, vText2, vOffset) ;for use with AHK's Sort command
{
	vNum1 := RegExReplace(vText1, ".*=")
	vNum2 := RegExReplace(vText2, ".*=")
	return vNum1 < vNum2 ? 1 : vNum1 > vNum2 ? -1 : -vOffset
}

w::
vText := " ;continuation section
(
A=30.000000
B=15
C=5
D=8
E=11.800000
F=1
)"

oArray := []
Loop, Parse, vText, `n, `r
{
	oTemp := StrSplit(A_LoopField, "=")

	;Objects
	;https://autohotkey.com/docs/Objects.htm
	;AHK x32 supports -2147483648 to 2147483647

	;negative so that the bigger numbers
	;appear first in an object loop
	vNum := Round(-oTemp.2*1000000)
	if !oArray.HasKey(vNum)
		oArray[vNum] := []
	oArray[vNum].Push(A_LoopField)
}

vOutput := ""
for vKey in oArray
	for vKey2, vValue in oArray[vKey]
		vOutput .= vValue "`r`n"
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4056
Joined: 17 Jul 2016, 01:02
Contact:

Re: Ini read and output the Key name that has largest amount

03 Dec 2017, 07:30

Hello :wave:
For the slightly off-topic sorting of ini-values,
A_AhkUser wrote:If you expect only positive integers, this also can be achieved in-line:
For all real numbers, v2,

Code: Select all

sort(regexreplace(section, "m)(^.+=\s{0,})(.+$)", "$2"), "N")
For v1,

Code: Select all

sortIniSectionValues(section, opt:="N"){
	section:=regexreplace(section, "`am)(^.*=\s{0,})(.*$)", "$2")	; removes keyX = 
	sort section, % opt
	return section
}
note that the `a option is not needed in v2.

Cheers.
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Ini read and output the Key name that has largest amount

04 Dec 2017, 05:33

Thanks everyone.

@A_AhkUser

This is what I need, but just if I could output the Key name too! I saw the code you posted, but I'm just too new so I don't get it :(

Code: Select all

IniRead, pairs, ini.ini, INFO
o := Object(StrSplit("0`n" . pairs . "`n", ["=", "`n"])*)
maxToRetieve := 3
Loop % maxToRetieve {
var%a_index% := o.maxIndex()
o.removeAt(o.MaxIndex())
}
MsgBox % var1
MsgBox % var2
MsgBox % var3

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], john_c, Rohwedder and 201 guests