How to get the line number from a char position ?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

How to get the line number from a char position ?

Post 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.
Discovering LiveCode Community 6.5.2.
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus »

Bonsoir atout66,

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


Best

Klaus
atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

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

Post by atout66 »

Thanks Klaus, this lineoffset() has a nice looking from the dictionary :wink:
I'll give it a try on monday...
Discovering LiveCode Community 6.5.2.
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10502
Joined: Wed May 06, 2009 2:28 pm

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

Post 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
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10103
Joined: Sat Apr 08, 2006 7:05 am
Contact:

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

Post by FourthWorld »

Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

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

Post 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...
Discovering LiveCode Community 6.5.2.
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10502
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx »

Welcome to our world :D

Craig
Post Reply