Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right justified numbers in Perl

How can I print numbers right justified in Perl, like this:

a=   1
b=  22
c= 333
d=4444  
like image 261
gthm atla Avatar asked Jan 05 '12 07:01

gthm atla


3 Answers

Try like this.

printf ("%4d\n",1);
printf ("%4d\n",11);
printf ("%4d\n",111);
printf ("%4d\n",1111);
like image 65
Pavunkumar Avatar answered Nov 18 '22 03:11

Pavunkumar


The official resource for this is perldoc -f sprintf , which has a nice summary of examples:

For example:

  printf '<% d>',  12;   # prints "< 12>"
  printf '<%+d>',  12;   # prints "<+12>"
  printf '<%6s>',  12;   # prints "<    12>"
  printf '<%-6s>', 12;   # prints "<12    >"
  printf '<%06s>', 12;   # prints "<000012>"
like image 5
Zaid Avatar answered Nov 18 '22 02:11

Zaid


Use printf with a precision and a space as "filler":

printf "a=% 4d\n", 1;
printf "b=% 4d\n", 22;
like image 2
Mat Avatar answered Nov 18 '22 02:11

Mat