Error in range object expression

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
montymay
Posts: 145
Joined: Thu Jul 18, 2013 5:23 am

Error in range object expression

Post by montymay » Wed Nov 04, 2015 7:17 am

I want my script to insert a text file into a field and then make the lines ending with a colon boldface, as shown below.
Capture.JPG
[img]Capture.JPG[/img]

My script reads:

Code: Select all

on mouseup
   put URL("file:gca/annos/t"&tTitNum&".txt") into tData
   repeat for each line tLine in tData 
      if the last char of the first word of line tLine of tData is ":" then
         set the textstyle of line tLine of tData to "bold"
         end if
   end repeat
   put tData into fld "annos"
end mouseup
It stops on the 5th line, with the message "chunk: error in range start expression."

Thanks for pointing out my error or any suggestions.

Monty

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Error in range object expression

Post by bn » Wed Nov 04, 2015 9:11 am

Hi Monty,

there are two problems with your code.

first you try to set the textStyle of a variable:

Code: Select all

set the textstyle of line tLine of tData to "bold"
this does not work because only the field can have a textStyle. The variable tData is just the text, no styling.

second you use "repeat for each line tLine in tData". While this is faster than "repeat with i = 1 to the number of lines of tData" repeat for each has no idea what the number of the line is that you are looking at, it has the text of a line in variable tLine. So you either use "repeat with i = 1 to x" to know the number of the line you are looking at because "i" is the number or you would have to add a counter to the "repeat for each line tLine in tData" loop

unless your text is very long I would use something like this:

Code: Select all

on mouseUp
   put URL("file:gca/annos/t"&tTitNum&".txt") into tData
   
   put tData into field "annos" -- first put data into field
   
   repeat with i = 1 to the number of lines of tData -- do repeat with instead of for each line to know where you are
      if the last char of the first word of line i of tData is ":" then
         set the textstyle of line i of field "annos" to "bold" -- now change textStyle of field
      end if
   end repeat
   
end mouseUp
There are other ways to set the textStyle of a line but they are more complicated, although probably a bit faster.

If what I am trying to say is not clear please ask.

Kind regards
Bernd

montymay
Posts: 145
Joined: Thu Jul 18, 2013 5:23 am

Re: Error in range object expression

Post by montymay » Thu Nov 05, 2015 1:47 am

Hello Bernd

Thank you for your taking the time to read my script and replying. My text files are not large and it works flawlessly and virtually instantaneously.

I took my script from the following function, which does work:

Code: Select all

function goodNumber gChapNum
  repeat for each char theChar in gChapNum
          if theChar is in ".0123456789" then put theChar after temp
   end repeat
   return temp
end goodNumber
Since the script will not know how many characters are in the variable gChapNum, I don't yet understand why this script works and mine doesn't, but hope to as I continue studying programming.

Monty

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Error in range object expression

Post by bn » Thu Nov 05, 2015 2:26 am

Hi Monty,
function goodNumber gChapNum
repeat for each char theChar in gChapNum
if theChar is in ".0123456789" then put theChar after temp
end repeat
return temp
end goodNumber

Since the script will not know how many characters are in the variable gChapNum, I don't yet understand why this script works and mine doesn't, but hope to as I continue studying programming.
this works because the function does not need to know the postion of the chars, it just filters them and they come in order so if there is a hit then put that char after the variable temp and you have the original order without unwanted extra characters in between.

What often helps is to set a breakpoint in a script and look at what the variables actually contain.

Kind regards
Bernd

Post Reply