Link to home
Start Free TrialLog in
Avatar of ashsysad
ashsysadFlag for United States of America

asked on

Using 'print' statement with fixed width

Hello,

I'm not that comfortable in using 'printf' in Perl. Hence am seeking your help to produce a formatted output using printf statement. To explain my problem, I made a tiny perl script using 'printf'and shown a sample execution of it.


$ cat check.pl
#!/usr/bin/perl

print "Enter the Operating system name : ";
chomp ($OsIssue=<STDIN>);
chomp ($Current_Data = `date`);

system("clear");
printf "\nOperating System                 : %-30s", "$OsIssue";
printf "\t\t\tCurrent System time                : %-30s\n\n", "$Current_Data";


$ perl check.pl
Enter the Operating system name : RedHat Linux 5.8

Operating System                 : RedHat Linux 5.8                                     Current System time              : Fri Aug 31 06:35:28 PDT 2012

$ perl check.pl
Enter the Operating system name : Enterprise Linux Enterprise Linux Server

Operating System                 : Enterprise Linux Enterprise Linux Server                     Current System time              : Fri Aug 31 06:35:38 PDT 2012

$

Open in new window



In the above 2 sample execution, you can notice that if the number of characters of string variable "$OsIssue" is more, the output text "Current System time" is drifting towards right. I don't want that to happen. I want the second print statement should print its text in a fixed width (regardless of number of characters of string "$OsIssue".  Hope I made myself clear. Please let me know how to use the printf or sprintf in a correct way for this type of requirement. Thankyou !
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

As far as I am aware ... perl will overflow (widen) the print for longer values than the format normally permits.  A way around would be substr() the values being printed.
Here is a quick example.
my $x = 1;
for (my $i=1; $i < 12; $i++) {
	$x = $x * 11;
	printf "STR: %-8s  NUM: %6d <<END\n", "."x$i, $x;
}

$x = 1;
for (my $i=1; $i < 12; $i++) {
	$x = $x * 11;
	printf "STR: %-8s  NUM: %6d <<END\n", substr("."x$i,0,8), substr($x,0,6);
}

Open in new window

part of the problem is the use of the \t tabs.

This works correctly for me.
#!/usr/bin/perl

print "Enter the Operating system name : ";
chomp ($OsIssue=<STDIN>);
chomp ($Current_Data = `date`);

system("clear");
printf "%-25s : %-30s\n", 'Operating System', $OsIssue;
printf "%-25s : %-30s\n", 'Current System time', $Current_Data;

Open in new window

Avatar of ashsysad

ASKER

@FishMonger,  I still couldn't avoid using TAB (\t).  My requirement is to execute both the printf statement in Same line. So the first printf statement shouldn't have new-line character (\n). I'm wondering is there a way to do Right justification.

Operating System                 : Enterprise Linux Enterprise Linux Server                     Current System time              : Fri Aug 31 06:35:38 PDT 2012
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, let me try this and get back to you tomorrow. I'm just packing up from my office.

Have a great weekend !
>> and make sure you specify a length equal to or grater than the longest length of the given field
That was the problem I believe ("the number of characters of string variable "$OsIssue" is more") ... in the original example it was %-30s but the input string "Enterprise Linux Enterprise Linux Server" is 40 chars ... hence it was pushing to the right.

I believe the asker is after a method to stop the pushing to the right when the length is less than the given field.

I suggested use of a substr() on the value to constrain it to the maximum length in the format.  A similar result would occur using Perl's format method e.g.
my $s;
my $y = 1;
format test_line =
STR: @<<<<<<<  NUM: @>>>>> <<END
     $s,            $y
.
for (my $i=1; $i < 12; $i++) {
	$y = $y * 11;
	$s = "."x$i;
	$~ = 'test_line';
	write;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fair enough - I agree with what you are saying.  I just misunderstood your suggestion.
Hello FishMonger,  Your solution really helped me to fix my long-time problem with print statement alignment.  I implemented your solution on the script which am developing and got the expected output.

@lwadwell, Thanks for your help too.
Thankyou so much !!!!