Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Simple perl question Message-ID: <1990Aug20.234233.5896@iwarp.intel.com> Date: 20 Aug 90 23:42:33 GMT References: <24941@boulder.Colorado.EDU> <1990Aug20.223101.28912@iwarp.intel.com> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 52 In-Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) In article <1990Aug20.223101.28912@iwarp.intel.com>, merlyn@iwarp (Randal Schwartz) writes: | In article <24941@boulder.Colorado.EDU>, jorgy@boulder (Eric R. Jorgensen) writes: | | I have input that looks like: | | x 1 | | x 5 | | x 6 | | x 7 | | y 25 | | y 26 | | And I would like output like: | | x 1,5-7 | | y 25-26 | | Well, here's a lukewarm stab at it. [my lukewarm stab deleted.] Naah. I like this one better. Same general idea, but using a s/foo/bar/e to do the hard work. ================================================== snip #!/local/usr/bin/perl while (<>) { chop; ($tag,$page) = split; (warn "[skipping '$_']"), next if (length($tag) < 1) || ($page !~ /^\d+$/); if ($tag ne $otag) { print "$otag\t$opage\n" if length($otag); $otag = $tag; $opage = $page; next; } $opage =~ s#([-,])?(\d+)$# ($2 + 1 != $page) ? "$1$2,$page" : ($1 eq "-") ? "-$page" : "$1$2-$page" #e; } print "$otag\t$opage\n" if length($otag); ================================================== snip Gotta stop hacking this now. Although, creating this program couldn't have come at a better time (I presume you are making an index for a book... a task at which I am becoming more acquainted with as each day passes :-). 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: "Welcome to Portland, Oregon, home of the California Raisins!"=/