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

This is Just a piece of the code

I'm having trouble printing out my solution. It works fine but its not in the right columns. Would anybody know whats causing this problem. Thanks.

print"\nYear\t\tBalance\t\tInterest\t\tNew Balance\n\n"; $convertInterest = ($annualInterest/100); $year = 2015; while($startAge <= $endAge){ $InterestGained = $startAmount * $convertInterest; $NewBalance = $InterestGained + $startAmount; $Interestrounded = sprintf("%.2f", $InterestGained); $NewBalancerounded = sprintf("%.2f", $NewBalance); $StartAmountRounded = sprintf("%.2f", $startAmount); print"$year"."\t\t"."$StartAmountRounded"."\t\t"."$Interestrounded"."\ +t\t"."$NewBalancerounded"."\n"; $startAmount = $NewBalance + $yearlyDeposit; $year++; $startAge++; };#ends while loop

Replies are listed 'Best First'.
Re: Print output looks wierd
by hdb (Monsignor) on Mar 14, 2015 at 07:41 UTC

    You already use sprintf for formatting. Why don't you develop a full line template for your report like this;

    # header printf "%4s %20s %20s %20s\n\n", "Year", "Balance", "Interest", "New B +alance"; .... # report (in while) printf "%4d %20.2f %20.2f %20.2f\n", $year, $StartAmountRounded,$Inter +estrounded, $NewBalancerounded;

    Of course, the header can be done directly, using printf there is just for demonstration purposes.

    Also, if you would provide a little standalone running script, preferably with use strict; and use warnings; you would get more answers to your problem as it is far easier to run a script and look at its output than to read the code only.

      Thanks for pointing me in the right direction.

        There is more than one way to do it and as such there is not THE right direction either, just A right direction.

Re: Print output looks wierd
by pme (Monsignor) on Mar 14, 2015 at 06:32 UTC
    hi programmercarlito

    '\t' jumps to the next tab. Tab positions are aligned to every 8 columns by default. perlform provides enhanced mechanism to generate reports.