brpower has asked for the wisdom of the Perl Monks concerning the following question:

I am looking to right justify ONLY the totalQty field in below script. How do I accomplish this?

open(CUSTORDS, ">\\\\bcedi\\d\$\\dsdata\\rp\\lby\\custords.dat")||die +"Can't open file"; while ( my @row = $sth->fetchrow_array() ) { $lineStatus = $row[0]; $style = $row[1]; $colorCode = $row[2]; $lblCode = $row[3]; $totalQty = $row[4]; $startDate = $row[5]; $orderType = $row[6]; $orderNum = $row[7]; $orderStatus = $row[8]; $confType = $row[9]; $poNum = $row[10]; $customer = $row[11]; $sku = $style . $colorCode . ($space x (32 - length($style . $colorCod +e))); $company = $space x 3; $division = $space x 8; $corp = $space x 12; $sold = $space x 8; $dept = $space x 3; $shipTo = $space x 12; $totalQty = $totalQty . ($space x (8 - length($totalQty))); $poNum = $poNum . ($space x (13 - length($poNum))); $customer = $customer . ($space x (8 - length($customer))); $remarks = $space x 30; $orderStatus = $orderStatus . ($space x (4 - length($orderStatus))); $year = substr($startDate,0,4); $month = substr($startDate,5,2); $day = substr($startDate,8,2); $startDate = $year . $month . $day; $poNum = substr($poNum,0,13); print CUSTORDS "$sku$company$division$corp$sold$dept$shipTo$totalQty$s +pace$poNum$customer$remarks$orderStatus$startDate\n";

Replies are listed 'Best First'.
Re: right justify
by rjt (Curate) on Jul 17, 2013 at 14:29 UTC

    Please put <code> tags around your code, so we can read it. See Markup in the Monastery. I will attempt to make some sense of it anyway.

    I am looking to right justify ONLY the totalQty field in below script.

    print CUSTORDS "$sku$company$division$corp$sold$dept$shipTo$totalQty$space$poNum$customer$remarks$orderStatus$startDate\n"; totalQty = $totalQty . ($space x (8 - length($totalQty)));

    I think you mean you want $totalQty to have the leading spaces in front of its value instead of behind, like " 42" instead of "42 ", right? The least invasive way to do this would simply be to flip the order of the concatenation:

    totalQty = ($space x (8 - length($totalQty))) . $totalQty;

    Now the longer answer...

    How exactly do you want your output to look? Can you give us some sample input? It would be really helpful if your source code actually demonstrated your question in a way we could run. $sth->fetchrow_array isn't much use to those of us that aren't already connected to and familiar with your database. See How do I post a question effectively?.

    $linestatus = $row[0]; $style = row[1]; . .

    This can be greatly shortened with something like:

    my ($lineStatus, $style, $colorCode) = @row;
    $company = $space x 3;

    Those lines seem very odd to me. Are you trying to blank out certain fields?

    Given this, and your question of how to "right justify" the $totalQty field, it seems printf is what you need.

    my $sku = $style . $colorCode; # Remove the $space x ?? padding printf CUSTORDS "%-32s%-3s%-8s%-12s%-8s%-3s%-12s%8s...\n", $sku, '', '', '', '', '', '', $totalQty;

    Notice how all of the string formats have a '-' in front of the length, except for the last one, %8s, which corresponds to the $totalQty. That will cause all of the strings to be left-justified, except for $totalQty, which will be right justified.


    Also, please use warnings; use strict; in your scripts, and maybe use diagnostics; -- these pragmas will flag a lot of errors before they become bugs.

    Edit: Added some <hr/>s to stop my ideas from running together.

      Thanks rjt!!!!!!! your solution is just what I need. sorry for omitting code tags in my post. Thanks again.
Re: right justify
by CountZero (Bishop) on Jul 17, 2013 at 14:33 UTC
    Explore the perl documentation for the sprintf (formatted print into a string) function: perldoc -f sprintf

    You can also use printf to directly print a formatted output but the main documentation is to be found under sprintf.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: right justify
by pachydermic (Beadle) on Jul 17, 2013 at 14:18 UTC

    It's a good idea to put your code between \<code\></code> tags so that we can read your stuff!

    open(CUSTORDS, ">\\\\bcedi\\d\$\\dsdata\\rp\\lby\\custords.dat")||die +"Can't open file"; while ( my @row = $sth->fetchrow_array() ) { $lineStatus = $row[0]; $style = $row1; $colorCode = $row2; $lblCode = $row3; $totalQty = $row4; $startDate = $row5; $orderType = $row6; $orderNum = $row7; $orderStatus = $row8; $confType = $row9; $poNum = $row10; $customer = $row11; $sku = $style . $colorCode . ($space x (32 - length($style . $colorCod +e))); $company = $space x 3; $division = $space x 8; $corp = $space x 12; $sold = $space x 8; $dept = $space x 3; $shipTo = $space x 12; $totalQty = $totalQty . ($space x (8 - length($totalQty))); $poNum = $poNum . ($space x (13 - length($poNum))); $customer = $customer . ($space x (8 - length($customer))); $remarks = $space x 30; $orderStatus = $orderStatus . ($space x (4 - length($orderStatus))); $year = substr($startDate,0,4); $month = substr($startDate,5,2); $day = substr($startDate,8,2); $startDate = $year . $month . $day; $poNum = substr($poNum,0,13); print CUSTORDS "$sku$company$division$corp$sold$dept$shipTo$totalQty$s +pace$poNum$customer$remarks$orderStatus$startDate\n";

      "It's a good idea to put your code between \<code\></code> tags so that we can read your stuff!"

      For clarity code and data go between code tags like this: <code>code and data goes here.</code>.