Sorting out letters from numbers in a field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

totallyLost
Posts: 5
Joined: Mon Feb 12, 2024 11:13 am

Sorting out letters from numbers in a field

Post by totallyLost » Mon Apr 07, 2025 2:36 pm

Why can I not use the following script to set the color of letters to black and numbers to red in a particular field?

===
repeat with i= 1 to the number of characters in fld 1
if the isNumber of character i of fld 1 is true then
set textColor of character i of fld 1 to red
else
set textColor of character i of fld 1 to black
end if
end repeat
===

No-clue Newbie

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting out letters from numbers in a field

Post by jmburnod » Mon Apr 07, 2025 3:02 pm

Hi,
I tested your script and it works here.
Sorry no idea why.

best regards

Jean-Marc
Last edited by jmburnod on Mon Apr 07, 2025 5:42 pm, edited 1 time in total.
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Mon Apr 07, 2025 3:11 pm

Hi.

There is no issue with setting the textColor of a character to whatever you want. But a character does not have the ability to hold a custom property. LC does not provide for it.

So you must rethink how you want to do this. Something like:

Code: Select all

put "abcdefghijlklmnopqrstuvwxyz" into letterList
repeat with y = 1 to the number of chars of yourText
   if char y of yourText is an integer then set the textColor of char y of yourText to red
   if char y of yourText is in letterList then set the textColor of char y of yourText to black
 end repeat
Craig
Last edited by dunbarx on Mon Apr 07, 2025 3:26 pm, edited 4 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Mon Apr 07, 2025 3:13 pm

Bernd.

I was unable to either set or access any sort of custom property of a single character residing in a field. I assumed even before I tried it that a character is not any sort of LC control, and could not "hold" a custom property.

Same with "word" or "line" or any sort of direct reference to actual text.

Am I wrong?

Craig

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

Re: Sorting out letters from numbers in a field

Post by Klaus » Mon Apr 07, 2025 3:35 pm

Hi Craig,

ISNUMBER is a valid FUNCTION in LC, although it sounds like the name of a custom property,
we have here a case of rare but nevertheless valid use of the syntax:

Code: Select all

...
answer isnumber(88)
-> TRUE
answer the isnumber of 88
-> TRUE
...
Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Mon Apr 07, 2025 3:56 pm

Klaus.

I know about "isNumber" being a function in LC. But it refers to a value, and is not a property of a character, word or line in a field or variable.

The problem with the OP's original post is that he is trying to ascribe that function to an element of field text as if it was a property of that element of text. That dog don't hunt.

Craig

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

Re: Sorting out letters from numbers in a field

Post by Klaus » Mon Apr 07, 2025 4:33 pm

Craig, you don't get it!

ISNUMBER is a function which returns TRUE or FALSE if the parameter in question is a number or not!

Code: Select all

...
put isnumber("a string") 
## -> FALSE

put isnumber("666")
##  -> TRUE

put isnumber(666) 
## -> TRUE
...
put "This is a string and a number 6" into tString
put isnumber(char -1 of tString)
## -> TRUE
...

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

Re: Sorting out letters from numbers in a field

Post by bn » Mon Apr 07, 2025 6:13 pm

dunbarx wrote:
Mon Apr 07, 2025 3:13 pm
Bernd.
I was unable to either set or access any sort of custom property of a single character residing in a field. I assumed even before I tried it that a character is not any sort of LC control, and could not "hold" a custom property.
Same with "word" or "line" or any sort of direct reference to actual text.
Am I wrong?
Craig
Craig,

You can set the metadata but not custom properties of text elements of fields. Also of lines.

Kind regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Mon Apr 07, 2025 6:18 pm

Klaus.

I do get it. I am not talking about what "isNumber", in and of itself, can or cannot do. I am trying to solve the OP's issue. And that is beyond what the "isNumber" function can do.

Say he has the following in a field 1:
AC,D&4(w)6
and this in a button script:

Code: Select all

on mouseUp
   get fld 1
   repeat with y = 1 to the number of chars of it
      put isNumber(char y of it) & comma after temp
   end repeat
end mouseUp
The result will be:
false,false,false,false,false,true,false,false,false,true,
So my offering early in this thread deals with the actual situation, digits or letters of the alphabet, and that cannot involve "isNumber". A char like "&" will be colored like a "W". Only the OP knows if this is what he wants for such chars.

Craig

EDIT- Klaus or Bernd, if you saw this right after I posted, I corrected some errors I had.
Last edited by dunbarx on Mon Apr 07, 2025 7:04 pm, edited 6 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Mon Apr 07, 2025 6:19 pm

Bernd.

Good point. But my 5-line handler does a decent job all on its own.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Sorting out letters from numbers in a field

Post by jacque » Mon Apr 07, 2025 6:25 pm

Craig, he's using the alternate function syntax, like you can use date() or "the date". The original handler works for me too. But I did get an error when referencing an empty field by accident.

If the field has a lot of text, using htmltext will be much faster. Iterating on shorter text should be fast enough though.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Mon Apr 07, 2025 6:31 pm

Jacque.

I get that. OK.

It is still up to the OP to answer whether he wants to color a char such as "&" in the same way as any other non-digit char. If he does, in other words there are exactly two sets of chars, integers and absolutely everything else, then "isNumber" works fine. I read his first post as not that case, "letters and numbers", but I may have read too little into it. :wink:

Craig

totallyLost
Posts: 5
Joined: Mon Feb 12, 2024 11:13 am

Re: Sorting out letters from numbers in a field

Post by totallyLost » Tue Apr 08, 2025 1:40 pm

Thank you so much for all your input!

Jean-Marc, thanks to you, I found out an extremely stupid mistake of mine and I realized the script I wrote actually works. (For you guys' amusement to know my mistake, I forogt to insert "on mouseup" and "end mouseup"!)

Craig, thanks for letting me know another way; I tried your script and of course it works! (By the way, I don't need to color a char such as "&".)

Thanks to Klaus and Bernd and Jacque for getting me curious to try to understand further! I asked Grok 3 (before I realized my script was ok) and it gave me a script which also works:
on setColors -- Define a handler to call the script
repeat with i = 1 to the number of chars in field 1
if isNumber(char i of field 1) then
set the textColor of char i of field 1 to "255,0,0" -- Red
else
set the textColor of char i of field 1 to "0,0,0" -- Black
end if
end repeat
end setColors
=====
Groks asked me to correct isNumber Syntax, saying,

"Changed the isNumber of character i of fld 1 to isNumber(char i of field 1). The isNumber() function takes a value as an argument and returns true or false."

Which, as a newbie always, is a little difficult for me to get.

But the important things is, thanks to you all, I get it done and I was able to learn more (and I know I was right: asking people is always better than asking AI); thank you, thank you, thank you all!!!

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10090
Joined: Fri Feb 19, 2010 10:17 am

Re: Sorting out letters from numbers in a field

Post by richmond62 » Tue Apr 08, 2025 1:52 pm

While it is undoubtedly useful to determine where something is a number or an alphabetic form, what also might be useful is to determine whether something is written is some language or not:

'Hello, Happy People' might return 'English', or, at least 'Latin' (as in alphabet), while

'Здрасти, хора' might reutrn 'Bulgarian', or, at least 'Cyrillic'.

But I am not sure whether that could be done without leveraging Unicode ranges.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting out letters from numbers in a field

Post by dunbarx » Tue Apr 08, 2025 3:01 pm

Well then, welcome to LC and this forum.
...By the way, I don't need to color a char such as "&".)
That was my point, that using "isNumber" only tells you if the argument to the function is, er, a number. In other words, there are other chars in a body of text besides numbers and the "ABC's. For what I think are your purposes, you are probably safe in thinking there are three character sets, the third set being any character one can type that is not a member of the other two. In that case you need something like my original handler.

Craig

Post Reply