Counting Upper and Lower Case Characters?

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

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
mikesign
Posts: 4
Joined: Fri Jan 07, 2011 1:49 pm

Counting Upper and Lower Case Characters?

Post by mikesign » Wed Jan 19, 2011 2:14 pm

This works on the first field but can't seem to find the upper and lower or space functions?
I want to be able to return just the number of UPPER case characters and LOWER case characters?

on mouseUp --Compute Button
put the number of chars of field "userInput" into field "numCharField"
put the number of UPPERCHARACTERS of field "userInput" into field "upCharField"
put the number of LOWERCHARACTERS of field "userInput" into field "loCharField"
put the number of SPACES of field "userInput" into field "numSpaceField"
end mouseUp

I think i might be able to do this with an Item Delimiter? Any Thoughts?
Thank You Mike
Attachments
charactercounter2.jpg
charactercounter2.jpg (228.53 KiB) Viewed 2380 times

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2581
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Counting Upper and Lower Case Characters?

Post by jmburnod » Wed Jan 19, 2011 2:56 pm

Hi Mike,
Try this

Code: Select all

on UpLowAndSpace --Compute Button
   put fld "userInput" into tText
   put the number of chars of tText into NbChar
   put 0 into tUp
   put 0 into tLow
   put 0 into tspace
   repeat for each char MyChar in tText
      put chartonum(myChar) into MyCTN
      if myCTN = 32 then add 1 to tspace
      if myCTN > 64 and myCTN < 97 then add 1 to tUp --•• from A to Z or what you want
      if myCTN > 96 and myCTN < 123 then add 1 to tlow --•• from a to z or what you want
   end repeat
   put NbChar into field "numCharField"
   put tUp into field "upCharField"
   put tlow into field "loCharField"
   put tspace into field "numSpaceField"
end UpLowAndSpace 
All the best

Jean-Marc

Klaus
Posts: 12285
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Counting Upper and Lower Case Characters?

Post by Klaus » Wed Jan 19, 2011 5:46 pm

Hi Mike,

yep, as Jean-Marc already demonstrated, you need to check the ASCII values of the single characters.


Best

Klaus

mikesign
Posts: 4
Joined: Fri Jan 07, 2011 1:49 pm

Re: Counting Upper and Lower Case Characters?

Post by mikesign » Wed Jan 19, 2011 9:40 pm

THANK YOU!
Worked perfectly! :D

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5147
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Counting Upper and Lower Case Characters?

Post by Mark » Sat Jan 22, 2011 3:06 am

Hi,

Have you considered using matchText?

Code: Select all

put number of chars of replaceText("ThiSisText7withUpperAndLowerCASE","[a-z0-9]","") into myUpperChars
put number of chars of replaceText("ThiSisText7withUpperAndLowerCASE","[A-Z0-9]","") into myLowerChars
put number of chars of replaceText("ThiSisText7withUpperAndLowerCASE","[a-zA-Z]","") into myNumbers
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

kray
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 52
Joined: Sat Apr 08, 2006 5:28 pm
Location: Eau Claire, WI
Contact:

Re: Counting Upper and Lower Case Characters?

Post by kray » Sat Jan 22, 2011 9:17 am

I like where you're going with the regex, but it doesn't take into account punctuation or other non-letter/numbers.
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5147
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Counting Upper and Lower Case Characters?

Post by Mark » Sat Jan 22, 2011 12:19 pm

Hi kray,

OP didn't ask for it (yet). The only thing to add is

Code: Select all

put number of chars of replaceText("ThiSisText7withUpperAndLowerCASE","[ ]","") into myNrOfSpaces //0
or

Code: Select all

set the itemDel to space
put number of items of (myVar & "bla") into myNrOfSpaces
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Talking LiveCode”