Counting how many times item is in string Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
AHKStudent
Posts: 466
Joined: 05 May 2018, 12:23

Counting how many times item is in string

18 Apr 2019, 02:03

sku := "2122,3285,6525,2122,5485,6525"

I would like to get a list that shows how many times a item is in list from the most to least

2 2122
2 6525
1 3285
1 5485

I found code by sinkfaze that does everything except sorting by the number of occurrences

Code: Select all

text =
(
1
1
5
1
3
9
10
11
3
4
3
26
26
6
2
12
2
7
2
6
10
)
pos=1
While	pos :=	RegExMatch(text,"\b\d+\b",m,pos+StrLen(m))
	n .=	(!n ? "" : "`n") m
Sort, n, U N
Loop, parse, n, `n, `r
	RegExReplace(text,"\b" A_LoopField "\b","$0",count), out .=	A_LoopField " - " count "`n"
MsgBox %	out
ExitApp
Odlanir
Posts: 365
Joined: 20 Oct 2016, 08:20

Re: Counting how many times item is in string

18 Apr 2019, 03:03

Code: Select all

myvar := "2122,3285,6525,2122,5485,6525"
myColl := ComObjCreate("Scripting.Dictionary")
For Each, x In StrSplit(myVar, ",")
    myColl.Exists(x) ? myColl.Item(x) ++ : myColl.Item(x) := 1
For x In myColl
	myResult .=  myColl.Item(x) "`t" x "`n"
Sort, myResult, RN
MsgBox % myResult
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Counting how many times item is in string

18 Apr 2019, 03:11

If you'd rather keep what you're already written, do:

Code: Select all

text =
(
1
1
5
1
3
9
10
11
3
4
3
26
26
6
2
12
2
7
2
6
10
)
pos=1
While	pos :=	RegExMatch(text,"\b\d+\b",m,pos+StrLen(m))
	n .=	(!n ? "" : "`n") m
Sort, n, U N
Loop, parse, n, `n, `r
	RegExReplace(text,"\b" A_LoopField "\b","$0",count), out .=	count " - " A_LoopField "`n"
Sort, out, N R	; Sort numerically and in reverse order (so that large numbers come first)
MsgBox %	out
ExitApp
The only changes are switching "count" and "A_loopField" in the "out" assignment and adding the Sort line below it.
just me
Posts: 6366
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Counting how many times item is in string  Topic is solved

18 Apr 2019, 03:30

You don't need to use a Scripting.Dictionary:

Code: Select all

MyStr := "2122,3285,6525,2122,5485,6525"
MyArr := []
For Each, X In StrSplit(MyStr, ",")
   MyArr[X] := (V := MyArr[X]) ? (V + 1) : 1
MyResult := ""
For X, V In MyArr
	MyResult .=  (MyResult ? "`n" : "") . V . "`t"  . X
Sort, MyResult, RN
MsgBox, %MyResult%
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Re: Counting how many times item is in string

18 Apr 2019, 04:40

AHKStudent wrote:
18 Apr 2019, 02:03
There probably is a better way to do this, but this works :) assuming each number combo is 4 digits

Code: Select all

numberCombo := "2122,3285,6525,2122,5485,6525"

For key in (sku := StrSplit(numberCombo, ","))
	StrReplace(numberCombo, sku[A_Index],, Occurances)
,	results .= Occurances . sku[A_Index] "`n"	

Sort, results, N U R
For key in (results := StrSplit(results, "`n"))
	if (A_Index != results.Length())
		sortedResults .= SubStr(results[A_Index], 1, StrLen(results[A_Index]) - 4) "`t"	SubStr(results[A_Index], -3) "`n"		
MsgBox % sortedResults
Last edited by Tigerlily on 18 Apr 2019, 05:01, edited 3 times in total.
-TL
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Counting how many times item is in string

18 Apr 2019, 04:55

You'd better use Scripting.Dictionary
Because
Arry is... an idiot

Code: Select all

MyStr := "a,A,a,2122,3285,6525,2122,5485,6525"
User avatar
nnnik
Posts: 4187
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Counting how many times item is in string

18 Apr 2019, 05:16

When using an array like a dictionary it might be good to use ObjRawGet and ObjRawSet.
In this case however it isn't necessary.
Recommends AHK Studio
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Counting how many times item is in string

18 Apr 2019, 05:28

ObjRawSet is caseInSensitive too
User avatar
nnnik
Posts: 4187
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Counting how many times item is in string

18 Apr 2019, 05:35

Almost everything in AHK is caseInsensitive.
Recommends AHK Studio
AHKStudent
Posts: 466
Joined: 05 May 2018, 12:23

Re: Counting how many times item is in string

18 Apr 2019, 06:03

thank you for so many solutions, :thumbup:

Return to “Ask For Help”

Who is online

Users browsing this forum: aifritz, Odlanir, Portwolf, Rohwedder and 166 guests