Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: atoi equivalents?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99408] Re: atoi equivalents?
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 5 May 2009 05:41:03 -0400 (EDT)
  • References: <gtnk34$3le$1@smc.vnet.net>

Michael wrote:
> What is the most efficient or recommended way to parse numbers in Mathematica?
> 
> I can use ToExpression["1234"] and it will return the number.
> But this won't do, because I could be scraping un-trusted data and the
> above could lead to an injection of code.
> I could also use something like this: ToExpression[StringCases["1234",
> x1 : NumberString ~~ ___ -> x1]]
> but then I have to add error checking and whatnot.
> I suppose I could write a simple MathLink program which implement
> atoi, atol, etc.
> 
> All of these solutions seems like a lot of overhead.  Anybody have a
> better idea?

I don't know whether this does what you need, but ImportString with
format table automatically detects and converts everything that can be
interpreted as a number to a number, everything else is left untouched
as a string. E.g:

ImportString["1234", "Table"]
ImportString["12.e-3", "Table"]
ImportString["Plot[x,{x,0,1}]", "Table"]

if you have access to WebMathematica, you could also use the function
MSPToExpression from the MSP` package which adresses the security
problem in a wider sense and can be configured to let only certain
symbols be converted. Anyway, that approach might well be overkill for
your problem and I don't know whether it will work with versions 6 and 7...

hth,

albert


  • Prev by Date: Re: Control[], Manipulate, Why labels not showing?
  • Next by Date: Re: DynamicModule Wormholes
  • Previous by thread: Re: atoi equivalents?
  • Next by thread: INCLUDE A LEGEND IN A GRAPHIC