ashsysad
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.
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 !
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
$
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 !
part of the problem is the use of the \t tabs.
This works correctly for me.
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;
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
Operating System : Enterprise Linux Enterprise Linux Server Current System time : Fri Aug 31 06:35:38 PDT 2012
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, let me try this and get back to you tomorrow. I'm just packing up from my office.
Have a great weekend !
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.
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Fair enough - I agree with what you are saying. I just misunderstood your suggestion.
ASKER
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.
@lwadwell, Thanks for your help too.
ASKER
Thankyou so much !!!!
Here is a quick example.
Open in new window