Convert the numbers on the screen to a text view

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bapl
Posts: 119
Joined: 17 Apr 2021, 00:24

Convert the numbers on the screen to a text view

15 Apr 2023, 20:07

Hello!
I have an array of data with the following values:

Code: Select all

Array := {"A1":"300","A2":"90","A3":"150"}
I need to sort them so that the key with the lowest value is output first, that is, something like this should turn out:

Code: Select all

Array := {"A2":"90","A3":"150","A1":"300"}
This is only the first moment.
There is one but..

Sometimes a key can store two values at once, for example "A4":"170.80"
and you need to somehow make sure that the array no longer has A4 170 and 80, but for example A4 170 and B4 80.

What do I need all this for:
I have the following code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallMouseHook
#InstallKeybdHook
#NoEnv
#Include Include\Gdip_All.ahk
#Include Include\Gdip_ImageSearch2022.ahk
I_Icon = icon.ico
Menu, Tray, Icon, %I_Icon%

Process, Priority, , H
CoordMode Pixel, Screen
Numpad4::

Token := Gdip_Startup()
WinGet, hwnd, ID, A 
;hwnd := "790488"

ArrayVar := {}
Loop, 10
{
bmpHaystack := Gdip_BitmapFromHWND(hwnd)
bmpNeedle := Gdip_CreateBitmapFromFile(A_Index - 1 ".bmp") ;
image1 := Gdip_ImageSearch(bmpHaystack, *4 bmpNeedle, outputVar,932,930,0,0,0,0xFF0000,1,3)
Gdip_DisposeImage(bmpHaystack)
Gdip_DisposeImage(bmpNeedle)
Sleep, 17
If outputVar 
{
ClipBoard := outputVar
outputVar := RegExReplace(outputVar, "(.*)\,.*","$1")
ArrayVar[A_Index - 1] := outputVar
}
}
For key, value in ArrayVar
{
    MsgBox, % "Key: " key ", Value: " value
}

return 
What is he doing:
I have 10 images named from 0.bmp to 9.bmp, each image searches for a digit on the screen at a specific location.
If he finds a digit, he remembers its coordinates and writes it to an array with the desired key.
After a full scan of the screen, I need to calculate in what order the numbers are in order to convert them into a readable form.
I haven't come up with anything better yet.
Rohwedder
Posts: 7749
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Convert the numbers on the screen to a text view

15 Apr 2023, 23:53

Hallo,
your sorting makes no sense!
{"A1":"300","A2":"90","A3":"150"} and {"A2":"90","A3":"150","A1":"300"} is the same array!
See:

Code: Select all

Array1 := {"A1":"300","A2":"90","A3":"150"}
Array2 := {"A2":"90","A3":"150","A1":"300"}
For Key, Value in Array1
	Content1 .= Value ":" Key " "
For Key, Value in Array2
	Content2 .= Value ":" Key " "
MsgBox,% "Content1: " Content1 "`nContent2: " Content2
This is how the sorting could work if it made sense:

Code: Select all

Array := {"A1":"300","A2":"90","A3":"150","A4":"170.80"}
Content := ""
For Key, Value in Array
{
	Split := StrSplit(Value, ".")
	Content .= Split.1 ":" Key ","
	IF Split.2
		Content .= Split.2 ":" StrReplace(Key,"A","B") ","
}
Content := RTrim(Content, ",")
Sort, Content, N D,
MsgBox,% "Content:`n" StrReplace(Content, ",", "`n")
Loop, Parse, Content, CSV
{
	Split := StrSplit(A_LoopField, ":")
	Array[Split.2] := Split.1
}
For Key, Value in Array
	MsgBox,% "Key: " Key "`tValue. " Value
User avatar
Chunjee
Posts: 1487
Joined: 18 Apr 2014, 19:05
Contact:

Re: Convert the numbers on the screen to a text view

16 Apr 2023, 01:32

Ah yes correct, ahk keys objects are not real sortable (the key dictates the iteration order)

You can alter that I think with viewtopic.php?f=6&t=37083 or similar. The easier thing to do would be to use a different data structure such as a table or array.
User avatar
Chunjee
Posts: 1487
Joined: 18 Apr 2014, 19:05
Contact:

Re: Convert the numbers on the screen to a text view

16 Apr 2023, 01:51

for fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

Array := {"A1":"300", "A2":"90", "A3":"150", "A4":"80"}
; split each element and sort by the 2nd value
sortedArr := A.sortBy(A.toPairs(Array), 2)
print(sortedArr)
; => [["A4", "80"], ["A2", "90"], ["A3", "150"], ["A1", "300"]]
https://biga-ahk.github.io/biga.ahk/#/?id=frompairs
https://biga-ahk.github.io/biga.ahk/#/?id=sortby



bapl wrote:
15 Apr 2023, 20:07
Sometimes a key can store two values at once, for example "A4":"170.80"
and you need to somehow make sure that the array no longer has A4 170 and 80, but for example A4 170 and B4 80.
I did not change the key-part to a "B" but I hope you can figure that part out, split the thingy, convert it to the next character or whatever is desired.

Code: Select all

Array := {"A1":"300", "A2":"90", "A3":"150", "A4":"170.80"}
newStruct := []
for key, value in Array {
	if (inStr(value, ".")) {
		splitValue := strSplit(value, ".")
		newStruct.push([key, splitValue[1]])
		newStruct.push([key "+1", splitValue[2]])
		continue
	}
	newStruct.push([key, value])
}
print(newStruct)
; => [["A1", "300"], ["A2", "90"], ["A3", "150"], ["A4", "170"], ["A4+1", "80"]]


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Ragnar, Rohwedder and 129 guests