MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: LetterQ and DigitQ question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36113] Re: LetterQ and DigitQ question
  • From: hartmut.wolf at t-systems.com (Hartmut Wolf)
  • Date: Thu, 22 Aug 2002 04:32:50 -0400 (EDT)
  • References: <aj5a1s$a73$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"DrBob" <majort at cox-internet.com> wrote in message news:<aj5a1s$a73$1 at smc.vnet.net>...
> expr = "{1,2}";
> And @@ (DigitQ /@ (ToString /@ (ToExpression@expr)))
> 
> True
> 
> That works if all the strings you'll apply it to are in that list
> format.  If not, then something like this works...
> 
> expr1 = "{1,2,"; 
> expr2 = "{1,2,m"; 
> f[expr_] := (Intersection[#1, Characters["01234567890,{}"]] == 
>      Union[#1] & )[Characters[expr]]
> f[expr1]
> f[expr2]
> 
> True
> 
> False
> 
> Bobby Treat
> 
> -----Original Message-----
> From: Charles H. Dresser [mailto:dresserc at gouldacademy.org] 
To: mathgroup at smc.vnet.net
> Subject: [mg36113]  LetterQ and DigitQ question
> 
> Hello,
> I have a problem with DigitQ and letterQ I would like for a string to
> yeild True when run with DigitQ if the the string looks like this:
> {1,2} 
> and false if it looks like this:
> {hello, there!!!}
> Right now both strings return False because of "{ , }". So, if there
> were anyway to include the charecters "{", "}", and "," into the
> DigitQ "0-9" list temperarelly that would work, but I am sure there is
> a better way to do that.   Thank You,
> Charles


Charles,

here quite a different method, with possible advantages (and anyway
interesting, I think): it will sort out any non numerical entries in
your string of any complexity.

In[1]:= strng = "{123 * 765, Sqrt[2 \[Pi]], cake}"

In[2]:=
expr = MakeExpression[strng]
Level[expr, {-1}, Hold]
NumericQ /@ Level[expr, {-1}]

Out[3]=
HoldComplete[{123*765, Sqrt[2*Pi], cake}]

Out[4]=
Hold[123, 765, 2, \[Pi], cake]

Out[5]=
{True, True, True, True, False}

You look at all bottom objects of the expression made from your
string; cake is recognized as being not numeric, whereas the symbol Pi
is accepted. One advantage of this is that you may introduce the
numbers in any format (e.g. hexadecimal).

Now, if "cake" in your string happens to be a symbol already defined
with a numerical value, it will pass the test.

In[6]:=
cake = I;
NumericQ /@ Level[expr, {-1}]

Out[7]=
{True, True, True, True, True}

That may be desirable or not. 
If not

In[8]:=
xxs = ToString /@ Level[expr, {-1}, 
    Function[Null, {HoldForm /@ Unevaluated[##]}, {HoldAll}]]
Out[8]=
{"123", "765", "2", "Pi", "cake"}

xxs gives you the string representation of all objects at bottom (the
Holds, Unevaluated, and the specially constructed function are just
made to recover these unevaluated. What we have done so far is just
using the Mathematica parser, receiving all tokens at bottom level
(and ignoring the Heads).

The next line looks if any of these tokens is a symbol defined in the
global context, such you also recognize an inadvertent use of a non
numeric string, even if that corresponds to a defined symbol with a
numeric value.

In[9]:= FreeQ[Names["Global`*"], #] & /@ xxs
Out[9]=
{True, True, True, True, False}

Of course, again, you may ask if the tokens are all digits

In[10]:= DigitQ /@ xxs
Out[10]=
{True, True, True, False, False}

Pi of course isn't

--
Hartmut Wolf


  • Prev by Date: Re: function mySet[]
  • Next by Date: RE: rectangle intersection
  • Previous by thread: Re: LetterQ and DigitQ question
  • Next by thread: Out of memory