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

I am still trying to format a .txt file provided to me by my professor for an assignment. I've figured most of it out but I'm still having problems doing the following...

-I need to center the Section Headings
-I need to put new sections on new pages (i.e. when a new section starts I need a page break to go to the next page)
-I need to put page numbers on every page

I DON'T WANT SOMEONE TO DO MY HOMEWORK FOR ME!
I just need to be pointed in the right direction.

Here's a sample of the .txt file...

====================================================================== +======= SECTION 35 - BUILDING VIM FROM SOURCE 35.1. How do I build Vim from the sources on a Unix system? For a Unix system, follow these steps to build Vim from the sources: - Download the source and run-time filles archive (vim-##.tar.bz2) fro +m the ftp://ftp.vim.org/pub/vim/unix directory. - Extract the archive using the bzip2 and tar utilities using the com +mand: $ bunzip2 -c <filename> | tar -xf - - Run the 'make' command to configure and build Vim with the default configuration. - Run 'make install' command to installl Vim in the default directory. To enable/disable various Vim features, before running the 'make' comm +and you can run the 'configure' command with different flags to include/ex +clude the various Vim features. To list all the available options for the 'configure' command, use: $ configure -help For more information, read :help install 35.2. How do I install Vim in my home directory or a directory other than the default installation directory in Unix? To install Vim in a directory other than the default installation directory, you have to specify the directory using the --prefix option while running the configure script. $ ./configure --prefix=/users/xyz You can enable/disable various Vim feature by supplying different argu +ments to the configure script. For more information about all these options, + run $ ./configure --help For more information, read :help install-home :help install


Thanks a lot!

Replies are listed 'Best First'.
Re: Formatting Text...Again
by idsfa (Vicar) on May 21, 2006 at 20:33 UTC

    You want to learn about format. Almost of your questions are discussed there.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon

      I agree that format is what seems to be asked for by the assignment. But Perl has pretty much moved away from format and I'm strongly tempted to sabotage the question by suggesting alternate ways of solving the problem. There are many domain specific formatting modules around now that are a lot more appropriate to use.

      lollipop7081 should be aware that completing the assignment using some technique other than format however may result in a lower mark, especially if a lot more code needs to be written to achieve the goal.


      DWIM is Perl's answer to Gödel
Re: Formatting Text...Again
by GrandFather (Saint) on May 21, 2006 at 20:42 UTC

    but where is the sample of your code? I seem to recall seing something like this file before and I know that code was suggested, so you should at least have the centering problem sorted out.

    The new page (form feed) character is \f. So print "\f"; generates a new page on STDOUT.

    Putting page numbers at the top or bottom of each page seems trivial to me. From the answers to your previous question you already know how to center stuff. You do know that print "Page: $pageNum\n" gets the contents of $pageNum interpolated into the string and thus printed don't you?

    Tell us where you are actually having trouble. Show us the code that doesn't do what you want and tell us, or show us with some sample output, what you expect. BTW, don't show us two complete pages of output where there are only 6 interesting lines. Show us the 6 interesting lines with elipsis (...) to indicate omited stuff. That way we can see what you are interested in much more easily. If we can run your sample code and get your sample output then you have asked a question worth a B+. If you show us what you would like and explain where your problem is too you get an A+.


    DWIM is Perl's answer to Gödel
      Ok, here's two pieces of code that i'm trying to use.

      This one is to try to center the headings and print the page numbers
      #!/usr/bin/perl use strict; use warnings; my $file = '/u/home/jhart2/perl/project3Data.txt'; # Name the fil +e open(INFO, $file); # Open the file for input my @Project3 = <INFO>; # Read it into an array my $NoOfCharsPerLine = 80; my $NoOfLinesPerPage = 100; my $RightMargin = 7; my $header = 3; my $footer = 5; my $width = 80; my $heading; print "Page: $pageNum\n"; #while (Project3) { if (/(SECTION)(\s)(\d+)(.*)(\3)(\.1\.)(.*)/) { $heading = "$1.$2.$3.$4"; format Ident = @||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| "$heading" . } }

      This is what it returns:
      [evans:19:55:07 perl]$ ./project3-6.pl Global symbol "$pageNum" requires explicit package name at ./project3- +6.pl line 25. Missing right curly or square bracket at ./project3-6.pl line 40, at e +nd of line syntax error at ./project3-6.pl line 40, at EOF Execution of ./project3-6.pl aborted due to compilation errors.
      I know that I need to define the $pageNum but I don't know what to define it as. And I can't figure out the other errors. I am VERY NEW to perl, and to unix for that matter.

      This is the other code I used to try to center the headings:
      #!/usr/bin/perl use strict; use warnings; my $file = '/u/home/jhart2/perl/project3Data.txt'; # Name the fil +e open(INFO, $file); # Open the file for input my @Project3 = <INFO>; # Read it into an array my $NoOfCharsPerLine = 80; my $NoOfLinesPerPage = 100; my $RightMargin = 7; my $header = 3; my $footer = 5; my $width = 80; my $heading; for (@Project3) { if (/(SECTION)(\s)(\d+)(.*)(\3)(\.1\.)(.*)/) { $heading = $1.$2.$3.$4; print " " x (($width-length($heading))/2); } }


      It doesn't print anyting.

        If you don't understand simple scalar variables then you need to start with reading Perl Babysteps 1: Your First Simple Script. You are no where near ready to be parsing and reformating files! When you have read that turtorial and some of the others in the Getting Started with Perl section of tutorials, come back and we will help further.

        Not understanding fundamental stuff like scalar variables is a severe impediment to completing any Perl script! We can provide a solution script for your assignement, but that doesn't help you at all. What is incredible though is that you have been given such an assignment when you seem to be so ill equiped to complete it. Is this because the course is poorly structured, or is your teacher right out of touch with what is actually going on in class?


        DWIM is Perl's answer to Gödel
        Sorry, I forgot to sign in - the "Anonymous Monk" above is me! Also, there's a slight correction. The second code above does print something, it prints:

        Use of uninitialized value in pattern match (m//) at ./project3-5.pl line 25, <INFO> line 7507.
        It prints this over and over again, at least 100 times before I killed it.
        You shouldn't put your format in a block (the 'if' statement), and you should not have leading whitespace (or anything else) before the actual format string, otherwise it will appear in the output.
        the 'dot'(.) which terminates the format MUST be at the beginning of a line, otherwise it will go to end-of-file looking for it.
        Perl is free-format almost everywhere except in format. ;-)
Re: Formatting Text...Again
by moklevat (Priest) on May 21, 2006 at 22:38 UTC
    Since we don't know what you have tried, I am going to re-recommend Perl6::Form. You can certainly use it to center section headings and create page numbers all day long.

    Here is a node I wrote with some sample code using Perl6::Form to create a formatted table. Maybe this will help.

    Otherwise (as others noted, please post your code, an example of the input text, and an example of the output with an indication of where you think things are going wrong.