http://www.perlmonks.org?node_id=664344

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

Hi Folks,
I have a file with over 1000 words in first column and some value in the second column.
I am using the following piece of code:
my %datahash; open(OUTFILE,">","$log"); my $ofh = select(OUTFILE); $~ = "NEWFORMAT"; $^ = "NEWFORMAT_TOP"; $^L = ""; select($ofh); format NEWFORMAT_TOP = -Page Number- @||| $%, +-----------------------------------+------------+ | | | | Data String | Value | +-----------------------------------+------------+ . foreach my $key (keys %datahash) { format NEWFORMAT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @###.## $key, @datahash{$key} . write(OUTFILE); }
In some cases, $key has a long string containing around 100 words in it.
When I use the above code, I see that my $key is "pruned" to the number of '<' I have in format NEW_FORMAT.
I want to print the whole string that $key is equal to. How do I do this?
Thanks in advance for the help!
--Jessica

Replies are listed 'Best First'.
Re: Help with formatting a text file
by artist (Parson) on Jan 25, 2008 at 19:44 UTC
Re: Help with formatting a text file
by Anonymous Monk on Jan 25, 2008 at 20:33 UTC
    take a look at perlform, especially the @* and ^* formats and the "Using Fill Mode" section.

    however, it may be profitable to investigate the text manipulation modules mentioned in a previous response.

Re: Help with formatting a text file
by Narveson (Chaplain) on Jan 25, 2008 at 20:21 UTC
    while (my ($key, $value) = each %datahash) { print {OUTFILE} "$key\t$value\n"; }

    You may not think the output meets your requirements, but don't open it in a plain text editor. There are applications that will open a tab-separated text file and display it in two columns with text wrapping for long cells, just as you desire. You can adjust column widths as you desire while viewing the output, without having to go back and reprogram the output routine.

    One such application (among many) is Microsoft Excel.

Re: Help with formatting a text file
by apl (Monsignor) on Jan 25, 2008 at 20:54 UTC
    Either define the format for $key to be large enough to accomodate the longest possible value, or break up the value of $key into (say) 20 character chunks, and print out one such chunk perline (with a new one-line format just for that purpose).

    Yeah, sounds clumsy to me, too. I'd go with one of the earlier suggestions.

    By the way, if you do continue with formats (I use them), I'd suggest placing them towards the top of your source, or at the very end. Don't put them in the middle, as per your example. It breaks up the flow when reading the source code.
Re: Help with formatting a text file
by noobee (Acolyte) on Jan 25, 2008 at 21:03 UTC
    Hi Folks, Thank you for all the help! Appreciate very much. I think the best approach would be to simply use:
    foreach my $key (keys %datahash) { format NEWFORMAT = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @###.## $key, @datahash{$key} ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~ $key . write(OUTFILE); }
Re: Help with formatting a text file
by moklevat (Priest) on Jan 26, 2008 at 19:18 UTC
    It looks like you got what you were looking for, but in the future, for formatting text into tables you should check out Perl6::Form. Here's a simple (recycled) example that would get you started:
    #!/usr/bin/perl -w use strict; use Perl6::Form; my @barcodes=qw/8129348 2354234 234234534 21342134 23453421/; my @prices = qw/625.50 626.35 234.23 546.54 3245.45/; my @quants = qw/2 2 34 4 6/; my @values = qw/21345 345345 345345 3453 34534/; my $total = 0; foreach(@values){$total+=$_}; print form ' ============================================================', '| Goods in Stock |', '|===========================================================|', '| Barcode | Price | Quantity | Value |', '|-------------+---------------+-------------+---------------|', '| {[[[[[[[[[} | {$]]]]]].[[[} | {]]]]]]]]]} | {$]]]]]]].[[[}|', \@barcodes, \@prices, \@quants, \@values, '|===========================================================|', '| Total Value In Stock {$]]]]]]].[[[}|', $total, ' =========================================================== ', ;

    Produces:

    =========================================================== | Goods in Stock | |===========================================================| | Barcode | Price | Quantity | Value | |-------------+---------------+-------------+---------------| | 8129348 | $625.5 | 2 | $21345.0 | | 2354234 | $626.35 | 2 | $345345.0 | | 234234534 | $234.23 | 34 | $345345.0 | | 21342134 | $546.54 | 4 | $3453.0 | | 23453421 | $3245.45 | 6 | $34534.0 | |===========================================================| | Total Value In Stock $750022.0 | ===========================================================