smgfc has asked for the wisdom of the Perl Monks concerning the following question:
here is the codeTitle G: #givens Given 1 Given 2 etc. P: proof statement Pr: #start of proof statement 1 => reason 1 etc.
and any other tips are also very welcome#!/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 }
Title edit by tye as one-word titles complicate simple searches
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing
by grep (Monsignor) on Feb 11, 2002 at 04:31 UTC | |
by grep (Monsignor) on Feb 11, 2002 at 04:56 UTC | |
by smgfc (Monk) on Feb 12, 2002 at 00:02 UTC | |
by smgfc (Monk) on Feb 11, 2002 at 04:40 UTC | |
by smgfc (Monk) on Feb 11, 2002 at 04:50 UTC |