Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2012, 08:29 PM   PM User | #1
begood321
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
begood321 is an unknown quantity at this point
Unhappy printf left alignment string format issue

Hi Guys,

I have an issue here. My code in printf is not formatting string left alignment for last column properly. See attachment for output.

Code:
printf MYFILE ("%-8s %13.0f %-38s\n", $original_line, $result, $user_folders);
$original_line prints 1st 3 columns as 1 string.

Command printf prints last column close to previous column left justified (but with inadequate spacing). I think this is the format I need. However, changing number in printf for last column to get better alignment under folder access heading doesn't increase spacing until column jumps to next line abruptly.

Thanks for any help you can provide.
Attached Files
File Type: txt printf issue.txt (548 Bytes, 54 views)
begood321 is offline   Reply With Quote
Old 12-18-2012, 12:45 AM   PM User | #2
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Quote:
$original_line prints 1st 3 columns as 1 string.
That's the first obvious mistake, but if you specify the correct format length, which is the combined total of those 3 fields, it should be ok. It appears that the correct length would be between 60 and 68 depending on the desired spacing prior to the next field. You've specified a field length of 8, which is clearly not even close to what it should be.

After a couple tests, this might be closer to what you need.
Code:
printf MYFILE ("%-60s %13.0f %30s\n", $original_line, $result, $user_folders);
FishMonger is offline   Reply With Quote
Old 12-18-2012, 02:04 PM   PM User | #3
begood321
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
begood321 is an unknown quantity at this point
Thanks Fish for your response.

However, although you modify numbers I still get similar result when I change -38 to 38, tested before I opened this new thread. Last column values are aligned under last column header, but right aligned.

Your code for last column values gives right alignment as oppose to left alignment (correct format that I need) to make report more appropriate.

Based on printf formatting it should give left alignment for -30, but isn't. Any ideas why and what is solution?
begood321 is offline   Reply With Quote
Old 12-18-2012, 02:56 PM   PM User | #4
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
If you put some delimiters around each field's format specification, you'll be able to see more clearly the leading and trailing spaces between the fields. Based on that info, you should be able to see what adjustments you need to make.

See if this adjustment makes it more clear.
Code:
printf ("<%-8s> <%13.0f> <%-38s>\n", $original_line, $result, $user_folders);

You haven't clearly stated how the columns should line up, but Here's my test script which I think gives the output you desire.
Code:
#!/usr/bin/perl

use strict;
use warnings;

print <<'HEADER';
----------------------------------------------------------------------------------------------------------
domain\username    full name                 last access date       last access (days)     folder access
----------------------------------------------------------------------------------------------------------
HEADER

my $original_line = 'test\JOHN123       John Campbell             Fri 10-19-2012';
my $result = 59;
my $user_folders = 'EDITOR';

printf ("%-67s %-22.0f %-14s\n", $original_line, $result, $user_folders);
Outputs:
Code:
----------------------------------------------------------------------------------------------------------
domain\username    full name                 last access date       last access (days)     folder access
----------------------------------------------------------------------------------------------------------
test\JOHN123       John Campbell             Fri 10-19-2012         59                     EDITOR
FishMonger is offline   Reply With Quote
Old 12-18-2012, 03:53 PM   PM User | #5
begood321
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
begood321 is an unknown quantity at this point
Thanks Fish, I do get desired result but now 59 is left aligned not right aligned (correct format).

I would prefer output to be right aligned and not as seen below

1
59
123
begood321 is offline   Reply With Quote
Old 12-18-2012, 04:40 PM   PM User | #6
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
You could reduce the size of that field and then add a "spacer" field to push over the last field by that same amount.
FishMonger is offline   Reply With Quote
Old 12-19-2012, 08:13 PM   PM User | #7
begood321
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
begood321 is an unknown quantity at this point
Thanks Fish, your suggestion helped. Here is final solution below:

Code:
			my $column1 = 0;
			my $column3 = "";
			# right align numbers
			if ($result > 0 && $result < 10) {
				$column1 = 75;
				$column3 = " ";
			} elsif ($result > 9 && $result < 100) {
				$column1 = 74;
				$column3 = "  ";
			} elsif ($result > 99 && $result < 1000) {
				$column1 = 73;
				$column3 = "   ";
			}
			#  print final report
			for (my $i=0; $i<=$#user_folders; $i++) {
				if ($file[$j] =~ m|\Q$user_folders[$i]\E|g) {
					if ($i==0) {
						printf MYFILE ("%-".$column1."s %-17.0f %-0s %-12s\n", 
						$original_line, $result, $column3, $user_folders[$i]);
					} 
			}

Last edited by begood321; 12-19-2012 at 08:23 PM..
begood321 is offline   Reply With Quote
Old 12-19-2012, 09:06 PM   PM User | #8
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Glad I was able to help, but to be honest, that seems to be a very kludgy approach.
FishMonger is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:29 PM.


Advertisement
Log in to turn off these ads.