Custom Sort help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Custom Sort help

26 Jul 2018, 10:53

hi-
I have a script that I use to monitor the status outputs on some test equipment. When there is an event, a line is added to a text file. The text file looks like this: Mac address followed by an error/status code and description.

Code: Select all

00-C0-XX-XX-XX-XX > E5002: Temperature Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5001: Temperature Lo Limit Exceeded
00-C0-XY-XY-XY-XY > E5003: Voltage Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5004: Voltage Lo Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5006: Impedance Lo Limit Exceeded
00-C0-XZ-XY-XZ-XY > E5002: Temperature Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5002: Temperature Hi Limit Exceeded
00-C0-XX-XX-XX-XX > E5002: Temperature Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5001: Temperature Lo Limit Exceeded
00-C0-XY-XY-XY-XY > E5003: Voltage Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5004: Voltage Lo Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5006: Impedance Lo Limit Exceeded
00-C0-XZ-XY-XZ-XY > E5002: Temperature Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5002: Temperature Hi Limit Exceeded
What I am trying to do is to sort this text file by the number of duplicates. so the line that has the most duplicates (happens the most) would be sorted to the top of the list. kinda gives me a Pareto chart type of ranking. i'm not looking to delete or in any other way change the file, i just want to be able to sort it so the highest occurring instances are at the top of the file and the lowest occurring are at the bottom. Any ideas how to accomplish this? I am currently doing it on a separate computer using a couple of excel functions, but i'd like to try and do this using autohotkey since that is what i am using to monitor these outputs and write the text file.

thanks
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: Custom Sort help

26 Jul 2018, 13:18

Not sure if this is the most efficient way to do it, but it works.

Code: Select all

str := "
(
00-C0-YY-YY-YY-YY > E5001: Temperature Lo Limit Exceeded
00-C0-XX-XX-XX-XX > E5002: Temperature Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5003: Voltage Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5004: Voltage Lo Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XX-XX-XX-XX > E5002: Temperature Hi Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XX-XX-XX-XX > E5002: Temperature Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5006: Impedance Lo Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XZ-XY-XZ-XY > E5002: Temperature Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5002: Temperature Hi Limit Exceeded
00-C0-XX-XX-XX-XX > E5002: Temperature Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5001: Temperature Lo Limit Exceeded
00-C0-XY-XY-XY-XY > E5003: Voltage Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5004: Voltage Lo Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-XY-XY-XY-XY > E5006: Impedance Lo Limit Exceeded
00-C0-XZ-XY-XZ-XY > E5002: Temperature Hi Limit Exceeded
00-C0-XZ-XZ-XY-XY > E5005: Impedance Hi Limit Exceeded
00-C0-YY-YY-YY-YY > E5002: Temperature Hi Limit Exceeded
)"

;add each line as an object key and count the number of times it appears
objLines := {}
Loop, parse, str, `n, `r
	objLines[A_LoopField] := objLines.HasKey(A_LoopField) ? objLines[A_LoopField] + 1 : 1

;sort lines based on occurrence count
sortedLines := []
for line, ct in objLines
{
	thisLine := { "string" : line, "count" : ct }
	insertSpot := 0
	Loop % sortedLines.Length()
		if (ct > sortedLines[A_Index].count)
		{
			insertSpot := A_Index
			Break
		}

	if insertSpot
		sortedLines.InsertAt(insertSpot, thisLine)
	else
		sortedLines.Push(thisLine)
}

;output lines in order of most frequent
result := ""
for each, sortedLine in sortedLines
	Loop % sortedLine.count
		result .= sortedLine.string . "`r`n"
MsgBox % result
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Custom Sort help

26 Jul 2018, 16:08

works perfect, thanks for you help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, mikeyww and 250 guests