Page 1 of 1
Finding A Character (.) In A String - Return Position
Posted: Thu Jun 25, 2020 6:44 am
by Googie85
Hi Guys!
I am trying to locate a "." in a string and return its character position within the string.
Simple I know, but the Dictionary has been of no help. Thanks for any and all replies!
Googie.
Re: Finding A Character (.) In A String - Return Position
Posted: Thu Jun 25, 2020 6:56 am
by mrcoollion
Use Offset. How ? See the following URL :
https://livecode.fandom.com/wiki/Offset
Regards,
Paul
Re: Finding A Character (.) In A String - Return Position
Posted: Thu Jun 25, 2020 7:59 am
by richmond62
Garden spade time:
Code: Select all
on mouseUp
ask "Enter your string here: "
put it into SSTRING
if SSTRING contains "." then
put it into SSTRINGY
put 0 into POZ
repeat until char 1 of SSTRING is "."
add 1 to POZ
delete char 1 of SSTRING
end repeat
put " '.' is char #" & POZ && "of string '" & SSTRINGY & "'" into fld "fOUTPUT"
else
put "Your string does not contain a '.' " into fld "fOUTPUT"
end if
end mouseUp
-
Re: Finding A Character (.) In A String - Return Position
Posted: Thu Jun 25, 2020 5:50 pm
by jacque
Garden spade time:
That's way too much work.
Re: Finding A Character (.) In A String - Return Position
Posted: Thu Jun 25, 2020 5:56 pm
by richmond62
jacque wrote: ↑Thu Jun 25, 2020 5:50 pm
Garden spade time:
That's way too much work.
Yup, so it seems, but I got a bit "
offset" by domestic problems here.
And
"too much work" is all a bit relative as that took me no longer than 10 minutes.

Re: Finding A Character (.) In A String - Return Position
Posted: Thu Jun 25, 2020 6:12 pm
by jacque
Yup, so it seems, but I got a bit "offset" by domestic problems here.
That's okay, the whole world has domestic problems right now.

Re: Finding A Character (.) In A String - Return Position
Posted: Fri Jun 26, 2020 9:20 am
by richmond62
I'm not entirely convinced that this is advantageous in any way:
Code: Select all
on mouseUp
ask "Enter your string here: "
put it into SSTRING
if SSTRING contains "." then
put offset(".", SSTRING) into POZ
put " '.' is char #" & POZ && "of string '" & SSTRING & "'" into fld "fOUTPUT"
else
put "Your string does not contain a '.' " into fld "fOUTPUT"
end if
end mouseUp
-
-
Of course it might not be a bad thing if
a script could locate ALL the occurrences of "." in a paragraph . . .
Re: Finding A Character (.) In A String - Return Position
Posted: Fri Jun 26, 2020 6:01 pm
by jacque
Using offset, you don't need to check whether the text contains the character, so the "if" clause is unnecessary. If the character isn't in the text, offset returns zero. In your example the script needs to scan the text twice, once to check for the character and again to determine its offset.
You can find all occurances of a character by using offset's optional third parameter. This is the equivalent of "send in time" and is very fast because it only needs to go through the text once. It does require a few more lines of script, where a single offset search only needs one line.
Re: Finding A Character (.) In A String - Return Position
Posted: Fri Jun 26, 2020 8:20 pm
by bogs
jacque wrote: ↑Fri Jun 26, 2020 6:01 pm
You can find all occurances of a character by using offset's optional third parameter. This is the equivalent of "send in time" and is very fast because it only needs to go through the text once.
It does require a few more lines of script, where a single offset search only needs one line.
Mind posting an example, Jacque? Offset parameters 1 and 2 I think I have a fair understanding of, but I don't see how that 3rd paramter (relative position) would help me to find all of the occurrences of a character (or string of chars)

Re: Finding A Character (.) In A String - Return Position
Posted: Sat Jun 27, 2020 9:07 am
by richmond62
-
Code: Select all
on mouseUp
put empty into fld "fOUTPUT"
put fld "fINPUT" into SSTRING
put the number of chars in SSTRING into TLIMIT
put 0 into SKIPPY
put 1 into TOCHKA
repeat until (SKIPPY + 1) > (TLIMIT)
put offset(".", SSTRING, SKIPPY) into POZ
put " '.' is char #" & (POZ + SKIPPY) && "of string '" & SSTRING & "'" into line TOCHKA of fld "fOUTPUT"
add POZ to SKIPPY
add 1 to TOCHKA
end repeat
end mouseUp
Re: Finding A Character (.) In A String - Return Position
Posted: Sat Jun 27, 2020 9:34 am
by richmond62
You can find all occurrences of a character by using offset's optional third parameter.
You can, but it is a "
right fudge" doing it:
Code: Select all
put offset(".", SSTRING, SKIPPY) into POZ
put " '.' is char #" & (POZ + SKIPPY) && "of string '" & SSTRING & "'" into line TOCHKA of fld "fOUTPUT"
add POZ to SKIPPY
This is the
inside of a
repeat loop . . .
As you can see the
third optional parameter is a real
so-and-so as it does
NOT refer to a second or third occurrence,
it refers to the number of characters from the start of the string that have to be
excluded during the operation
[It took me 2 glasses of whisky, a hot bath and a completely irrelevant argument with my wife to work that out.]
The '
Documentation' "
leaves a bit to be desired" in this respect.
Re: Finding A Character (.) In A String - Return Position
Posted: Sat Jun 27, 2020 5:09 pm
by jacque
Right, the third parameter is always the number of characters from the start of the string, which does mean you need to add the most current found position to the counter. It's a bit confusing because offset returns the position from the last starting point, which is the end of the last found position.
The MC docs had a reminder to always add the two locations together. The LC docs would benefit from the same.