Differentiating between a number and a letter

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
montymay
Posts: 145
Joined: Thu Jul 18, 2013 5:23 am

Differentiating between a number and a letter

Post by montymay » Thu Oct 15, 2015 8:36 pm

Can LC tell whether a character is a number or a letter? I have a locked field with list behavior set to true that lists the chapters of a title in a legal code, e.g., "Chapter 6 - Retirement Fund." However, the Legislature named a few chapters like this, "Chapter 23A - Government Claims." The latter naming convention (number plus letter) "spoils" the following script:

Code: Select all

      put the selectedtext into tSelection
      put the second word of tSelection into tChapterNumber      
     if tChapterNumber < 10, then
        [statements]
        else
       [statements]
       end if
LC will not recognize "23A" as a number greater than "9", so the the statements after "else" will not execute. If all number+letter "words" in the chapter names were the same length, I know how to extract the number, but they could be 2, 3, or 4 characters in length, e.g., "9A", "25A", or "100A."

If the number absolutely must be extracted from a variable-length chunk, I am guessing that there is a custom function that could do it, but I can't find any discussion in this forum, the User Guide, etc. Is a custom function the answer? Thank you for any suggestions.

Monty May

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

Re: Differentiating between a number and a letter

Post by dunbarx » Thu Oct 15, 2015 8:51 pm

Hi.

Simple function. But before that, why are you validating the chapter at all? In other words, if it can be either a number or a string, what is the advantage? Now if there is something special about the first nine chapters (whether they are "6" or "6A' or not, since you check for values <9) then OK. But if not, then why do it?

Or if you know much more about this than I do:

Code: Select all

function goodNumber tText
   repeat for each char theChar in tText
      if theChar is in ".0123456789" then put theChar after temp
   end repeat
   return temp
end goodNumber
Craig Newman

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

Re: Differentiating between a number and a letter

Post by snm » Thu Oct 15, 2015 9:05 pm

So "21.32.787.22" is the number :)

Marek

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

Re: Differentiating between a number and a letter

Post by dunbarx » Thu Oct 15, 2015 9:10 pm

Marek.
So "21.32.787.22" is the number
The function would return that string. Is that a bad thing? Do you mean to say that the string is not a valid number? The function does not check that, only removing all but periods and digits. The chapters given as an example might actually have that format (Chapter 23.3.1).

But if not, modifying the function to allow only one period, or similarly, checking to see if the final filtered string is a number, is simple enough.

SNM, is any of this important to you?

Craig

montymay
Posts: 145
Joined: Thu Jul 18, 2013 5:23 am

Re: Differentiating between a number and a letter

Post by montymay » Thu Oct 15, 2015 11:40 pm

Hello Dunbarx,

Thank you for your quick reply. To answer your question, the purpose of my app is to download a selected chapter from a website that I don't control into a revBrowser on the same card as the field. Since I can't change the url, my script has to capture it exactly. The URLs on the website are written in the following pattern:

. . .
http://www.guamcourts.org/CompilerofLaw ... 7gc009.pdf
http://www.guamcourts.org/CompilerofLaw ... gc009A.pdf
. . .
http://www.guamcourts.org/CompilerofLaw ... gc042A.pdf
. . . and so forth

The "composite numbers" are the exception to the rule, so my script is written:

Code: Select all

                              if tChapterNumber > 9 then
               revBrowserSet gBrowserID, "url", tURL&"0"&tTitleNumber&"gca/"&tTitleNumber&"gc0"&tChapterNumber&".pdf"
            else
               revBrowserSet gBrowserID, "url", tURL&"0"&tTitleNumber&"gca/"&tTitleNumber&"gc00"&tChapterNumber&".pdf"
            end if
As you can see, the "gc00" changes to "gc0" if the chapter number is greater than 9 and therefore my script must resolve a composite number such as "9A" or "42A." As I am not a trained programmer (self-taught), I see no other way than to somehow get the number inside tChapterNumber. Is there another way to achieve my purpose?

Thanks again.

Monty

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

Re: Differentiating between a number and a letter

Post by dunbarx » Fri Oct 16, 2015 5:00 am

Hmm.

Tell me again, exactly what you need. If you run the "goodNumber" function above, which extract only the numeric part of any string, why can't you incorporate this into your "revBrowserSet" command?

Code: Select all

if goodNumber(tChapterNumber) > 9 then
Craig

Post Reply