in reply to Console Output Formatting Help Required

You could use printf (for a good tutorial, see Using (s)printf()), as in:

#! /usr/bin/perl -w use strict; use warnings; printf("%-60s", 'this is the first message...'); print STDOUT "done\n"; printf("%-60s", 'this is the second message of different length...'); print STDOUT "done\n";

The disadvantage is that it is a fixed space distance, and rolls around when the terminal window is too narrow...

Replies are listed 'Best First'.
Re^2: Console Output Formatting Help Required
by tarunmudgal4u (Sexton) on Sep 27, 2013 at 07:46 UTC

    thanks for the help. But, it solves the problem partially. When the size of first field exceeds from 60, then alignment changes.

    I'm looking for something that, even if first field size exceeds, second field alignment should be maintained and first field text should be extended to next line.

      Hi, I've written two methods printR and printL to write text on the right side and left side respectively on the screen. It's working fine.

      sub printL { my $str = shift; my $widthL = 60; #my $widthR = 80; my @strWrap = (); for (1 .. int (length $str)/$widthL ) { push @strWrap, substr($str, 0, $widthL-1, '')."\n"; } #push @strWrap, $str; print "@strWrap"; printf ("%-80s", "$str"); } sub printR { if (@_ == 1){ my $status = shift; print "$status\n"; } elsif (@_ == 2){ my $status = shift; my $msg = shift; print "$status\n$msg\n"; } }

      Now there is one problem. In my code I've used multiple printL methods consecutively. So first printL writes at the left side of the screen and leaves the cursor at that place only and next consecutive printL continues from there. that's not what I want.

      I have a solution for this. I can find the current cursor coordinates and if x coordinate is greater than 0, next printL will add newline char at the beginning of the string. But, how to find current cursor position, I don't know. Can somebody help me with this?

      Perl tell function doesn't help me with this as it returns the position in bytes. Any suggestion would also fine.

        Its simple, keep track of what you printed :)