in reply to Re: Formatting Text...Again
in thread Formatting Text...Again

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.

Replies are listed 'Best First'.
Re^3: Formatting Text...Again
by GrandFather (Saint) on May 22, 2006 at 02:41 UTC

    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
      A little of both I presume - it's allowed to be taken by anyone who wishes with instructor permission, but the instructor gives permission to everyone. He misrepresented the class and by the time I realized I was over my head it was to late to drop the class. (also, the class costs almost $3000 so taking an incomplete would be devastating to my finances). I realize that there's no way to complete the assignment in the time he's given me (a week and a half, it's due on the 23rd), but I want to do what I can and try to get a fairly decent grade in the class if possible.

        Ouch! I wondered if it might be something like that. But $3000!

        Ok, we'll help as much as we can, but you have some work to do! Get reading those tutorials. Come back with any questions after you've done the reading.


        DWIM is Perl's answer to Gödel
        "my professor assigned perl just to see how good we'd do (with only basic instruction on perl)." (from your first post on the topic)

        Are you sure he expects people to do well? It seems if he gave only basic instruction on Perl, he gave an interesting set of requirements. . . I'm just guessing he may intend something through this assignment other than seeing you write a professional program. Do you know he's going to give you an actual grade on it?


        -----------------
        s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;
Re^3: Formatting Text...Again
by lollipop7081 (Novice) on May 22, 2006 at 02:24 UTC
    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.
Re^3: Formatting Text...Again
by cdarke (Prior) on May 22, 2006 at 11:56 UTC
    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. ;-)