Hi,
I am creating a program that makes use of the charIndex function. I looked in the dictionary and was surprised to find that the charIndex function only applies to fields. This means I cannot say: answer the charIndex of the last word of "LiveCode Forums", but I can create a field called "text" with "LiveCode Forums" as its contents, and say: answer the charIndex of the last word of field "text". Is there a reason that charIndex cannot be used with strings and variables? (This would make my program quicker as currently it copies text from a variable into a field just to use the charIndex, and does this with 1000+ pieces of text.)
Thanks,
Andrew
Why is charIndex only for fields?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- VIP Livecode Opensource Backer
- Posts: 184
- Joined: Wed Apr 10, 2013 5:09 pm
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Why is charIndex only for fields?
I can't think of a reason why it wouldn't be possible to implement charIndex to work on variables, but their may be a good reason. You might consider submitting a request for that, and you'll either get an implementation or an answer as to why it can't be done (hopefully the former).
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Why is charIndex only for fields?
Edit.
Removed and corrected my less useful answer (Craig worked this out).
See correct answer below.
Removed and corrected my less useful answer (Craig worked this out).
See correct answer below.
Last edited by [-hh] on Sun Oct 19, 2014 4:16 am, edited 1 time in total.
shiftLock happens
Re: Why is charIndex only for fields?
The "offset" function works differently than the "charIndex" property. Both the charIndex and the offset can take any valid chunk expression, but there is an absolute length reference with the charIndex, whereas there is only a chunk evaluation with the offset. They do entirely different things in that regard. Perhaps because the "charIndex" is a property, and not a function, is why it only pertains to fields.
Put "aaa bbb ccc ddd aaa" into fld 1. Put this into a button:
Craig Newman
Put "aaa bbb ccc ddd aaa" into fld 1. Put this into a button:
Code: Select all
on mouseUp
get fld 1
answer offset( last word of it,it)
answer the charIndex of the last word of fld 1
end mouseUp
Re: Why is charIndex only for fields?
Craig.
You are right, I was wrong, thanks for correcting.
But it's still an "offset" (the docs also use this word) in a string, namely
charIndex (chunk) = 1+the length of the string that is before the chunk.
For example the charIndex of last word of S = 1 + number of chars before last word of S.
Accordingly lineIndex (chunk) = the number of lines of the chars until char 1 of the chunk.
No need to have charIndex/lineIndex for fields only, it's a comfortable syntax.
Here is a correct version of my (now deleted) post above:
Hi Andrew,
one big difference:
== If you wish to have a charIndex of the htmlText you read from file, you could in most cases use the much faster LENGTH function, that is, put the url into a variable and use the LENGTH function for that variable 1+(length of the string until the chunk that is to 'index').
== If you wish to have the charIndex for the (according to html formatted) text of the field there is no other way, because LC "translates" the html for you only for field contents. Then you could speed up with the following, because most of the time is needed for the display of the field and sending all the messages after changing its content.
For example, if you put the text of field "hidden" into S,
then you could get 1+length(word 1 to -2 of S) instead of the charIndex of last word of fld "hidden",
or use 1 + length (S) - length(last word of S).
NOTE: LC 7.0 introduces a lot of new offset functions to handle unicode, but charIndex isn't adapted to unicode until now. A good chance to have it for variables with this?
And, attaching to FourthWorld's request, an enhancement request for a htmlToText function for variables (what is already available in the engine for fields), would be a *very* good thing, especially as HTML 5 is coming ...
Hermann (with the big help of Craig).
You are right, I was wrong, thanks for correcting.
But it's still an "offset" (the docs also use this word) in a string, namely
charIndex (chunk) = 1+the length of the string that is before the chunk.
For example the charIndex of last word of S = 1 + number of chars before last word of S.
Accordingly lineIndex (chunk) = the number of lines of the chars until char 1 of the chunk.
No need to have charIndex/lineIndex for fields only, it's a comfortable syntax.
Here is a correct version of my (now deleted) post above:
Hi Andrew,
one big difference:
== If you wish to have a charIndex of the htmlText you read from file, you could in most cases use the much faster LENGTH function, that is, put the url into a variable and use the LENGTH function for that variable 1+(length of the string until the chunk that is to 'index').
== If you wish to have the charIndex for the (according to html formatted) text of the field there is no other way, because LC "translates" the html for you only for field contents. Then you could speed up with the following, because most of the time is needed for the display of the field and sending all the messages after changing its content.
Code: Select all
lock screen; lock messages
set htmltext of fld "hidden" to url("your file")
wait 10 milliseconds with messages -- LC may need some time to translate HTML to text
-- do you charIndex jobs with the text in the field
-- or, if possible, put the text of the field into a variable
-- here and do (much faster) with the LENGTH function your job.
# put the text of fld "hidden" into S
unlock screen; unlock messages
then you could get 1+length(word 1 to -2 of S) instead of the charIndex of last word of fld "hidden",
or use 1 + length (S) - length(last word of S).
NOTE: LC 7.0 introduces a lot of new offset functions to handle unicode, but charIndex isn't adapted to unicode until now. A good chance to have it for variables with this?
And, attaching to FourthWorld's request, an enhancement request for a htmlToText function for variables (what is already available in the engine for fields), would be a *very* good thing, especially as HTML 5 is coming ...
Hermann (with the big help of Craig).
shiftLock happens