Sorting out letters from numbers in a field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 5
- Joined: Mon Feb 12, 2024 11:13 am
Sorting out letters from numbers in a field
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
===
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
Re: Sorting out letters from numbers in a field
Hi,
I tested your script and it works here.
Sorry no idea why.
best regards
Jean-Marc
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
Re: Sorting out letters from numbers in a field
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:
Craig
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
Last edited by dunbarx on Mon Apr 07, 2025 3:26 pm, edited 4 times in total.
Re: Sorting out letters from numbers in a field
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
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
Re: Sorting out letters from numbers in a field
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:
Best
Klaus
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
...
Klaus
Re: Sorting out letters from numbers in a field
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
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
Re: Sorting out letters from numbers in a field
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!
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
...
Re: Sorting out letters from numbers in a field
Craig,dunbarx wrote: ↑Mon Apr 07, 2025 3:13 pmBernd.
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
You can set the metadata but not custom properties of text elements of fields. Also of lines.
Kind regards
Bernd
Re: Sorting out letters from numbers in a field
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:
The result will be:
Craig
EDIT- Klaus or Bernd, if you saw this right after I posted, I corrected some errors I had.
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:
and this in a button script:AC,D&4(w)6
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
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.false,false,false,false,false,true,false,false,false,true,
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.
Re: Sorting out letters from numbers in a field
Bernd.
Good point. But my 5-line handler does a decent job all on its own.
Craig
Good point. But my 5-line handler does a decent job all on its own.
Craig
Re: Sorting out letters from numbers in a field
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.
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
HyperActive Software | http://www.hyperactivesw.com
Re: Sorting out letters from numbers in a field
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.
Craig
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.

Craig
-
- Posts: 5
- Joined: Mon Feb 12, 2024 11:13 am
Re: Sorting out letters from numbers in a field
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!!!
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!!!
-
- Livecode Opensource Backer
- Posts: 10090
- Joined: Fri Feb 19, 2010 10:17 am
Re: Sorting out letters from numbers in a field
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.
'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.
Re: Sorting out letters from numbers in a field
Well then, welcome to LC and this forum.
Craig
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....By the way, I don't need to color a char such as "&".)
Craig