Posted: Sat Nov 07, 2009 12:56 am
Good morning smash,
It’s great that you posted the script you are using. It will really help us “tuneâ€
It’s great that you posted the script you are using. It will really help us “tuneâ€
Questions and answers about the LiveCode platform.
https://forums.livecode.com/
Code: Select all
global theData
on mouseUp
sortData
tagScores
end mouseUp
on sortData
put empty into theData
set the itemdelimiter to tab -- sets the delimiter to the default table delimiter
repeat with i = 1 to the number of lines in field "scoresField" -- this iterates through the field
put i & tab & line i of field "scoresField" & cr after theData -- this adds a tab delimited line number to the front of the data line, and cr after to keep things line based
put empty into item 3 line i of field "scoresField" -- While iterating might as well clear out column 3
set the forecolor of line i field "scoresField" to empty -- since we're iterating through the field, might as well set the colors back to empty
end repeat
--set the itemdelimiter to tab -- sets the delimiter to the default table delimiter
-- for my table, the scores are in item 3. As in the other script if you have a % sign in your field, use replace to get rid of it so you can do a numeric search.
-- since this is only the data in the variable, theres no need to mess with the % in the table
sort lines of theData descending numeric by item 3 of each
put theData
end sortData
-- handler to handle tagging and colorization
on tagScores
set the itemdelimiter to tab -- set the delimiter to match the data we're working with
put "1st" & tab & "2nd" & tab & "3rd" & tab & "4th" & tab & "5th" & tab & "6th" & tab & "7th" & tab & "8th" & tab & "9th" & tab & "10th" into theRanks -- a list of the possible rankings
put "Blue" & tab & "Red" & tab & "Green" & tab & "Orange" & tab & "cyan" & tab & "purple" & tab & "yellow" & tab & "Brown" & tab & "magenta" & tab & "blue" into theColors -- a list of the possible colors
-- to do it this way I did, you need 2 rank counters one for actual, one for effective. Actual will be handled by the repeat loop effective is handled by rankCounter
put 1 into rankCounter
put 10 into numOfRanks -- define how many ranks
-- adjust the table as desired in this loop
repeat with i = 1 to the number of lines in theData -- loop through the number of lines in theData
-- check to see if we're still within the range of assigned rankings, but not at the very first record.
-- also checks to make sure this is not a duplicate score. If its not a dupe, adjust the corresponding field line to insert the actual rank level.
if i > 1 and i < numOfRanks + 1 and item 3 line i of theData < item 3 line i-1 of theData then
put item i of theRanks into item 3 line (item 1 line i of theData) of field "scoresField"
set the forecolor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item i of theColors -- sets the color
put i into rankCounter -- since this is NOT a duplicate score, set the effective and actual rank index to match
else if i > 1 and i < numOfRanks + 1 and item 3 of line i of theData = item 3 line i-1 of theData then -- this handles duplicates
put item rankCounter of theRanks into item 3 line (item 1 line i of theData) of field "scoresField" -- uses the effective rather than actual index to set color and rank in the field
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item rankCounter of theColors
-- handles the very first line whcih will always be first and red
else if i = 1 then
put item i of theRanks into item 3 line (item 1 line i of theData) of field "scoresField"
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item i of theColors
-- this handles the special case where 5th position IS or HAS 1 or more duplicates. Does a direct comparison to the 4th line of theData
else if i > numOfRanks and item 3 of line i of theData = item 3 of line 4 of theData then
put item rankCounter of theRanks into item 3 line (item 1 line i of theData) of field "scoresField"
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item rankCounter of theColors
else
-- if none of the above match, no reason to finish cycling through theData
exit repeat
end if
end repeat
end tagScoresCode: Select all
global theData,theSuffix
on mouseUp
sortData
tagScores
end mouseUp
on sortData
put empty into theData
set the itemdelimiter to tab -- sets the delimiter to the default table delimiter
repeat with i = 1 to the number of lines in field "scoresField" -- this iterates through the field
put i & tab & line i of field "scoresField" & cr after theData -- this adds a tab delimited line number to the front of the data line, and cr after to keep things line based
put empty into item 3 line i of field "scoresField" -- While iterating might as well clear out column 3 which contains 1st, 2nd etc
set the forecolor of line i field "scoresField" to empty -- since we're iterating through the field, might as well set the colors back to empty
end repeat
--set the itemdelimiter to tab -- sets the delimiter to the default table delimiter
-- for my table, the scores are in item 2, item 3 in theData. As in the other script if you have a % sign in your field, use replace to get rid of it so you can do a numeric sort.
sort lines of theData descending numeric by item 3 of each
put theData
end sortData
-- handler to handle tagging and colorization
on tagScores
set the itemdelimiter to tab -- set the delimiter to match the data we're working with
put "Blue" & tab & "Red" & tab & "Green" into theColors
-- to do it this way I did, you need 2 rank counters one for actual, one for effective. Actual will be handled by the repeat loop effective is handled by rankCounter
put 1 into rankCounter
-- adjust the table as desired in this loop
repeat with i = 1 to the number of lines in theData -- loop through the number of lines in theData
-- check to see if we're still within the range of assigned rankings, but not at the very first record.
-- also checks to make sure this is not a duplicate score. If its not a dupe, adjust the corresponding field line to insert the actual rank level.
if i > 1 and item 3 line i of theData < item 3 line i-1 of theData then
ordinate i
put theSuffix into item 3 line (item 1 line i of theData) of field "scoresField"
set the forecolor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item i of theColors -- sets the color
put i into rankCounter -- since this is NOT a duplicate score, set the effective and actual rank index to match
else if i > 1 and item 3 of line i of theData = item 3 line i-1 of theData then -- this handles duplicates
ordinate rankCounter
put theSuffix into item 3 line (item 1 line i of theData) of field "scoresField" -- uses the effective rather than actual index to set color and rank in the field
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item rankCounter of theColors
-- handles the very first line whcih will always be first and red
else if i = 1 then
put "1st" into item 3 line (item 1 line i of theData) of field "scoresField"
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item i of theColors
end if
end repeat
end tagScoresCode: Select all
global theSuffix
on ordinate pNum
switch
case pNum is among the items of "11,12,13"
put pNum & "th " into theSuffix
exit switch
case the last char of pNum is "1"
put pNum & "st " into theSuffix
exit switch
case the last char of pNum is "2"
put pNum & "nd " into theSuffix
exit switch
case the last char of pNum is "3"
put pNum & "rd " into theSuffix
exit switch
default
put pNum & "th " into theSuffix
end switch
end ordinatewhich works brilliantly, i am just trying to work out if how to skip a placing/placings if there are equal percentages ???on mouseUp
/* Identify the table field */
put "Entries" into tableFieldName
put card field tableFieldName into tableStore
/* Identify the column number you want to colour */
put 4 into chosenColumn
-- extract the desired column, adding the line numbers, into variable
set the itemDelimiter to tab
repeat with rowNumber = 1 to the number of lines in tableStore
put (item chosenColumn of line rowNumber of tableStore)&tab&rowNumber&return after extractedColumn
-- clear previous highlights in column
set the foregroundcolor of item chosenColumn of line rowNumber of card field tableFieldName to empty
put empty into item (chosenColumn + 1) of line rowNumber of card field tableFieldName
end repeat
replace "%" with empty in extractedColumn
-- sort the variable
sort numeric descending extractedColumn
/* Currently, we are colouring the top three percentages */
put 12 into rankLevels
put 1 into currentRank
put item 1 of line 1 of extractedColumn into currentTop
-- cycle through our list
set the lockScreen to true
repeat with rowNumber = 1 to the number of lines in extractedColumn
-- identify 1st, 2nd, 3rd, 4th rank- stop when we reach 34th rank
if item 1 of line rowNumber of extractedColumn < currentTop then
put item 1 of line rowNumber of extractedColumn into currentTop
put currentRank + 1 into currentRank
-- once we have coloured the top three ranks, we can stop
if currentRank > rankLevels then
exit repeat
end if
end if
-- using the line numbers of the top three ranks, we can colour them
put (item 2 of line rowNumber of extractedColumn) into lineToColor
if currentRank = 1 then
put "1st" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
set the foregroundcolor of item (chosenColumn +1) of line lineToColor of card field tableFieldName to blue
end if
if currentRank = 2 then
put "2nd" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
set the foregroundcolor of item (chosenColumn +1) of line lineToColor of card field tableFieldName to red
end if
if currentRank = 3 then
put "3rd" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
set the foregroundcolor of item (chosenColumn +1) of line lineToColor of card field tableFieldName to green
end if
if currentRank = 4 then
put "4th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 5 then
put "5th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 6 then
put "6th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 7 then
put "7th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 8 then
put "8th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 9 then
put "9th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 10 then
put "10th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 11 then
put "11th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 12 then
put "12th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
end repeat
end mouseUp
smash wrote:hi sturgis,
i keep getting a message error for line 38 ??
now, i will confess, that your script has me a touch lost (well very lost)![]()
![]()
it will only put up 1st place, and not skip for equal placings.
now i liked that the "placings" where just highlighted, so i added that to this script.
my script is nowwhich works brilliantly, i am just trying to work out if how to skip a placing/placings if there are equal percentages ???on mouseUp
/* Identify the table field */
put "Entries" into tableFieldName
put card field tableFieldName into tableStore
/* Identify the column number you want to colour */
put 4 into chosenColumn
-- extract the desired column, adding the line numbers, into variable
set the itemDelimiter to tab
repeat with rowNumber = 1 to the number of lines in tableStore
put (item chosenColumn of line rowNumber of tableStore)&tab&rowNumber&return after extractedColumn
-- clear previous highlights in column
set the foregroundcolor of item chosenColumn of line rowNumber of card field tableFieldName to empty
put empty into item (chosenColumn + 1) of line rowNumber of card field tableFieldName
end repeat
replace "%" with empty in extractedColumn
-- sort the variable
sort numeric descending extractedColumn
/* Currently, we are colouring the top three percentages */
put 12 into rankLevels
put 1 into currentRank
put item 1 of line 1 of extractedColumn into currentTop
-- cycle through our list
set the lockScreen to true
repeat with rowNumber = 1 to the number of lines in extractedColumn
-- identify 1st, 2nd, 3rd, 4th rank- stop when we reach 34th rank
if item 1 of line rowNumber of extractedColumn < currentTop then
put item 1 of line rowNumber of extractedColumn into currentTop
put currentRank + 1 into currentRank
-- once we have coloured the top three ranks, we can stop
if currentRank > rankLevels then
exit repeat
end if
end if
-- using the line numbers of the top three ranks, we can colour them
put (item 2 of line rowNumber of extractedColumn) into lineToColor
if currentRank = 1 then
put "1st" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
set the foregroundcolor of item (chosenColumn +1) of line lineToColor of card field tableFieldName to blue
end if
if currentRank = 2 then
put "2nd" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
set the foregroundcolor of item (chosenColumn +1) of line lineToColor of card field tableFieldName to red
end if
if currentRank = 3 then
put "3rd" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
set the foregroundcolor of item (chosenColumn +1) of line lineToColor of card field tableFieldName to green
end if
if currentRank = 4 then
put "4th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 5 then
put "5th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 6 then
put "6th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 7 then
put "7th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 8 then
put "8th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 9 then
put "9th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 10 then
put "10th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 11 then
put "11th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
if currentRank = 12 then
put "12th" into item (chosenColumn + 1) of line lineToColor of card field tableFieldName
end if
end repeat
end mouseUp
thank you for ideas sturgis, every little bit helps
cheers
Code: Select all
global theData,theSuffix
on mouseUp
-- fill the table with random data
put empty into field "scoresField"
repeat with i = 1 to 114
put "Name" & i & tab & random(100) into line i of field "scoresField"
end repeat
sortData
tagScores
end mouseUp
-- fill var theData with data from the table plus line numbers
on sortData
put empty into theData
set the itemDelimiter to tab -- sets the delimiter to the default table delimiter
repeat with i = 1 to the number of lines in field "scoresField" -- this iterates through the field
put i & tab & line i of field "scoresField" & cr after theData -- this adds a tab delimited line number to the front of the data line, and cr after to keep things line based
put empty into item 3 line i of field "scoresField" -- While iterating might as well clear out column 3
set the foreColor of line i field "scoresField" to empty -- since we're iterating through the field, might as well set the colors back to empty
end repeat
-- for my table, the scores are in item 2, but item 3 in theData.
-- sort theData variable based on item 3, which contains the scores. Line 1 of theData now contains 1st place.
sort lines of theData descending numeric by item 3 of each
end sortData
-- handler to handle tagging and colorization
on tagScores
set the itemDelimiter to tab -- set the delimiter to match the data we're working with
put "Blue" & tab & "Red" & tab & "Green" into theColors
-- to do it this way I did, you need 2 rank counters one for actual, one for effective.
--Actual will be handled by the repeat loop effective is handled by rankCounter
put 1 into rankCounter
-- adjust the table as desired in this loop
repeat with i = 1 to the number of lines in theData -- loop through the number of lines in theData
-- This if structure mathes 3 possible conditions.
--Is it the first line of theData?
-- If its not the first line of the data, do the current data line and the preceeding data line match = true?
--If its not th efirst line of data, do the current data line and the preceeding data line match = false?
-- this is the "not the first, and not a match" conditional.
--Since there is no match, the counter i indicates the placement position, so we send it to the ordinate handler to set the suffix.
-- We then shove the ordinated index i into the proper item and line number of the table.
-- the table line number in this case is determined by item 1 line i of theData.
--We set the forecolor of the suffix to rankCounter. If i is more than 3 the forecolor is set to empty clearing any prev color
-- since this is a non-matching conditiona, it's necessary to set rankCounter to i so that effective and actual counters match
if i > 1 and item 3 line i of theData < item 3 line i-1 of theData then
ordinate i
put theSuffix into item 3 line (item 1 line i of theData) of field "scoresField"
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item i of theColors -- sets the color
put i into rankCounter -- since this is NOT a duplicate score, set the effective and actual rank index to match
-- this is the "Not the first, and IS a match" conditional.
-- as above, ordinate is used to generate the correct place suffix, but since its a matching rank, we use rankCounter rather than i to pass to ordinate.
-- put the suffix into the correct item and line of the table as above .
-- set the forcolor using rankCounter rather than i so that it is the same as the previous color
-- Don't modify rankCounter so that on next loop if there is another duplicate, rankcounter still contains the correct value.
else if i > 1 and item 3 of line i of theData = item 3 line i-1 of theData then -- this handles duplicates
ordinate rankCounter
put theSuffix into item 3 line (item 1 line i of theData) of field "scoresField" -- uses the effective rather than actual index to set color and rank in the field
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item rankCounter of theColors
-- handles the very first line which will always be first and red
-- don't do anything with rankCounter here, both rankCounter and i are the same at this point.
else if i = 1 then
ordinate i
put theSuffix into item 3 line (item 1 line i of theData) of field "scoresField"
set the foreColor of item 3 of line (item 1 of line i of theData) of field "scoresField" to item i of theColors
end if
end repeat
end tagScoresCode: Select all
global theSuffix
on ordinate pNum
put empty into theSuffix
put "st,nd,rd" into theTags
put the length of pNum into numDigits
repeat with theNumber= 1 to 3
if (char numDigits of pNum = theNumber) and (char numDigits - 1 of pNum <> "1") then
put pNum & item theNumber of theTags into theSuffix
exit repeat
end if
end repeat
if theSuffix is empty then put pNum & "th" into theSuffix
end ordinate