First 1000 characters without loop?

Richard Gaskin ambassador at fourthworld.com
Thu Jun 22 17:18:02 EDT 2017


Rick Harrison wrote:

 >> On Jun 22, 2017, at 2:17 PM, Mark Talluto wrote:
 >>
 >> On Jun 22, 2017, at 11:03 AM, Rick Harrison wrote:
 >>>
 >>> I have a string variable which contains over 2500 characters.
 >>> I only want to grab the first 1000 characters of that string.
 >>> Rather than looping 1000 times to grab each character
 >>> is there a way to just grab the first 1000 efficiently in
 >>> one big chunk?
 >>
 >> Hi Rick,
 >>
 >> put char 1 to 1000 of tOriginalVar into tNewVar
 >
 > Thanks for this solution.
 >
 > Does that statement create an implied loop?
 > It’s great for a one liner though!


With many chunk expressions, I would imagine it does.  With line chunks, 
for example, the engine needs to walk through the string, comparing each 
character to CR, counting the found CRs as it goes.

But even then, better to have the engine do that in machine code than 
for us to do it in script. :)

In this case, though, I believe it doesn't need a loop per se, since 
AFAIK character are fixed-size entities internally (Mark Waddingham, is 
that true that UTF-16 gives us two-bytes per char across the board?).

If I'm mistaken there, any traversal of the string is still about as 
efficient as it's going to get in a general-purpose language, since it's 
relying on the well-optimized Unicode libraries many projects depend on.

All that said, as much as I enjoy benchmarking I wouldn't sweat 
use-cases involving small data.  1k chars could be sliced with any chunk 
type so quickly it probably won't matter.

-- 
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  Ambassador at FourthWorld.com                http://www.FourthWorld.com




More information about the use-livecode mailing list