in reply to eliding number ranges
And the output -#!/usr/local/bin/perl -w use strict; while (<DATA>) { chomp; my ($from, $to) = split /\s+/; my $diff = $to - $from; # get the difference ($from, $to) = ($to, $from) if $diff < 0; # error checking if (!$diff) { print "$from\n"; } else { $to = substr($to, length($to) - length($diff)) if length($diff) < length($from) and substr($from, 0, 1) eq substr($to, 0, 1); print "$from-$to\n"; } } __DATA__ 1 32 4 19 28 39 34 123 321 321 324 329 325 349 340 509 51 1
Update: Thanks Abigail-II I have fixed the bug in the code.1-32 4-19 28-39 34-123 321 324-9 325-49 340-509 1-51
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: eliding number ranges
by qq (Hermit) on Nov 14, 2003 at 11:13 UTC | |
|
Re: eliding number ranges
by Abigail-II (Bishop) on Nov 14, 2003 at 11:34 UTC |