How to replace just the first found space with tab for all lines in a variable?

Nonsanity form at nonsanity.com
Wed May 25 10:39:07 EDT 2011


If you want the following to happen:

word[space][space][space]word
becomes -->
word[tab]word

Then:

function StripFirstSpace1 myText
   repeat with a = 1 to the number of lines in myText
      if space is not in line a of myText then next repeat
      put word 1 of line a of myText & tab & word 2 to -1 of line a of
myText into line a of myText
   end repeat
   return myText
end StripFirstSpace1


OR if you want these results:

word[space][space][space]word
becomes -->
 word[tab][space][space]word

Then...

function StripFirstSpace2 myText
   repeat with a = 1 to the number of lines in myText
      if space is not in line a of myText then next repeat
      put tab into char (offset( space, line a of myText )) of line a of
myText
   end repeat
   return myText
end StripFirstSpace2


 ~ Chris Innanen
 ~ Nonsanity


On Wed, May 25, 2011 at 6:11 AM, Keith Clarke <
keith.clarke at clarkeandclarke.co.uk> wrote:

> Hi folks,
> A simple requirement but I'm struggling with getting the structure and
> syntax needed to replace only the first space character in each line of a
> variable with a tab.
>
> I have a field of label-value pairs - and some of the values contain
> spaces. This creates problems if I attempt a blanket approach of 'replace
> space in tVariable' with tab.
>
> So, I tried iterating through each tLine of tVariable but get a runtime
> error with 'replace space 1 in line tline with tab'. So, I'm guessing that
> 'space' can't be used to set chunk expression scope.
>
> So, within each line, I'm iterating at character-level, with 'repeat for
> each character tChar in the line, if tChar is space then...
>
> I've tried 'replace tChar with tab' and 'add tab after tChar' and then
> 'delete tChar' but I get errors - probably because I'm trying to delete the
> reference variable(?)
>
> I'm sure there is a nice efficient way to do this, so any advice on
> structure, syntax or command usage would be greatly appreciated.
> Best,
> Keith..
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list