Ok, so I was doing this geometery take home test and neatness counts, and my handwriting isnt bad, but I decided to do the proofs on the computer and have perl spit out some nicely formatted text. The problem is I can't think of an intelligent way of dealing with the header/imfomation (Title, Given, Proof statments). Right now it just spits that out, and even the way I did that seems ugly, so suggestions would be amazing. The format of the file to be parsed is:
Title G: #givens Given 1 Given 2 etc. P: proof statement Pr: #start of proof statement 1 => reason 1 etc.
here is the code
#!/usr/bin/perl -w use strict; my ($file, $tr, @s, @r, $len, $lent, $pad, $i, $line); $file="untitled:Desktop Folder:test2"; open (HAN, "<$file") or die ("Can't open $file: $!\n"); $tr=0; #I used $tr so I could skip the if statement in the while loop +W ##### # Start a loop to go through the file to be parsed ##### W: while (<HAN>) { chomp; if ($_ ne "Pr:" and $tr == 0) { #Just print out the Givens and the + Prove statement, "Pr:" appears before the actual proof print $_ . "\n"; next W; } elsif ($tr == 0) { print "\n\n"; $tr=1; next W; } m/(.+) => (.+)/; #format is statement => reason push @s, $1; #constuct an array of statements push @r, $2; #constuct an array of reasons } close (HAN); $len=0; $lent=0; ##### # Find the longest statement and reason for formatting ##### foreach (@s) { if ( $len < length() ) { $len = length(); } } foreach (@r) { if ( $lent < length() ) { $lent = length(); } } $pad=length($#s+1); #This is so the numbers along the side are lined u +p correctly print "_" x ($len+$pad+4) . "|" . "_" x ($lent+2) . "\n"; #Print the t +op ie _____|______ for ($i=0; $i<$#s+1; $i++) { $r[$i] =~ s/#-(\d+?)/($i+1)-$1/ge; #replace #-? with the line # yo +u are on minus ? print $i+1 . "." . " " x ($pad - length($i+1)+1) . $s[$i] . " " x +($len - length($s[$i])) . " | " . $r[$i] . "\n"; #print the stateme +nts and resons }
and any other tips are also very welcome

Title edit by tye as one-word titles complicate simple searches


In reply to Parsing tips by smgfc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.