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

Can MAP in PERL be use to right justify values in fields of the third column? If yes, how can it be done? If not, is there another way to do it?

Example:
FROM This 
Prod Type 	Price   
1    A 	        12.00   
2    B		126.00  
3    C		1000.00 

To This

                                
Prod	Type	Price   
1	A	  12.00 
2       B        126.00 
3	C	1000.00 
<\pre>   	

Edited 2002-06-10 by mirod: fixed the formating (<pre> tags only around the relevant parts

Replies are listed 'Best First'.
Re: Right Justify data in Fields
by talexb (Chancellor) on Jun 10, 2002 at 15:14 UTC
    You should probably check out printf, and in particular the format specifier %-20s which right justifies a string within a 20 character wide column. (If the string is longer than 20 characters the behaviour varies.)

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: Right Justify data in Fields
by mirod (Canon) on Jun 10, 2002 at 15:16 UTC

    Would using printf solve your problem?:

    #!/usr/bin/perl -w use strict; my @values=( [1, 'A', 12], [2, 'B', 126], [3, 'C', 1000]); foreach my $row (@values) { printf( "%-02d %2s %7.2f\n", @$row); }
Re: Right Justify data in Fields
by Joost (Canon) on Jun 10, 2002 at 15:19 UTC
    Yes, it can - more or less, take a look at perldoc -f sprintf

    my @aligned = map { sprintf "%9.2f",$_ } qw(1 1.5 2.4578 373.237); print join("\n",@aligned);
    Prints:
    1.00 1.50 2.46 373.24
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Right Justify data in Fields
by traveler (Parson) on Jun 10, 2002 at 18:09 UTC
    If you are using formats do something like this:
    format STDOUT_TOP = Prod Type Price . format STDOUT = @<<<<<<<@<<<<<<<<@>>>>>>> $prod, $type, $price .
    Note the '>>>' to force right justification

    HTH, --traveler