How to find the offset of the last instance of a repeating character in a string?
Devin Asay
devin_asay at byu.edu
Mon Oct 29 11:49:51 EDT 2018
On Oct 29, 2018, at 9:32 AM, Keith Clarke via use-livecode <use-livecode at lists.runrev.com> wrote:
>
> Folks,
> Is there a simple way to find the offset of a character from the ‘right’ end of a string, rather than the beginning - or alternatively get a list of all occurrences?
>
> I’m trying to separate paths & pages from a list of URLs and so looking to identify the position of the last ‘/‘ character.
>
> Thanks & regards,
> Keith
There was a discussion on this topic on the list a few years ago, and I saved these functions in my script library:
From Peter Brigham:
These are utility functions I use constantly for text processing. Offsets(str,cntr) returns a comma-delimited list of all the offsets of str in ctnr. Lineoffsets(str,cntr) does the same with lineoffsets. Then you can interate over the list of offsets to do whatever you want to each instance of str in cntr. I keep them in a utility stack that is in the stackinuse, so it is available to all stacks. I don't use regex, as I have never gotten the regex syntax to stick in my head firmly enough to find it natural, and in any case doing it by script turns out to be as fast or faster.
Peter's lineOffsets function returns a line number for each found char offset. I added a function that returns only unique line numbers.
function offsets str,cntr
-- returns a comma-delimited list of
-- all the offsets of str in cntr
put "" into oList
put 0 into startPoint
repeat
put offset(str,cntr,startPoint) into os
if os = 0 then exit repeat
add os to startPoint
put startPoint & "," after oList
end repeat
if oList = "" then return "0"
return item 1 to -1 of oList
end offsets
function lineOffsetsAll str,cntr
-- returns a comma-delimited list of
-- all the lineoffsets of str in cntr
# (returns a line number for ALL instances)
put offsets(str,cntr) into charList
if charList = "0" then return "0"
put the number of items of charList into nbr
put "" into oList
repeat for each item n in charList
put the number of lines of (char 1 to n of cntr) \
& "," after oList
end repeat
return item 1 to -1 of oList
end lineOffsetsAll
# added by Devin Asay
function lineOffsets pStr,pSearchTxt
# (returns only unique line numbers)
put empty into tList
put 0 into tStartLine
repeat
put lineOffset(pStr,pSearchTxt,tStartLine) into tLineNum
if tLineNum = 0 then exit repeat
add tLineNum to tStartLine
put tStartLine & "," after tList
end repeat
if tList is empty then return "0"
return item 1 to -1 of tList
end lineOffsets
Hope this helps.
Devin
Devin Asay
Director
Office of Digital Humanities
Brigham Young University
More information about the use-livecode
mailing list