Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 Attached Files

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,838
    Thanks
    2
    Thanked 160 Times in 155 Posts
    $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);

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,838
    Thanks
    2
    Thanked 160 Times in 155 Posts
    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

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,838
    Thanks
    2
    Thanked 160 Times in 155 Posts
    You could reduce the size of that field and then add a "spacer" field to push over the last field by that same amount.

  • #7
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,838
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Glad I was able to help, but to be honest, that seems to be a very kludgy approach.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •