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
Ini read and output the Key name that has largest amount Topic is solved
Re: Ini read and output the Key name that has largest amount
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...
Re: Ini read and output the Key name that has largest amount Topic is solved
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
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: Ini read and output the Key name that has largest amount
Hi HIAC,
If you expect only positive integers, this also can be achieved in-line:
[EDIT]
Looks like I misread the title... in order to retrieve the key name instead, and without using RegExMatch:
Code: Select all
IniRead, pairs, % iniFile, INFO
MsgBox % (Object(StrSplit("0`n" . pairs . "`n", ["=", "`n"])*)).maxIndex()
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.
Re: Ini read and output the Key name that has largest amount
@A_AhkUser Thanks.
@Odlanir, is it possible to output last 3 with max value?
@Odlanir, is it possible to output last 3 with max value?
Re: Ini read and output the Key name that has largest amount
@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).
[EDIT:] Here's a version done on 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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Ini read and output the Key name that has largest amount
Could you kindly make me one like you did previously to read .ini file but outputting last 3 instead )
Re: Ini read and output the Key name that has largest amount
Hi HIAC,
Using the method above (btw I edit my first reply which was wrong):
[EDIT]
Note: again, this will work only if you expect only positive integers.
[EDIT]
And in order to retrieve the key names...
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
Note: again, this will work only if you expect only positive integers.
Thanks jeeswg. Btw it relies on THE hack of Helgef to convert ini data to an object@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.
[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.
Re: Ini read and output the Key name that has largest amount
Thank you, how can i divide them in 3 different variables?
Re: Ini read and output the Key name that has largest amount
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...)
Re: Ini read and output the Key name that has largest amount
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=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
Re: Ini read and output the Key name that has largest amount
That's what I said. Here's a more general solution:HIAC wrote:it won't consider numbers with decimals in it, so result would be 15, 8,5 instead of 30,11,15
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()
}
Re: Ini read and output the Key name that has largest amount
That's what I said. Here's a more general solution: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
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
Re: Ini read and output the Key name that has largest amount
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Ini read and output the Key name that has largest amount
Hello
For the slightly off-topic sorting of ini-values,
For v1,
note that the `a option is not needed in v2.
Cheers.
For the slightly off-topic sorting of ini-values,
For all real numbers, v2,A_AhkUser wrote:If you expect only positive integers, this also can be achieved in-line:
Code: Select all
sort(regexreplace(section, "m)(^.+=\s{0,})(.+$)", "$2"), "N")
Code: Select all
sortIniSectionValues(section, opt:="N"){
section:=regexreplace(section, "`am)(^.*=\s{0,})(.*$)", "$2") ; removes keyX =
sort section, % opt
return section
}
Cheers.
Re: Ini read and output the Key name that has largest amount
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
@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
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Rohwedder and 103 guests