> On Fri, Feb 19, 2010 at 10:34 PM, Bryan R Harris
> <bryan_r_har...@raytheon.com> wrote:
>> 
>> 
>>> 
>>>> Is there any way to keep perl's eval from interpreting numbers starting
>>>> with
>>>> "0" as octal?
>>> 
>>> Stringify them ?
>>> 2 * '012' is 24.
>> 
>> Manually?
>> 
>> We could have thousands of them.  How do I stringify them when they may
>> potentially be in the middle of an expression?  eg.  75+32*(15+052/3)
>> 
>> - Bryan
>> 
> 
> Step back a second.
> 
> You said the data is zero-padded to make your columns line up, but
> there are no columns in your input.

Ah, the ever-present problem with asking questions of a list.  My problem
and situation is far more complicated than what I've described, but tried to
simplify it to maximize the chances of getting a good answer quickly (which
I think I have, I know that perl doesn't allow you to shut it off, and I
have a workaround, which was pretty much all I hoped for).


> That means there is some intermediate step where the the columnar data
> is being compiled into the expressions you want to evaluate.

That is true, however that step is being left to the users right now, and
many of which aren't quite skilled enough yet to do the strip-leading-zeroes
step (and some may forget they need to do it, e.g. the leading zeroes don't
appear in the list of data until you get down to line 12000).

My biggest concern with this problem is that you get answers that are
roughly in the right order of magnitude, but which are *wrong*.  That's a
huge problem for me.


> The simplest method--if you have access to it--would be to strip the
> leading zeros from the initial input, rather than trying to parse the
> expression prior to evaluating it.
> 
> Assuming the first time you see the data is when you eval it, though,
> something like the following should come pretty close to meeting your
> needs:
> 
>  perl -e 'print "> "; while(<>) {chomp; s/(\D)(0\d*)/$1 eq "." ?
> $1."$2" : $1.(0+$2)/ge; print "$_\n"; print(( eval $_ ), "\n> ")}'
> 
> That is, search the string for every instance of a non-digit, followed
> by zero, followed by zero or more digits. If the non-digit is a ".",
> return the non-digit and the string of digits as a string. If the the
> non-digit is something else, return the 0 + the non-digit, interpreted
> as base 10.
> 
> That won't be as robust as constructing your own parser as suggested
> above, but as long as your input is composed of numbers and operators
> as you expect, it should be a start.

Thanks, your point is well taken.

- Bryan




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to