Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!rex!samsung!uunet!olivea!tymix!grimoire!mooring From: mooring@grimoire.uucp (Ed Mooring) Newsgroups: comp.lang.perl Subject: Re: Challenge: Generalized automatic numbering Message-ID: <129@tymix.Tymnet.COM> Date: 7 Feb 91 20:16:15 GMT References: <9917@pitt.UUCP> Sender: usenet@tymix.Tymnet.COM Reply-To: mooring@grimoire.tymnet.com (Ed Mooring) Organization: BT Tymnet Bit Bucket Brigade Lines: 64 Nntp-Posting-Host: grimoire In article <9917@pitt.UUCP> al@ee.pitt.edu (A. Martello) writes: >I wrote the man page, who's going write the perl "one-liner"? (just >kidding) Anyway, I am looking for some perl code to perform the >following. Anyone have anything lying around that comes close (or >feel like hacking it together)? > >It is likely that there are ambiguities and errors in the description >below. [self-characterization deleted] >How about a perl script to scan an input file and replace occurrences >of a 'special char' with a number. Simple enough? How about being >able to have "nested" numbers (like you'd use in an outline). > [specification removed for brevity's sake] This isn't quite what you asked for, but close. I needed a crude text formatter for a bunch of people using different machines, with no time to train them to use a real formatter, so I put this together. The style and technique are amateurish because it's the very first perl program I ever wrote, two days after I brought up version 2.0, I promise I'd do better now :-) #!/usr/local/bin/perl # # Quick perl script to replace commands of the form # @L# with the pseudo mil-spec section numbering form. # # Added @LB to begin a list # @LI to insert a list item (#.) # @LE to end a list $ListLevel = -1; while (<>) { if (($Spaces, $Level, $Etc) = /^(\s*)@L(\d+)(.*)/) { die "That's not a level!" unless (length($Level)); $Level -= 1; # make up for 0 starting arrays for ($i = $Level+1; $i <= $#depth; $i++) { @depth[$i]=0; } @depth[$Level] += 1; $#depth = $Level; print $Spaces, join ('.',@depth),$Etc, "\n"; die "Unterminated list" unless ($ListLevel == -1); } elsif (/@LB/) { # start a list die "Nested lists are not allowed" unless ($ListLevel == -1); $ListLevel = 0; } elsif (($spaces, $rest) = /^(\s*)@LI(.*)/) { # list item $ListLevel += 1; die "List item not between @LB and @LE " unless ($ListLevel > 0); print $spaces,$ListLevel,".",$rest,"\n"; } elsif (/@LE/) { #end a list die "List ended without having begun" unless ($ListLevel >= 0); $ListLevel = -1; } else { print; } } exit 0; ---------- Ed Mooring (mooring@grimoire.tymnet.com 408-999-7504)