In 1993, I wrote some code to parse a C header file with a bunch of numerical token values in #define statements. Then print them out in this series manner, in order to find holes and duplicates. Amazingly, I was able to dig that code up and adapt it for this problem.

Ignore my overuse of printf and the unused flag to turn on hex vs decimal printing. This reflects some of my earliest perl coding, and it's pretty obvious that I'm a C programmer by trade.

#!/usr/bin/perl use strict; use warnings; use diagnostics; use List::Util qw(min); my $opt_x = 0; # Original code allows hex or decimal my @list = (1, 2, 3, 6, 9, 10, 13, 22, 20, 19, 15, 21); my %Count; $Count{$_}++ foreach @list; my $number; foreach $number (sort numerically keys(%Count)) { my $count = $Count{$number}; if ($count > 1) { printf "Token value %d occurs %d times.\n", $number, $count if (!$opt_x); printf "Token value 0x%x occurs %d times.\n", $number, $count if ( $opt_x); } } # Print a list of the form: # 100,102,104,108-110,112-115,119 my $expected = min(@list); my $in_list=0; my $list_start=0; my $comma=""; foreach $number (sort numerically keys(%Count)) { if (($number == $expected) && ($in_list)) { # Accumulate list $in_list++; } elsif (($number == $expected) && ($in_list == 0)) { $list_start=$number; $in_list = 1; } elsif ($in_list > 1) # not expected { printf "%s%d-%d", $comma, $list_start, $expected-1 if (!$opt_x); printf "%s0x%x-0x%x", $comma, $list_start, $expected-1 if ( $opt_x); $comma = ","; $list_start = $number; $in_list = 1; } else # in_list == 1 and not expected { printf "%s%d", $comma, $list_start if (!$opt_x); printf "%s0x%x", $comma, $list_start if ( $opt_x); $comma = ","; $list_start = $number; $in_list = 1; } $expected = $number + 1; } if ($in_list > 1) # Terminate { printf "%s%d-%d", $comma, $list_start, $expected-1 if (!$opt_x); printf "%s0x%x-0x%x", $comma, $list_start, $expected-1 if ( $opt_x); } elsif ($in_list) { printf "%s%d", $comma, $list_start if (!$opt_x); printf "%s0x%x", $comma, $list_start if ( $opt_x); } else { printf "%s%d", $comma, $number if (!$opt_x); printf "%s0x%x", $comma, $number if ( $opt_x); } print "\n"; sub numerically { return $a <=> $b; } exit 0;

In reply to Re: Print series of numbers by gmargo
in thread Print series of numbers by mickep76

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.