Advertisement
Guest User

ILM Ballot Counter

a guest
Oct 29th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FileDelete, votes.txt
  2.  
  3. NumBallots = 0
  4.  
  5. ; Count ballots
  6. Loop, Files, ballots\*.txt
  7.     NumBallots += 1
  8.  
  9. IfNotExist, master.txt
  10.     MsgBox, 0, Warning, I can't find a list of nominations (master.txt). The votes will be counted, but there might be doubles in the final results.
  11.  
  12. Progress, b2 w200,, Processing ballots...
  13.  
  14. Loop, Files, ballots\*.txt
  15. {
  16.  
  17.     Prog := (A_Index/NumBallots)*100
  18.     Progress, %Prog%
  19.  
  20.     Loop, Read, %A_LoopFileFullPath%
  21.     {
  22.  
  23.         Vote := A_LoopReadLine
  24.  
  25.         /* CALCULATE POINTS
  26.  
  27.         Change the next uncommented line (Points := 51-A_Index) according to your points system. A_Index is the line number of the current ballot. In this example the first line (where A_Index = 1) gets 50 points (51-1).
  28.  
  29.         This is what I used for the soul poll. The first 25 albums were weighted (first one 30 points, so 31-A_Index), all the others on the ballot got 5 points.
  30.         Points := (A_Index <= 25) ? (31-A_Index) : 5
  31.  
  32.         */
  33.         Points := 51-A_Index
  34.  
  35.         NrOne := (A_Index = 1) ? 1 : 0
  36.  
  37.         IfNotExist, master.txt
  38.             Gosub, CountVotes
  39.         Else
  40.         {
  41.             ; check for album in master list
  42.             FileRead, Master, master.txt
  43.             IfInString, Master, %Vote%
  44.                 Gosub, CountVotes
  45.             Else
  46.             {
  47.                 MsgBox, 0, Error in %A_LoopFileName%, Line %A_Index% not in master list:`n%A_LoopReadLine% (%Points% points)
  48.                 Run, notepad.exe "%A_LoopFileFullPath%"
  49.             }
  50.         }
  51.  
  52.     }
  53.  
  54. }
  55.  
  56. FileRead, Votes, votes.txt
  57. Votes := SortByColumn(Votes, 2, "N R")
  58. Votes := RTrim(Votes, "`n`r")
  59. FileDelete, votes.txt
  60. FileAppend, %Votes%`n, votes.txt
  61.  
  62. Progress, Off
  63. MsgBox, 0, Ready!, %NumBallots% ballots processed.
  64.  
  65. ExitApp
  66.  
  67. Esc::ExitApp
  68.  
  69.  ; -------- Functions and subroutines ---------
  70.  
  71. SortByColumn(Str, SortCol, SortOptions="", ColSep="`t", RowSep="`n", RowOmitChars="") {
  72.     IfEqual,SortCol,1,Sort,Str,D%RowSep% %SortOptions%
  73.     IfEqual,SortCol,1,Return Str
  74.     BS:=Chr(8), AA:=BB:=""
  75.     Loop, parse, Str, %RowSep%, %RowOmitChars%
  76.         p:=InStr(A_LoopField,ColSep,false,1,SortCol-1)
  77.         ,AA.=SubStr(A_LoopField,(p=0?1:p+1)) BS A_LoopField RowSep
  78.     Sort, AA, D%RowSep% %SortOptions%
  79.     Loop, parse, AA, %RowSep%
  80.         BB.=SubStr(A_LoopField,InStr(A_LoopField,BS)+1) RowSep
  81.     Return RTrim(BB, RowSep)
  82. }
  83.  
  84. CountVotes:
  85. ; check for album in votes.txt
  86. FileRead, Votes, votes.txt
  87. IfInString, Votes, %Vote%
  88. {
  89.  
  90.     ; add Points to votes.txt
  91.     Out =
  92.     Loop, Parse, Votes, `n, `r
  93.     {
  94.         IfInString, A_LoopField, %Vote%
  95.         {
  96.             AlbumArray := StrSplit(A_LoopField, "`t")
  97.             Album := AlbumArray[1]
  98.             Points := Points+AlbumArray[2]
  99.             NumVotes := AlbumArray[3]+1
  100.             NrOne := NrOne+AlbumArray[4]
  101.             Out .= Album . "`t" . Points . "`t" . NumVotes . "`t" . NrOne . "`n"
  102.         }
  103.         Else
  104.             Out .= A_LoopField . "`n"
  105.  
  106.     }
  107.     FileDelete, votes.txt
  108.     FileAppend, %Out%, votes.txt
  109.  
  110. }
  111. Else
  112.     FileAppend, %Vote%`t%Points%`t1`t%NrOne%`n, votes.txt
  113.  
  114. Return
  115. ; END CountVotes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement