Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!bionet!agate!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal L. Schwartz) Newsgroups: comp.lang.perl Subject: Re: Splitting by character count in perl? Message-ID: <1991Mar6.203859.17332@iwarp.intel.com> Date: 6 Mar 91 20:38:59 GMT References: <17806@milton.u.washington.edu> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal L. Schwartz) Distribution: usa Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 23 In-Reply-To: djo7613@milton.u.washington.edu (Dick O'Connor) In article <17806@milton.u.washington.edu>, djo7613@milton (Dick O'Connor) writes: | Our old program copied the first 78 characters on a line to the output file | after prepending and appending the character 'A'. Characters 79-150 were | written to line 2 of the output file with prepended and appended 'B'. A | simple routine glued things back together at the other end. To split'em: perl -pe 'chop; ($a,$b) = unpack("a78a*",$_); $_ = "A${a}A\nB${b}B\n";' To join'em: (presuming alternating lines of A's and B's from above): perl -pe 's/^A(.*)A\n$/$1/ || s/^B(.*)B\n$/$1\n/;' If you can have an A line without a B line, you will need to maintain state between lines. That's an exercise for you. print "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/