Text
function Clean fs
-- remove leading and trailing returns tabs and spaces
repeat while char 1 of fs is in return&tab&space
delete char 1 of fs
end repeat
repeat while char -1 of fs is in return&tab&space
delete char -1 of fs
end repeat
return fs
end Clean
function Q fs
-- because it is more readable to use "`" in a string literal than to write ""e&"
replace "`" with quote in fs
return fs
end Q
Example of use:
put Q("<span style='color: red;'>")
function Thousands fn
-- fn: a number passed as a character string
-- return the number with apostrophes marking groups of three digits in the integer part
-- and spaces marking groups of three digits in the decimal part
-- i.e. 1234567.89012345 becomes 1'234'567.890 123 45
-- and 123.456 remains 123.456
local ldoto -- offset of the decimal point
local lint -- length of the integer part
local ldec -- length of the decimal part
local ich -- index into character string fn
local ll -- length of fn
local lnrapostrophes -- number of apostrophes to be inserted
local lnrspaces -- number of spaces to be inserted
-- number n of apostrophes for length lint of integer part:
-- lint 0 1 2 3 4 5 6 7 8 9 10 11 12 ...
-- n 0 0 0 0 1 1 1 2 2 2 3 3 ...
-- n = (lint-1) div 3 if lint >0 and 0 if lint = 0
-- number n of spaces for length ldec of decimal part:
-- ldec 0 1 2 3 4 5 6 7 8 9 10 11 12 ...
-- n 0 0 0 0 1 1 1 2 2 2 3 3 ...
-- --> same formula.
put length(fn) into ll
put offset(".",fn) into ldoto
if ldoto=0 then -- it is an integer
put ll+1-4 into ich
put ll into lint
put 0 into ldec
else
put ldoto-4 into ich
put ldoto-1 into lint
put ll-ldoto into ldec
end if
-- do the apostrophes first:
if lint>0 then put (lint-1) div 3 into lnrapostrophes else put 0 into lnrapostrophes
repeat
if ich<=0 then exit repeat
put "'" after char ich of fn
subtract 3 from ich
end repeat
-- now do the spaces in the decimal part:
if ldec>0 then put (ldec-1) div 3 into lnrspaces else put 0 into lnrspaces
if lnrspaces=0 then return fn -- this is the most common case: the number has less than three decimal places if any.
put lnrapostrophes+ldoto+3 into ich
repeat with i=1 to lnrspaces
put space after char ich of fn
add 4 to ich
end repeat
return fn
end Thousands
function UnThousands fn
-- inverse of Thousands
replace "'" with "" in fn; replace " " with "" in fn
return fn
end UnThousands
function Padded fs,fn
-- pads fs with blanks at the end to make its length equal to fn.
repeat with i=1 to fn-length(fs)
put space after fs
end repeat
return fs
end Padded
function Indent fs,fn
-- puts fn spaces before every line of fs.
put "" into lIndent
repeat with i=1 to fn
put space after lIndent
end repeat
repeat with i=1 to the number of lines of fs
put lIndent before line i of fs
end repeat
return fs
end Indent
on MultiSortFields -- fieldname1, other fieldnames
-- Sort lines of a set of fields according to one field
--
-- the first field will be sorted by line,
-- the others will be rearranged accordingly.
-- Assumption: all fields have the same number of lines
--
-- If some lines are numbers, the comparison for sorting can be numeric or text.
-- Numbers will "float" to the top and be sorted there numerically.
--
put "" into lFields
repeat with i=1 to the paramcount
put "field ""e¶m(i)"e&"," after lFields
end repeat
delete last char of lCommand
do "put MultipleSorted("&lFields&") into lv"
repeat with i=1 to the paramcount
do "put lv["&i&"] into field ""e¶m(i)"e
end repeat
end MultiSortFields
function MultipleSorted -- parameters
-- Call: in two stages:
-- put multiplesorted(field "A",field "B", field "C") into lv
-- put lv[1] into field "SA"; put lv[2] into field "SB"; put lv[3] into field "SC"
put the paramcount into nParams
repeat with i=1 to nParams
put "" into ArrayOfSorted[i]
end repeat
-- do a simple insertion sort:
put 0 into iLinesPlaced; put 0 into lCurrentLine
repeat for each line lLineToBeInserted in param(1)
put false into lLinePlaced; add 1 to lCurrentLine
-- go find where in the corresponding sorted result to place it:
put 0 into lCurrentPlacedLine
repeat for each line lLineFromPlaced in ArrayOfSorted[1]
add 1 to lCurrentPlacedLine
if lLineToBeInserted < lLineFromPlaced then -- found a place to put this line
put lLineToBeInserted&return before line lCurrentPlacedLine of ArrayOfSorted[1]
repeat with iOtherText = 2 to nParams -- the lines of the other fields
put (line lCurrentLine of param(iOtherText))&return before line lCurrentPlacedLine of ArrayOfSorted[iOtherText]
end repeat
put true into lLinePlaced
exit repeat
end if
end repeat
if not lLinePlaced then -- put it at the end
put lLineToBeInserted&return after ArrayOfSorted[1]
repeat with iOtherText=2 to nParams
put (line lCurrentLine of param(iOtherText))&return after ArrayOfSorted[iOtherText]
end repeat
end if
add 1 to iLinesPlaced; -- put "{"&ArrayOfSorted[1]&"}"
end repeat
return ArrayOfSorted
end MultipleSorted