problem with counting words
Peter M. Brigham
pmbrig at gmail.com
Fri Oct 17 12:17:36 EDT 2014
Re extending the itemdelimiter keyword... The handler I posted for getItem() is easily modified to allow the itemdel to consist of a string of characters, so you could do:
getItem("a//b//c//d//e//f",4,"//") -> d
So while we're waiting for the language to be expanded, you could use this:
function getItem tList,tIndex,tDelim
-- returns item # tIndex of tList, given itemdelimiter = tDelim
-- could just "get item tIndex of tList" in the calling handler but
-- then have to set and restore the itemDelimiter, so this is less hassle
-- defaults to tDelim = comma
-- also allows tDelim to be a string of characters !
-- requires getDelimiters()
if tDelim = empty then put comma into tDelim
put getDelimiters(tList) into workingDelim
replace tDelim with workingDelim in tList
set the itemdelimiter to workingDelim
return item tIndex of tList
end getItem
function getDelimiters tText,nbr
-- returns a cr-delimited list of <nbr> characters
-- not found in the variable tText
-- use for delimiters for, eg, parsing text files, loading arrays, etc.
-- usage: put getDelimiters(tText,2) into tDelims
-- put line 1 of tDelims into lineDivider
-- put line 2 of tDelims into itemDivider
-- etc.
if nbr = empty then put 1 into nbr -- default 1 delimiter
put "2,3,4,5,6,7,8,16,17,18,19,20" into baseList
-- could use other non-printing ASCII values
put the number of items of baseList into maxNbr
if nbr > maxNbr then return "Error: max" && maxNbr && "delimiters."
repeat with tCount = 1 to nbr
put true into failed
repeat with i = 1 to the number of items of baseList
put item i of baseList into testNbr
put numtochar(testNbr) into testChar
if testChar is not in tText then
-- found one, store and get next delim
put false into failed
put testChar into line tCount of delimList
exit repeat
end if
end repeat
if failed then
put the number of lines of delimList into nbrFound
if nbr = 1 then
return "Error: cannot get delimiter!"
else if nbrFound = 0 then
return "Error: cannot get any delimiters!"
else
return "Error: can only get" && nbrFound && "delimiters!"
end if
end if
delete item i of baseList
end repeat
return delimList
end getDelimiters
More information about the use-livecode
mailing list