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

Hi guys, Like the title says I am a newbie.
and I am in need of some helpy here.
I have my script saving to a text file, and I would like to be able to read through the txt file and print the result in a two coluomn table.

Can someone kindly assist me here.

Thanks in advance.

Edit kudra, 2001-09-06 Changed title from 'Newbie'

  • Comment on Reading a text file and formatted printing

Replies are listed 'Best First'.
Re: Newbie
by jlongino (Parson) on Sep 06, 2001 at 09:04 UTC
    I'm sure many people will be willing to help but you should post a sample of the text file, the desired output file and whatever code you have so far.
    @a=split??,'just lose the ego and get involved!';
    for(split??,'afqtw{|~'){print $a[ord($_)-97]}
Re: Reading a text file and formatted printing
by wardk (Deacon) on Sep 06, 2001 at 18:47 UTC
    [id://Tas00],

    you will need to of course to open() the file and read through it, here is open file and read through it 101...

    open FH, "</path/to/myfilename" or die "cannot open myfilename $!\n"; while (<FH>) { # process each line which is stored in this example in $_ print $_; }

    Since you want to print in columns, I suspect you will profit from learing about split() and printf() (or sprintf() )

    have fun!

Re: Newbie
by dondelelcaro (Monk) on Sep 06, 2001 at 09:56 UTC
    Beyond posting some example code as jlongino recommends so we can help you out, you also might want to check out perldoc perlform to get information on outputing the result after you've parsed it into two columns.
Re (tilly) 1: Reading a text file and formatted printing
by tilly (Archbishop) on Sep 08, 2001 at 05:21 UTC
    As other people have said, the answer will strongly depend on what you are trying to do. For instance if you are going to a printer, then I would look into using a document layout language like TeX. A web page would be hard to do (at a guess, you can just write a 2 column table and guestimate a good place to break it). But if you wanted text, well that looked like a fun way to practice with Text::Autoformat, so here is a simple script to do it:
    #! /usr/bin/perl use strict; use Text::Autoformat; use Getopt::Std; use vars qw($opt_h $opt_n $opt_p $opt_w $opt_j $opt_s $opt_c %fmt); getopts("hn:j:sc:p:w:"); if ($opt_h) { exec('perldoc', $0); } # Process arguments. $fmt{justify} = $opt_j || "full"; $fmt{case} = $opt_c if $opt_c; $fmt{squeeze} = $opt_s || 0; $opt_n ||= 2; $opt_p ||= $=; $opt_w ||= 80; # Calculate layout information $fmt{left} = 0; $fmt{right} = int(($opt_w + 3) / $opt_n) - 5; $fmt{all} = 1; my $col_width = $fmt{right}; my $fmt_str = (join "|", map " %s ", 1..$opt_n)."\n"; # Read and process my $text = join '', <>; my @lines = split /\n/, autoformat($text, \%fmt); while (@lines) { my $col_len = int((@lines + $opt_n - 1) / $opt_n); $col_len = $opt_p - 2 if $col_len > $opt_p - 2; my @col; foreach (1..$opt_n) { push @col, [splice(@lines, 0, $col_len)]; } foreach my $i (0..$#{$col[0]}) { my @row = map $_->[$i], @col; $_ .= " " x ($col_width - length($_)) foreach @row; printf($fmt_str, @row); } print "\n" x ($opt_p - 2 - $col_len); print "\n\n"; } __END__ =head1 NAME reformat.pl - reformat text into columns =head1 SYNOPSIS B<reformat.pl> -[hs] [-c {lower,upper,sentence,title,highlight}] [-j {left,right,full,centre}] [-n cols] [-p page-length] [-w page-width] <file> =head1 DESCRIPTION This script takes input and prints it out as formatted ASCII text in multi-column format. The majority of the formatting is done by Damian Conway's excellent I<Text::Autoformat> module. If no input file is specified on the command line it will default to reading from standard input. The options that are supported are: =over 4 =item h Print this help page. =item c argument Automatically adjust the case of text. =item j argument Change the justifications within each column. Defaults to using the full width. =item n argument Choose how many columns to display the text formatted in. Defaults to 2. =item p argument Choose how many lines there are in a page. Defaults to 60. =item s Choose to squeeze multiple spaces into one. =item w argument Choose how many characters wide a page is. Defaults to 80. =back =head1 AUTHOR AND COPYRIGHT Written by Ben Tilly. This may be copied and redistributed under the same terms as Perl.