Count Max X & Max Y from List

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ah_sis90
Posts: 9
Joined: 19 Mar 2019, 04:37

Count Max X & Max Y from List

Post by ah_sis90 » 25 Oct 2021, 12:12

Dears,

Below is the script to count the maximum x from the list as below, but I am facing an issue counting the y, it's always getting the wrong result.

am think ( if am not wrong ) that the function still counting the X instead of Y.

Would you please look at it and reply to me?

Thank you so much for your attention and participation.

Code: Select all

list=
(
500,600
500,700
500,800
600,800
600,600
600,700
600,800
600,900
)

Msgbox % Get_MAX(List) ; Result 600 as 600 counts 5 times on the list on x.
	
Get_MAX(List)
{
	items :=	{}
	Loop, parse, list, `n, `r
	{
		StringSplit, p, A_LoopField, `,`
		RegExReplace(list,"`am)^" p1,"",x)
		items[p1] := {Count:x}
	}

	For x,obj in items 
		XX .= obj.count "," x "`n"
	Sort, XX, R
	list=
	Loop, parse, XX, `n, `r
	{
		if A_Index = 1
		{
			HighestValue := A_LoopField	
			HighestValue :=	RegExReplace(HighestValue,".,")
		}
	}
	return %  HighestValue
}
ExitApp

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

Re: Count Max X & Max Y from List

Post by mikeyww » 25 Oct 2021, 12:38

This function returns a single value, so it's not clear what it should be returning instead.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Count Max X & Max Y from List

Post by flyingDman » 25 Oct 2021, 15:10

Why not using something like this:

Code: Select all

list =
(
500,600
500,700
500,800
600,800
600,600
600,700
600,800
601,900
)

maxx := 0, maxy := 0
for x,y in strsplit(list, "`n", "`r")
	{
	maxx := max(strsplit(y,",").1,maxx)
	maxy := max(strsplit(y,",").2,maxy)
	}
msgbox % maxx "`n" maxy
14.3 & 1.3.7

ah_sis90
Posts: 9
Joined: 19 Mar 2019, 04:37

Re: Count Max X & Max Y from List

Post by ah_sis90 » 25 Oct 2021, 17:11

mikeyww wrote:
25 Oct 2021, 12:38
This function returns a single value, so it's not clear what it should be returning instead.
Thanks for your kind reply, maybe I didn't explain well.

I know that the return value is a single value, and as an example:
the list contains x,y

Code: Select all

list =
(
500,600
500,700
500,800
600,800
600,600
600,700
600,800
600,900
)
x contains 500 & 600
500 count is 3
600 count is 5

I need from the function to return the 600 as it is the max count on the X list.

ah_sis90
Posts: 9
Joined: 19 Mar 2019, 04:37

Re: Count Max X & Max Y from List

Post by ah_sis90 » 25 Oct 2021, 17:16

flyingDman wrote:
25 Oct 2021, 15:10
Why not using something like this:

Code: Select all

list =
(
500,600
500,700
500,800
600,800
600,600
600,700
600,800
601,900
)

maxx := 0, maxy := 0
for x,y in strsplit(list, "`n", "`r")
	{
	maxx := max(strsplit(y,",").1,maxx)
	maxy := max(strsplit(y,",").2,maxy)
	}
msgbox % maxx "`n" maxy

Thanks for your kind reply and your created function, but I don't want to retrieve the max value.
All I need to retrieve the max counted value as below example:

the list contains x,y

x contains 500 & 600
500 count is 3
600 count is 5

I need the function to return the 600 as it is the max count on the X list.

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

Re: Count Max X & Max Y from List

Post by mikeyww » 25 Oct 2021, 18:00

Your initial post indicated, "I am facing an issue counting the y, it's always getting the wrong result". Has the question changed during this thread?

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Count Max X & Max Y from List

Post by flyingDman » 25 Oct 2021, 18:26

Does this work?:

Code: Select all

list =
(
500,600
500,700
500,800
600,800
600,600
600,700
600,800
600,900
)
cntx := [], cnty := []
for a,b in strsplit(list, "`n", "`r")
	{
	x := strsplit(b,",").1, y := strsplit(b,",").2
	cntx[x] := !ObjHasKey(cntx, x) ? 1 : cntx[x] + 1
	cnty[y] := !ObjHasKey(cnty, y) ? 1 : cnty[y] + 1
	maxcntx := cntx[x] > maxcntx ? (cntx[x], rsltx := x) : maxcntx 
	maxcnty := cnty[y] > maxcnty ? (cnty[y], rslty := y) : maxcnty 
	}
msgbox % rsltx "`t" maxcntx "x`n" rslty "`t" maxcnty "x"
14.3 & 1.3.7

ah_sis90
Posts: 9
Joined: 19 Mar 2019, 04:37

Re: Count Max X & Max Y from List

Post by ah_sis90 » 25 Oct 2021, 23:59

mikeyww wrote: Your initial post indicated, "I am facing an issue counting the y, it's always getting the wrong result". Has the question changed during this thread?

No it's not, it just: "maybe I didn't explain well."

ah_sis90
Posts: 9
Joined: 19 Mar 2019, 04:37

Re: Count Max X & Max Y from List

Post by ah_sis90 » 26 Oct 2021, 00:01

flyingDman wrote:
25 Oct 2021, 18:26
Does this work?:

Code: Select all

list =
(
500,600
500,700
500,800
600,800
600,600
600,700
600,800
600,900
)
cntx := [], cnty := []
for a,b in strsplit(list, "`n", "`r")
	{
	x := strsplit(b,",").1, y := strsplit(b,",").2
	cntx[x] := !ObjHasKey(cntx, x) ? 1 : cntx[x] + 1
	cnty[y] := !ObjHasKey(cnty, y) ? 1 : cnty[y] + 1
	maxcntx := cntx[x] > maxcntx ? (cntx[x], rsltx := x) : maxcntx 
	maxcnty := cnty[y] > maxcnty ? (cnty[y], rslty := y) : maxcnty 
	}
msgbox % rsltx "`t" maxcntx "x`n" rslty "`t" maxcnty "x"
Its Work Perfectaly.

Thanks so much. :clap:

Post Reply

Return to “Ask for Help (v1)”