Loading ...
Sorry, an error occurred while loading the content.

22224Re: Finding gaps in a sequence

Expand Messages
  • diodeom
    Nov 12, 2011
      Flo wrote:
      > I've got a database where each record is indexed with an alpha-code from 'aaa' to 'zzz'.
      > Every now and then, I want to find out if there is a gap in a sorted list of these codes. There's a gap, for example, in...
      > zbx
      > zby
      > zbz
      > zca
      > zcc
      > zcd

      The following basic take looks for what the next index in sequence should be and flags a gap if no match is found:

      ;Assure blank line at the end of the list
      ^!Replace "\R*\Z" >> "\r\n" WRS
      ;26-character set to get ordinals from
      ^!Set %az%=abcdefghijklmnopqrstuvwxyz
      ^!Jump 1

      ^!Set %1st%=^$StrIndex("^$GetLine$";1)$
      ^!Set %2nd%=^$StrIndex("^$GetLine$";2)$
      ^!Set %3rd%=^$StrIndex("^$GetLine$";3)$
      ^!Set %i1%=^$StrPos("^%1st%";"^%az%";0)$
      ^!Set %i2%=^$StrPos("^%2nd%";"^%az%";0)$
      ^!Set %i3%=^$StrPos("^%3rd%";"^%az%";0)$

      ;Establish what the next index *should be*
      ^!Inc %i3%
      ^!Set %3rd%=^$StrIndex("^%az%";^%i3%)$
      ^!IfEmpty ^%3rd% Next Else Roll_call
      ^!Set %i3%=1; %3rd%=a
      ^!Inc %i2%
      ^!Set %2nd%=^$StrIndex("^%az%";^%i2%)$
      ^!IfEmpty ^%2nd% Next Else Roll_call
      ^!Set %i2%=1; %2nd%=a
      ^!Inc %i1%
      ^!Set %1st%=^$StrIndex("^%az%";^%i1%)$
      ^!IfEmpty ^%1st% End Else Roll_call

      ;Check if given index is present where expected
      ^!Jump +1
      ^!IfEmpty "^$GetLine$" End
      ^!If "^%1st%^%2nd%^%3rd%" = "^$GetLine$" Next_Index
      ;Report if absent
      ^!Jump Line_Start
      ^!InsertText ****** (gap)^%nl%
      ^!Goto Current_index

      Alternatively, maybe it would not be too crazy to generate all possible (17,576) combinations, append to them entries from the target list (array) where they match, and then rapidly replace all blocks of solo (unmatched) lines with "gap" statements.
    • Show all 29 messages in this topic