How to find the offset of the last instance of a repeating character in a string?
Kay C Lan
lan.kc.macmail at gmail.com
Tue Oct 30 00:54:05 EDT 2018
On Tue, Oct 30, 2018 at 2:33 AM Keith Clarke via use-livecode
<use-livecode at lists.runrev.com> wrote:
>
> I’m trying to separate paths & pages from a list of URLs and so looking to identify the position of the last ‘/‘ character.
>
If that is all you are after then I think setting the itemDelimiter to
"/" and separating the 'item -1' (page) from 'items 1 to -2' (path)
would give you a very simple a readable solution. The only problem is
if you have the unlikely but not impossible situation where you have
paths that contain no pages. Because of the known gotcha with LC and
how it counts items when the last item is empty you may need to
include and 'if' statement.
Try this, create a new Stack with a field and a button.
Into the field load the following text:
https://www.my.org/assets/general/february/
https://www.my.org/assets/general/march/
https://www.my.org/assets/general/april/2018.zip
https://www.my.org/assets/general/may/2018.zip
https://www.my.org/assets/general/june/2018.zip
https://www.my.org/assets/general/july/2018.zip
https://www.my.org/assets/general/july/2017.html
https://www.my.org/assets/general/july/2016.text
https://www.my.org/assets/general/july/2015.jpg
https://www.my.org/assets/general/august/2018.zip
https://www.my.org/assets/general/september/2018.zip
https://www.my.org/assets/general/october/2018.zip
https://www.my.org/assets/general/november/
https://www.my.org/assets/general/december/
Into the button load the following script (be careful of line breaks
there are 16 lines of code):
on mouseUp
put fld 1 into tText
set the itemDelimiter to "/"
repeat for each line tLine in tText
if (char -1 of tLine = "/") then --usual problem with dealing
with empty last items
put empty into tPath[tLine]
else
if (tPath[item 1 to -2 of tLine] = empty) then --initial entry
put item -1 of tLine into tPath[item 1 to -2 of tLine]
else --multiple entries
put tPath[item 1 to -2 of tLine] & cr & item -1 of tLine
into tPath[item 1 to -2 of tLine]
end if
end if
end repeat
breakpoint
end mouseUp
There is breakpoint at the end so the script will pause and you can
inspect the variables. You'll see that an array is created with each
unique path as a key and each page its element. In the case of 'july'
you will see that four pages are all listed, one per line.
>From there it should open a world of possibilities to arrange, sort
and sift through the paths.
HTH
More information about the use-livecode
mailing list