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

Hello

The following code reads the values of variables stored in several separate text files.
open PL, 'pvclength.txt' or die "Cant open file"; pvc_l=<PL>; open GIL, 'gilength.txt'or die "Cant open file"; $gipipe_l=<GIL>; open QGBS, 'qgibendsocket.txt' or die "Cant open file"; $gibend_sck_q=<QGBS>;
When writing these values and other computed values to a report file(.doc) using the code below:
printf EH "\n Item"; printf EH "\n No Descr. Qty Unit Rate Comp. + Party"; printf EH "\n 70 Pvc 4sqmm $pvc_l MTR 35.14 0.00 + $pvc_a"; printf EH "\n 71 Gi Pipe 25mm $gipipe_l MTR 94.93 0.00 + $gipipe_a\n"; printf EH "\n 73 Gi Bend $gibend_sck_q NO 50.00 0.00 + $gibend_sck_a\n"; printf EH "\n &Socket25mm\n";
The expected output is:
Item No Descr. Qty Unit Rate Comp. Party 70 Pvc 4sqmm 2 MTR 35.14 0.00 70.28 71 Gi Pipe 25mm 3 MTR 94.93 0.00 284.79 73 Gi Bend 4 NO 50.00 0.00 200 &Socket25mm
But the output I get is this i.e. everything after the Qty figure appearing on the second line
Item No Descr. Qty Unit Rate Comp. Party 70 Pvc 4sqmm 2 MTR 35.14 0.00 70.28 71 Gi Pipe 25mm 3 MTR 94.93 0.00 284.79 73 Gi Bend 4 NO 50.00 0.00 200 &Socket25mm
How to get around this problem? I tried removing whitespace from the Qty variables for example $pvc_l but that did not work. Any advice would be appreciated. Thanks.

Update

The .txt files contain just a single value for example "pvclength.txt" just contains the value 2.

Replies are listed 'Best First'.
Re: File write problem
by dHarry (Abbot) on Jul 08, 2010 at 13:29 UTC

    Maybe your variables don't contain what you think they do? There is probably a newline in the file you read and you don't chomp. I normally use sprintf when I want to format output. If you have to do it a lot it's probably a better idea to use a module.

      If you have to do it a lot it's probably a better idea to use a module
      I agree with this (and all your other suggestions), and I recommend Text::Table for this task.
Re: File write problem
by kennethk (Abbot) on Jul 08, 2010 at 14:13 UTC
    Based upon your update, I would say dHarry's diagnosis that you have unaccounted for newlines is correct. One way to fix that would be:

    open PL, 'pvclength.txt' or die "Cant open file"; $pvc_l=<PL>; chomp $pvc_l; open GIL, 'gilength.txt'or die "Cant open file"; $gipipe_l=<GIL>; chomp $gipipe_l; open QGBS, 'qgibendsocket.txt' or die "Cant open file"; $gibend_sck_q=<QGBS>; chomp $gibend_sck_q;

    See chomp.

    You also mention in your OP you "tried removing whitespace from the Qty variables". How? What code did you use? This should be straight-forward using regular expressions.

    Please note that because all your variable names are different lengths, your columns will not align - you have a different number of spaces following each one. Again, a simple solution is to follow dHarry's advice and use sprintf.

    Also, note you dropped a sigil off of pvc_l in your OP. Please get in the habit of testing code before you post so Monks don't go off chasing red herrings. See How do I post a question effectively? and make sure posted code passes strict and warnings.

    Update: Fixed oversights in code. Thanks wfsp.

Re: File write problem
by roboticus (Chancellor) on Jul 08, 2010 at 17:08 UTC

    perl_seeker:

    You should read the documentation for printf/sprintf. It's unreasonable for you to expect that it will magically interpolate the variables into their values for the desired width. You need to tell it the width you want. For example:

    printf "%-15.15s % 4d\n", $string, $integer;

    This code tells printf to format the $string variable into a 15 character column, left justifying it (and truncating it if it's too long. It formats $integer into a 4 character field, filling with spaces.

    ...roboticus

    Update: Cleaned up the formatting. (Thanks, moritz!)

      Hello, it was whitespace characters causing everything after the Qty figure to appear on the second line.
      Don't know why the code did not work earlier. Fixed the second line prob using this
      open PL, 'pvclength.txt' or die "Cant open file"; $pvc_l=<PL>; $pvc_l =~ s/^\s+//; $pvc_l =~ s/\s+$//;
      Also for the alignment will look up on sprintf. Thank you everyone for your comments and sorry for the delay in
      replying.

      perl_seeker