How do I write text in aligned columns in Perl?

Have a look at the Perl format command.


Tab-delimited data (where the fields are not consistent in length) does not line up when opened in a text editor.

Solutions:

  • Open the file in a spreadsheet.
  • Open it in a word processor, select all the text, and define appropriate tab stops. (thanks, Justsalt)
  • In your text editor, set your tab width to a value larger than any of your fields.
  • Use spaces instead of tabs (e.g., printf with field widths, or formats).

For example, you might use

printf("%-15s %-15s %-10s %9s\n", $name, $edu, $car, $cash);

The - after the % causes the field to be left justified. Numbers (like money) are usually right-justified (which is the default).

Tags:

Perl

Format