http://www.perlmonks.org?node_id=38466

Zo has asked for the wisdom of the Perl Monks concerning the following question:

To all the Monks here who might remember my past questions, I think I'm getting better... but yet again another question... This is my output file:
data data data data 7,843,391.58 xyz xzy xyz .... 12,224,675.68 abc ~~~ ~~~ .... 1,339,203.94 (more data) 21,407,271.2 (more data) 16,728,929.69 16,728,929.69
The following code is what I used; was taken from the Q and A section (thanks vroom) to insert commas.
sub addcommas{ my ($text)= $nocomma; $text=reverse $text; $text=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; $text=scalar reverse $text; return $text; }
My question/s is/are how can I: - align to the right the final column w/ the dollar amounts? (when printing out.. this is the end of a string print with other data left out for this example) or align the decimals? and - how to stop from dropping the end zero of the cents, where it's missing? This is my print statement how I got my final printout (where $comma is the returned variable caught from the above function):
for ($p = 0; $p < @a_table; $p++){ @a_print = split (/\t/, $a_table[$p]); $nocomma = $a_print[5] / 100; $comma = &addcommas(); print AUDIT "$a_print[0]\t$a_print[1]\t$read_time\t$a_print[3] +\t$comma\n"; } # end for loop
My final output is all correct, just the problems w/alignment and the loss of zero at end of cents... thanks for any/all help.

Replies are listed 'Best First'.
RE: Decimal alignment and loss of zero at print?
by Shendal (Hermit) on Oct 25, 2000 at 22:43 UTC
RE: Decimal alignment and loss of zero at print?
by KM (Priest) on Oct 25, 2000 at 22:43 UTC
    Sounds like a job for format! Take a look at 'perldoc perlform' for examples and use.
    But, first take a peek at printf() or sprintf(). But, formats are good to read about anyways ;)

    Cheers,
    KM

Re: Decimal alignment and loss of zero at print?
by runrig (Abbot) on Oct 25, 2000 at 22:43 UTC
    sprintf the data before adding commas.