need help with something that im sure is fairly simple

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nicktux
Posts: 2
Joined: 17 Aug 2018, 13:27

need help with something that im sure is fairly simple

17 Aug 2018, 13:35

I need a script that will look at a text file that has a bunch of names in it, remove any duplicates, leaving only unique lines left, but at the same time, to count how many of each there were, and order them in descending order according to how many of each unique entry there were.

so if the txt file looked like this
apple
orange
banana
apple
orange
apple
banana

running the script would replace that txt file with one that read:
apple - 3
orange - 2
banana - 2

thanks in advance :)

*edit* to have it replace the old file is not necessary it can make a brand new one that it just updates and amends, im not sure what would be easier, but this list that I need tallied and cleaned up is having names written to it automatically by other programs, however, it doesnt occur more often than one name being added every 4-5 minutes or so..
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: need help with something that im sure is fairly simple

17 Aug 2018, 14:35

I over-complicated the entire thing.... but it's working with your sample data:

Code: Select all

#SingleInstance, Force

; List of words
Words =
(
apple
orange
banana
apple
orange
apple
banana
orange
orange
orange
orange
orange
)

; Copy list of words to another variable
WordsUnique := Words

; Sort the copied list to remove duplicates
Sort, WordsUnique, U

; Initialize variable to store values to later
WordCount := ""

; Parse each unique word
Loop, Parse, WordsUnique, `n
{
	; Count how many times the word is duplicated
	StrReplace(Words, A_LoopField, A_LoopField, MatchNum)
	
	; Append the word count to variable
	WordCount .= MatchNum " - " A_LoopField "`n"
}

; Sort by numeric value, then reversed (descending)
Sort, WordCount, NR

; Initialize variable to store values to later
WordCountFinal := ""

; Parse list
Loop, Parse, WordCount, `n
{
	If (A_LoopField = "") {
		Continue
	}

	Hyphen := InStr(A_LoopField, "-",, 1)
	WordNum := SubStr(A_LoopField, 1, Hyphen - 2)
	WordAlpha := SubStr(A_LoopField, Hyphen + 2)
	WordCountFinal .= WordAlpha " - " WordNum "`n"
}

FileDelete, WordCounts.txt
FileAppend, % WordCountFinal, WordCounts.txt
Run, WordCounts.txt
User avatar
oldbrother
Posts: 273
Joined: 23 Oct 2013, 05:08

Re: need help with something that im sure is fairly simple

17 Aug 2018, 14:37

Just for fun:

Code: Select all

WordList=
(
apple
orange
banana
apple
orange
apple
banana
)

CountWord := Object()

Loop, parse, WordList, `n
{
  if (CountWord[A_LoopField] >=1)
	  CountWord[A_LoopField] +=1
  else
    CountWord[A_LoopField] :=1
}

Result :=""

For word, V in CountWord
Result .= word . "  - " . V . "`n"

Msgbox %Result%
nicktux
Posts: 2
Joined: 17 Aug 2018, 13:27

Re: need help with something that im sure is fairly simple

17 Aug 2018, 15:54

thanks for responding so quickly, very much appreciated! I'm going to figure out how to implement and test, but just so I'm aware (noob alert) these scripts aren't looking for the sample words I've supplied specifically are they (banana, orange,...)? they'll be able to identify any unique entry, correct? thanks again <3

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, inseption86, jaka1, LuckyJoe, Rohwedder and 327 guests