For the upcoming general election, in addition to scripting pages for North Dakota election results via RSS, I will be getting downloadable text results from the Minnesota Secretary of State election website. You can see examples of past results and a link to how results are formatted here: http://electionresults.sos.state.mn.us/ENR/Select/Download/2.
I need a script to loop through each candidate row, find the top 4 candidates with the highest vote count and then to write those to my results page. My question is, what would be the best (and most efficient) way to go about scripting this?
Below is an example of how I'm currently scripting my North Dakota result pages. The difference between MN and ND is that the ND RSS places all candidates in a single line of data separated by a break tag, which is why I use a single "MasterList" text object to loop through all candidates. I'd like to do something similar for MN results, but just don't know how to go about it.
I admit my scripting skills are severely lacking so any help would be appreciated. Thanks.
ND RSS feed @ http://results.sos.nd.gov/resultsRSS.aspx?text=All&type=SW&map=CTY
' These 5 arrays hold a master combined list of all candidates for all parties
' They will then be sorted
dim names(20) as string
dim votes(20) as integer
dim percentage(20) as string
dim used(20) as boolean ' used for sorting to mark a candidate as already being used
' Index/Loop variables
dim idx as integer
dim i as integer
dim j as integer
dim allcands(0) as string
dim cand as string
dim textobj as xpTextObject
dim group as xpBaseObject
' This text object will be datalinq'd to the appropriate datalinq field
allcands(0) = "MasterList"
' Reset the index
idx = 0
' Clear the used flags
for i = 0 to 20
used(i) = false
next
' Loop over each cand
for each cand in allcands
dim line as string
dim lines(20) as string
if self.GetObjectByName(cand, textobj) then
' Get the candidates and split them by
line = Replace(textobj.text, "
", vbCr)
lines = Split(line, vbCr)
' Loop over all of the candidates and add them to the master list
for i = 0 to UBound(lines)-1
dim segments(3) as string
' Remove the spaces around the dashes and replace with a carriage return
line = Replace(lines(i), " - ", vbCr)
if len(line) > 0 then
' Split the line into it's individual components
segments = split(line, vbCr, 3)
names(idx) = segments(0)
votes(idx) = CInt(segments(1))
percentage(idx) = segments(2)
idx = idx + 1
end if
next
end if
next
' NOW BEGIN THE PROCESS OF FINDING THE HIGHEST VOTE COUNTS
dim numPeople as integer
numPeople = idx
dim maxVotes as Integer
dim currentHighest as Integer
' Now we need to loop over the master combined list and find the highest votes
' We try to find the top 4 candidates
for i = 1 to 4
currentHighest = -1
maxVotes = -1
' Loop over the master candidate list to find the highest unused candidate
for j = 0 to numPeople-1
if used(j) = false then ' make sure we don't pick a used entry
' If this person has more votes than our maximum, use this person
if votes(j) > maxVotes then
currentHighest = j
maxVotes = votes(j)
end if
end if
next
if currentHighest >= 0 then
used(currentHighest) = true
if self.GetObjectByName("Candidate" + i.toString, textobj) then
if Trim(names(currentHighest)) = "write-in" then
textobj.text = "Write In"
else
textobj.text = Trim(names(currentHighest))
end if
end if
if self.GetObjectByName("Votes" + i.toString, textobj) then
textobj.text = votes(currentHighest).toString
end if
if self.GetObjectByName("Percentage" + i.toString, textobj) then
textobj.text = percentage(currentHighest)
end if
' Show the line
if self.GetObjectByName("Person" + i.toString, group) then
group.Visible = true
end if
else
' Hide the line
if self.GetObjectByName("Person" + i.toString, group) then
group.Visible = false
end if
end if
next
#XPression