Split Unicode-Mixed field into array

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
strongbow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 146
Joined: Mon Jul 31, 2006 1:39 am
Contact:

Split Unicode-Mixed field into array

Post by strongbow » Wed Oct 17, 2012 1:24 pm

Has anyone split old "mixed" fields (i.e. fields containing a mix of 1-byte e.g. English and 2 byte e.g. Japanese chars) into arrays successfully since the fields were changed significantly in Release 5.5?

I have a current project running fine in 4.6.4 and now it breaks under 5.5 with the new unicode (it just works!) field changes. Now I'm not even sure now how to successfully display the text that is in Japanese in the field at all (apart from using the htmlText of the fld).

I have the following text in a field:

en English
jp 日本語
de Deutsch

Previously I could easily split this array and use the japanese text in another btn or field. But now it's really not clear to me how I should go about this. I have read the release notes about unicodeLabel, unicodeText etc etc but still it's not very clear. Or perhaps I'm trying to do too much in the same way as I previously did, rather than changing my approach.

What I'm trying to do is to take the contents of a field (contents above) and then grab item 2 of line 2 (i.e. the word "Japanese" in japanese) and display that in a field or button label.

Attempting using something like the following code:

Code: Select all

put the unicodeText of fld "LangCodes" into tTemp
set the itemDelimiter to tab
put unicode item 2 of line 2 of tTemp into fld "test"
Has anyone sorted this out well since 5.5 came out?

Also, Is it possible to use the split command on a field containing Unicode text (ie. to store it in an array)? Or do we have to do it all manually?

Thanks for help, once again, on Unicode stuff...

cheers!

strongbow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 146
Joined: Mon Jul 31, 2006 1:39 am
Contact:

Re: Split Unicode-Mixed field into array

Post by strongbow » Wed Oct 17, 2012 5:43 pm

I can put the text directly from 1 field to another, but I want to be able to set the UnicodeText of a field from an array or variable.

i.e. the following works:

Code: Select all

set the unicodetext of line 1 of field "test" to the unicodetext of item 2 of line 2 of fld "Langcodes"
but not from a variable as far as I have been able to test so far.

i.e. this does not work:

Code: Select all

put unicode item 2 of line 2 of tTemp into fld "test"
Ideas anyone?

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Split Unicode-Mixed field into array

Post by snm » Wed Oct 17, 2012 9:26 pm

Try uniEncode and uniDecode

Marek

strongbow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 146
Joined: Mon Jul 31, 2006 1:39 am
Contact:

Re: Split Unicode-Mixed field into array

Post by strongbow » Thu Oct 18, 2012 8:49 am

Yup, have tried a bunch of variations using unidecode and uniencode, nothing works for me so far.

To summarise what my problem is:

I would like to store the UnicodeText of a field into a variable (or array) and then somewhere else set the unicodeText of a field to a chunk of that variable.

Is it possible?

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Split Unicode-Mixed field into array

Post by snm » Thu Oct 18, 2012 11:52 am

Make 2 fields on card named: Field1 and Field2
Make 2 buttons: Save and Restore
Put scripts into buttons
Save:

Code: Select all

on mouseUp
   set the cText of this card to uniDecode( unicodeText of field "Field1", "UTF8")
end mouseUp
Restore:

Code: Select all

on mouseUp
   set the unicodeText of field "Field2" to uniEncode (the cText of this card, "UTF8")
end mouseUp
Type something into Field1, click button Save, then click button Restore and in field Field2 should be the same text as in Field1.
Should work even if cText property of the card can be not the same as text in fields (it's utf-8 coded)

Hope it helped,
Marek

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Split Unicode-Mixed field into array

Post by Klaus » Thu Oct 18, 2012 12:03 pm

So much for "Unicode! It just works!" :(

strongbow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 146
Joined: Mon Jul 31, 2006 1:39 am
Contact:

Re: Split Unicode-Mixed field into array

Post by strongbow » Thu Oct 18, 2012 12:49 pm

Hi Marek

Yes, I can confirm that works. And storing it into a variable works similarly:

Code: Select all

put unidecode(the unicodeText of fld "LangCodes","utf8") into tTemp
set the unicodeText of fld "test" to uniencode(item 2 of line 2 of tTemp,"utf8") 
So, as Klaus says... "it, just, works"...

Not exactly marvellous I must say, but relieved to get that cleared up. So we need to use unidecode in order to get the unicode representation of the field into standard single-byte characters, and then uniencode in order to get it to convert to unicode again. And that's because all of our characters internally are single-byte presumably?

I see in Devin's Unicode summary (at: http://revolution.byu.edu/unicode/unicodeinrev.html ) that he mentions one part of this:
How to convert between Unicode (UTF-16) and UTF-8 text.
LiveCode displays non-Roman-alphabet languages using Unicode (UTF-16). You use the uniDecode and uniEncode functions to convert between UTF-16 and UTF-8.

The following statement converts a variable’s contents from UTF-8 to UTF-16, and places the resulting Unicode text in a field:

put uniEncode(myVariable,"UTF8") into field "My Field"
It just needs the extra bit about storing unicodeText from a field to a variable. e.g.

Code: Select all

put unidecode(the unicodeText of fld "My Field","UTF8") into myVariable
Hope someone else finds this useful!

Thanks again Marek!

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Split Unicode-Mixed field into array

Post by snm » Thu Oct 18, 2012 1:04 pm

You are welcome :D
Marek

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Split Unicode-Mixed field into array

Post by Klaus » Thu Oct 18, 2012 1:24 pm

OK, to be honest, this release is not the one with the promised "Unicode! It just works" 8)

Anyway, its too bad RunRev is currently focussing so much on MOBILE (OK, big money here probably)
and neglects the desktop quite a bit...

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Split Unicode-Mixed field into array

Post by snm » Sun Apr 21, 2013 8:59 am

Agree with Klaus. Unicode should have the highest priority, as it's used on any platform. Now there are workarounds to use UTF8 but not always possible. We hope it'll be made in near future, as from this depends a lot.

Marek

Post Reply