On 2014-01-07 20:28, Rajeev Prasad wrote:

so i have this series of numbers:

349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496

there are two ranges in there: 349-396, 425-496

any hints as to how to write a perl code to represent the series of
numbers as a range?

perl -wE'
  my $csv = q{
      1,  2,  4,  5,  6,  8,  9, 10,
    349,350,351,352,353,354,355,356,357,358,
    359,360,361,362,363,364,365,366,367,368,
    369,370,371,372,373,374,375,376,377,378,
    379,380,381,382,383,384,385,386,387,388,
    389,390,391,392,393,394,395,396,425,426,
    427,428,429,430,431,432,433,434,435,436,
    437,438,439,440,441,442,443,444,445,446,
    447,448,449,450,451,452,453,454,455,456,
    457,458,459,460,461,462,463,464,465,466,
    467,468,469,470,471,472,473,474,475,476,
    477,478,479,480,481,482,483,484,485,486,
    487,488,489,490,491,492,493,494,495,496
  };

  (my $ssr = $csv) =~ s/[^\d,]//ga;
  1 while $ssr =~ s/\b(\d+),(\d+)\b/($1==$2-1)?"$1-$2":"$1;$2"/e;
  $ssr =~ s/-\d+(?=-)//g;

  say $ssr;
'
1-2;4-6;8-10;349-396;425-496

(made for unsigned numbers)

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to