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

Re: [Clip] sorting a string of characters

Expand Messages
  • Don - htmlfixit.com
    My first run ... as usual watch for long lines: ;by don at htmlfixit.com ;takes a random string of nine numbers and ;outputs the possible combination of one
    Message 1 of 4 , Dec 20, 2004
      My first run ... as usual watch for long lines:
      ;by don at htmlfixit.com
      ;takes a random string of nine numbers and
      ;outputs the possible combination of one
      ;two and three digit numbers that can be
      ;made from them.

      ;start fresh
      ^!ClearVariables
      ;lets open and work in a whole new document
      ;so as to not mess up what you were doing
      ^!Keyboard CTRL+N
      ;get nine characters (currently any number flies)
      ^!Set %string%=^?[nine numbers in string]
      ;make each digit one line
      ;because line length is set to 1
      ^!Set %string_unordered%="^$StrFormat("^%string%";0;1;0)$"
      ;use StrSort to get the digits in numerically
      ;sorted order (this is the trick I was looking for)
      ;appears it only works with lines as delimiters?
      ^!Set %string_ordered%=^$StrSort("^%string_unordered%";False;True;False)$
      ;I really didn't want them all on a line ...
      ;so dump the returns used in StrFormat above
      ^!Set %string_ordered%=^$StrReplace("^P";"";"^%string_ordered%";0;0)$
      ;I want an array of zeros is there a better way to
      ;create such a thing? I use each array element to
      ;store the number of occurances of a number (but
      ;note my counting is off by one so item1 is 0
      ;item2 is 1 and so forth to item10 which is 9, because
      ;item0 is the number of items in the array
      ^!SetArray %number_count_array%="0";"0";"0";"0";"0";"0";"0";"0";"0";"0"

      ;number_counter for number of times to run this loop
      ;and number_searched that is always one less because
      ;of the one off thing explained above (note: later
      ;I use the calc function instead ... either works.
      ^!Set %number_counter%="1"
      ^!Set %number_searched%="0"
      :number_of_numbers
      ;set item# to the number of times that digit appears
      ;in the set of input numbers (ie in 000111222, 0 appears
      ;3 times, 1 appears three times and 2 appears three times
      ;all other numbers appear 0 times, so the number_count_array
      ;would be: 10,3,3,3,0,0,0,0,0,0,0 after this loop runs
      ^!Set
      %number_count_array^%number_counter%%=^$StrCount("^%number_searched%";"^%string_ordered%";False;False)$
      ;inc the counter and number being looked for and repeat
      ;until all 10 possible digits accounted for
      ^!Inc %number_counter%
      ^!Inc %number_searched%
      ^!If ^%number_counter% < 11 number_of_numbers ELSE Next

      ;built a list of possible numbers
      ^!Set %number_counter%="1"
      :loop_1_digit
      ;if there are zero occurances of a number, then
      ;don't write it to the possible numbers, otherwise
      ;do by adding it to a list with returns (^P)
      ^!If ^%number_count_array^%number_counter%% = 0 Skip
      ^!Set
      %single_digit_numbers%=^%single_digit_numbers%^P^$Calc(^%number_counter%-1)$

      ;inc and repeat (notice I used Calc above instead of
      ;a second variable like I did in the first loop
      ^!Inc %number_counter%
      ^!If ^%number_counter% < 11 loop_1_digit ELSE Next

      ;output the original string followed by the ordered string
      ;followed by the possible numbers
      ^!InsertText From the String: ^%string%^PReordered to:
      ^%string_ordered%^Pwe get the following possibilities:
      ^%single_digit_numbers%

      ;next on to the loop for two digit numbers
    Your message has been successfully submitted and would be delivered to recipients shortly.