Re: numbers in a variable.
Re: numbers in a variable.
- Subject: Re: numbers in a variable.
- From: kai <email@hidden>
- Date: Thu, 31 Jul 2003 20:51:41 +0100
on Thu, 31 Jul 2003 13:58:31 -0400, "Ruby Madraswala" wrote:
>
How do I check if a variable contains numbers (can range from 1 to 99)? I
>
tried:
>
If newword contains [1-99] then
>
Do something
>
Else
>
It's a heading
>
End if
>
>
I don't get any error message and "do something" does not work which right now
is just few display statements.
>
>
Example:
>
Printers
>
1 HP laserjet
>
2 HP inkjet
>
3 Canon color
>
Plotters
>
4 xerox
>
5 oce
>
and so on.
The example's actually quite useful, Ruby - since it suggests that each
'item' line *starts* with a number (rather than *contains* one) - while each
'heading' line would start with an alpha character.
This assumption may be wrong but, if the above is the case, one of the more
efficient methods is to simply check each line to see if it starts with a
numeric character. To demonstrate the principle, the example script below
places each line in either an item list or a heading list:
===========================
set sampleText to "Printers
1 HP laserjet
2 HP inkjet
3 Canon color
Plotters
4 xerox
5 oce"
set {itemList, hdngList, numStr} to {{}, {}, "1234567890"}
repeat with newLine in sampleText's paragraphs
tell newLine's contents to if its character 1 is in numStr then
set itemList's end to it
else
set hdngList's end to it
end if
end repeat
{itemList, hdngList}
===========================
--> {{"1 HP laserjet", "2 HP inkjet", "3 Canon color", "4 xerox", "5 oce"},
{"Printers", "Plotters"}}
If you really *have* to check the entire word at the beginning of each line,
then you might try something like this variation (you'll still need to set
sampleText as above - and rejoin the first 3 lines below to form a single
statement):
===========================
set {itemList, hdngList, numStr} to {{}, {},
"010203040506070809112131415161718192232425262728293343536373839445464748495
5657585966768697787988990"}
repeat with newLine in sampleText's paragraphs
tell newLine's contents to if its word 1 is in numStr then
set itemList's end to it
else
set hdngList's end to it
end if
end repeat
{itemList, hdngList}
===========================
--> {{"1 HP laserjet", "2 HP inkjet", "3 Canon color", "4 xerox", "5 oce"},
{"Printers", "Plotters"}}
---
kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.