Hi --

On 5/2/07, Bil Kleb <Bil.Kleb / nasa.gov> wrote:
> Hi,
>
> I have files full of numbers that I need to twiddle,
> but the format of the numbers cannot change[1], e.g.,
>
>   '0.4577' -> '0.7728'
>
> or
>
> '-2.345e-02' -> ' 1.232e-03'
>
> Using scanf for the output seems to be the solution to
> the second half of the problem, but how does one derive
> the format specifier string of the input fields, which vary?

You could probably just do a gsub, like this:

require 'scanf'

re = /-?\d+\.\d+(e-\d+)?/

a = "'0.4577' -> '0.7728'"
b = "'-2.345e-02' -> ' 1.232e-03'"

as = a.gsub(re, "%f")
bs = a.gsub(re, "%f")

p a.scanf(as)
p b.scanf(bs)

Output:

[0.4577, 0.7728]
[-0.02345, 0.001232]


David

-- 
Upcoming Rails training by Ruby Power and Light:
  Four-day Intro to Intermediate
  May 8-11, 2007
  Edison, NJ
  http://www.rubypal.com/events/05082007