Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!nuug!ulrik!ulrik!aas From: aas@boeygen.nr.no (Gisle Aas) Newsgroups: comp.lang.postscript Subject: Re: PostScript page reverser? Message-ID: Date: 31 Oct 90 19:15:58 GMT References: Sender: news@ulrik.uio.no (USENET News System) Reply-To: Gisle.Aas@nr.no Organization: Norwegain Computing Centre, Oslo, Norway Lines: 50 In-Reply-To: aas@boeygen.nr.no's message of 31 Oct 90 19:13:54 In article aas@boeygen.nr.no (Gisle Aas) writes: > I once wrote the following perl program. (Not quite a one-line :-) Since then I have learned not to write C for loops in perl. So here you have a rewrite of my psrev program, done the perl way. #!/usr/local/bin/perl # Simple psrev clone in perl. Reverses pages of conforming postscript files. # Author: Gisle Aas, NCC, Oslo. email: @psfile = <>; # Slurp in the whole file die "This is not a conforming PostScript file" unless $psfile[0] =~ /^%!PS-Adobe-(.*)/; $line = $[; # Locate pages for (@psfile) { if (/^%%Page:(.*)(\d+)$/) { push(@pages,$line); push(@labels,$1); } $line++; } die "This file contains no PostScript %%Page comments\n" if $#pages < $[; $trailer = $#psfile; # Locate trailer for (reverse(@psfile)) { last if /^%%Trailer/; $trailer--; } print @psfile[$[..$pages[0] - 1]; # Output prolog $pageno = 1; # Output pages in reverse order $to_line = $trailer - 1; while ($from_line = pop(@pages)) { printf "%%%%Page:%s%d\n", pop(@labels), $pageno++; print @psfile[$from_line + 1 .. $to_line]; $to_line = $from_line - 1; } print @psfile[$trailer .. $#psfile]; # Output trailer __END__ -- Gisle Aas | snail: Boks 114 Blindern, N-0314 Oslo, Norway Norsk Regnesentral | X.400: G=Gisle;S=Aas;O=nr;P=uninett;C=no voice: +47-2-453561 | inet: Gisle.Aas@nr.no