Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!decwrl!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Simple perl question Message-ID: <1990Aug20.223101.28912@iwarp.intel.com> Date: 20 Aug 90 22:31:01 GMT References: <24941@boulder.Colorado.EDU> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 63 In-Reply-To: jorgy@boulder.Colorado.EDU (Eric R. Jorgensen) 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 | | I would be very appreciative if someone could help me, or at least | point me in the direction of a similar example. Well, here's a lukewarm stab at it. It worked the first time on your data, but I didn't try it on anything else. And there are probably ways to optimize the heck out of it. (But I'm too busy working on "that other thing that I'm not supposed to mention because it keeps sounding more and more like a commercial". :-) ################################################## snip here #!/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,$opage) = ($tag,$page); next; } if ($opage =~ /^(.*,)?(\d+)$/) { ($early,$recent) = ($1,$2); if ($recent == $page - 1) { $opage = "$early$recent-$page"; } else { $opage = "$early$recent,$page"; } } elsif ($opage =~ /^(.*-)(\d+)$/) { ($early,$recent) = ($1,$2); if ($recent == $page - 1) { $opage = "$early$page"; } else { $opage = "$early$recent,$page"; } } } print "$otag\t$opage\n" if length($otag); ################################################## snip here 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!"=/