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

22226Re: Finding gaps in a sequence

Expand Messages
  • flo.gehrke
    Nov 12, 2011
    • 0 Attachment
      --- In ntb-clips@yahoogroups.com, "flo.gehrke" <flo.gehrke@...> wrote:
      >
      > I've got a database where each record is indexed with an alpha-code
      > from 'aaa' to 'zzz'...I want to find out if there is a gap in a
      > sorted list of these codes...


      Thanks for all replies to my question!

      @Rod

      > Treat them as 3-digit radix-26 numbers, and check the numerical
      > difference between two records. If not =1, something's missing.

      I understand that diodeom's solution is based on something like that where the numeric value of each character is determined by ^$StrPos$ and a string from a to z.

      @Axel

      > Get the first triple, say pqf and add it to the first line with
      > a space...

      Wittily, indeed! But we know from experience that this would lead to a lot of cursor movements, insertings etc ending in a very slow performance with a long list.

      @diodeom

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

      Thanks, that's great! The advantage is that it needs no '^$Calc$' functions which are rather slow when being called very often.

      Just for the fun of it, I would like to present my first approach. It's dealing with a pure list of 3-digit-alpha-codes from 'aaa' to 'zzz' without any further stuff in the lines.

      It's quite fast as well since it doesn't move from line to line but assigns the whole list to an array. The result is a list of codes with gaps, i.e. codes that do not immediately follow one another displayed in a second window (avoid empty line at end of list):


      ; Assign whole list to array %Codes%
      ^!SetListDelimiter ^%NL%
      ^!SetArray %Codes%=^$GetText$
      ^!Set %i%=1

      :Loop
      ; Assign first and second code to array %A% and %B%
      ^!Set %A%=^%Codes^%i%%
      ^!Inc %i%
      ^!Set %B%=^%Codes^%i%%
      ; Calculate numeric value of each code
      ^!Set %x%=^$Calc(^$CharToDec(^$StrIndex(^%A%;1)$)$+^$CharToDec(^$StrIndex(^%A%;2)$)$+^$CharToDec(^$StrIndex(^%A%;3)$)$)$
      ^!Set %y%=^$Calc(^$CharToDec(^$StrIndex(^%B%;1)$)$+^$CharToDec(^$StrIndex(^%B%;2)$)$+^$CharToDec(^$StrIndex(^%B%;3)$)$)$
      ; Calculate difference
      ^!Set %Diff%=^$Calc(^%y%-^%x%)$
      ^!If ^%Diff% = 1 Skip Else Next
      ^!If ^%Diff%=-24 Next Else Error
      ^!If ^%i%=^%Codes0% Out
      ^!Goto Loop

      :Error
      ; Assign wrong sequence to variable %Gaps%
      ^!Set %Gaps%=^%Gaps%^%A%/^%B%^P
      ^!If ^%i%=^%Codes0% Out
      ^!Goto Loop

      :Out
      ^!IfEmpty ^%Gaps% Next Else Skip_2
      ^!Info No gaps!
      ^!Goto End
      ^!Toolbar New Document
      ^!InsertText There's a gap at:^P^%Gaps%
      ^!Toolbar Second Window
      ^!ClearVariables

      Regards,
      Flo
    • Show all 29 messages in this topic