From: Robert Klemme Date: 2007-05-02T20:40:09+09:00 Subject: Re: Inverse scanf: finding format specifers of existing fields On 02.05.2007 12:47, Bil Kleb wrote: > 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? If there is a fixed number of formats you can probably use a cascade of RX matches. Otherwise it probably becomes a bit more complex like matching sequences of digits and measuring their lengths. >> md = %r{^(\d+)\.(\d+)?$}.match('0.4577') => # >> pa="%#{md[0].size}.#{md[2].size}f" => "%6.4f" >> pa % 0.4577111 => "0.4577" HTH robert