in reply to Help with decimals

Sorry to bring so much confusion to this problem. Let me explain my comment as to why I wanted to avoid math.

$file = 100+ lines of 12 field comma delimited text and numbers (text,text,num,num,num,...) there are no decimals in these numbers.

$bar = a number from 0 thru 9 that will be given to me by the third field in one 12 field comma delimited line. This number will determine the number of decimal spaces that the other fields in that same line will have.

$foo = numerical fields number 4 thru 8.

I thought I could say something like:
if $bar = x then put a "." x+1 characters in from the right of $foo.
as opposed to saying:

if $bar = 2 then $foo * .01 .. printf...
if $bar = 3 then $foo * .001 printf ...
if $bar = 4 then $foo * .0001 printf...

So this is why I thought that it would be easier not to do math.
I'm really sorry about not explaining myself properly.
Hope this helps.

Replies are listed 'Best First'.
Re: Re: Help with decimals
by HyperZonk (Friar) on Jul 30, 2001 at 08:13 UTC
    First of all, let me say that there must be a better way for you to store your data. However, you might be stuck with the format, for all I know, so on with an attempted solution ...

    Okay, let's say you've split one line into fields using the appropriate module. You store those fields in @currentLine. Then you can do something like:
    foreach (@currentLine[3 .. 11]) { printf ("%.*f", $currentLine[2], $_/(10 ** $currentLine[2]) }
    The * sucks up the next variable in the list and uses it as the field width; in this case, it slurps the first appearance of $currentLine[2] in the list.

    Update: As tilly has noted, the module I mentioned above does not handle newlines embedded within records. Another module, recommended by the same esteemed personage who is on vacation does that, if it is required.

    -HZ
      You're right about the data. The file is given to me by a client. I have no control over the format.