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

Nonsanity form at nonsanity.com
Wed May 25 12:18:30 EDT 2011


Actually, "a,b" would be considered a single word both when using the "word
of" format, as well as if the itemdel is space and you use "item of". In
other words...

word 2 of "a,b cd ef"   =   "cd"
and
set itemdel to space
item 2 of "a,b cd ef"  =  "cd"

The only real difference between the two forms is if there are runs of
spaces between words, like so:

word 2 of "a,b[space][space]cd[space]ef"  =  "cd"
and
set itemdel to space
item 2 of "a,b[space][space]cd[space]ef"  =  empty

The "item of" gives an empty result because it is returning the text between
the first two spaces, and there's nothing there. As far as putting a tab
after the first word, this version is functionally the same as the offset()
method I described before, but I /think/ the offset() method would be
faster.

Making simple conversion functions is very easy in LiveCode if you can make
certain assumptions about the input data. The trick is often just finding
the exceptions in large datasets that will trip up whichever function you
pick.

I once had to convert several months of transactions in a library database
between two formats using, I think, Supercard. It was the weird edge cases
that tended to trip it up. I kept having to add special tests for those
cases as they appeared.

 ~ Chris Innanen
 ~ Nonsanity



On Wed, May 25, 2011 at 11:41 AM, Alex Tweedly <alex at tweedly.net> wrote:

> Beware this will get the (possibly) wrong result for some data. For
> instance, a comma will terminate a word, so
>
> a,b def
>
> will give you a key of "aTABb def"
>
> rather than a key of "a,TABdef"
>
> I'd stick to the safer
>
>
> set the itemDel to space
> repeat for each line L in tData
>   put item 1 of L & TAB & item 2 to -1 of L & CR after tOutput
> end repeat
>
>
> or ... since they are key/value pairs, if (AND ONLY IF) you happen to know
> that the keys are unique
>
> split tData by CR and space
> combine tData by CR and TAB
>
> -- Alex.
>
>



More information about the use-livecode mailing list