Page 1 of 1

How to get the line number from a char position ?

Posted: Sat Mar 22, 2014 5:48 pm
by atout66
Hi to all,

After some search, I still don't find the answer of this problem... I use the following code :

Code: Select all

get offset(theString,theTxt)
where the <theString> is some chars like "to test this code" in a text var called <theTxt> with severall lines (many).
The offset() returns me the first char position of <theString> in <theTxt>. OK.

Problem: I don't see from there, how I can get the line number in which belongs <theString> ?

I would like to avoid a <repeat> loop inside each line of <theTxt> if possible (they are many), that's why I try whith offset()...

Any idea how to proceed maybe ?

Thanks in advance.

Re: How to get the line number from a char position ?

Posted: Sat Mar 22, 2014 6:12 pm
by Klaus
Bonsoir atout66,

use LINEoffset instead:
...
get lineoffset(theString,theTxt)
...
:D


Best

Klaus

Re: How to get the line number from a char position ?

Posted: Sat Mar 22, 2014 6:19 pm
by atout66
Thanks Klaus, this lineoffset() has a nice looking from the dictionary :wink:
I'll give it a try on monday...

Re: How to get the line number from a char position ?

Posted: Sun Mar 23, 2014 6:51 am
by dunbarx
Hi.

Klaus is right about the "lineOffset" being the simplest way.

But just in case you have a lot of free time, and insist on using the standard "offset", and want to see how LC might work if you sneak up on it, put several lines of text in a field (field 1). Now put "XYZ" inside one of those lines. Try this in a button script:

Code: Select all

on mouseUp
   answer the number of lines in char 1 to offset("XYZ",fld 1) of fld 1
end mouseUp
I think this is the sort of tapestry that Jacque used to weave. But do try to see how this works. It is old magic. The "lineOffset" is almost cheating.

Craig Newman

Re: How to get the line number from a char position ?

Posted: Sun Mar 23, 2014 7:18 am
by FourthWorld

Re: How to get the line number from a char position ?

Posted: Sun Mar 23, 2014 10:57 am
by atout66
Couldn't wait 'til monday :wink:
Your script, Craig Newman, is just fantastic (like this forum).
I fully love this kind of 'old magic', that's why I didn't try the lineoffset() yet...

Re: How to get the line number from a char position ?

Posted: Sun Mar 23, 2014 4:14 pm
by dunbarx
Welcome to our world :D

Craig