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

Re: [Clip] Finding Minimum Value in Array

Expand Messages
  • Don - HtmlFixIt.com
    I typically zero pad on the way in, something like: ;long line follows -- just a random line so variables aren t useful ^!InsertText
    Message 1 of 15 , Nov 1, 2009
    • 0 Attachment
      I typically zero pad on the way in, something like:
      ;long line follows -- just a random line so variables aren't useful
      ^!InsertText
      ^$StrFill("0";^$Calc(^%numberlength%-^$StrSize("^%place%")$)$)$^%place%.

      of course we aren't inserting test this time, we are changing the length
      of variables
      where numberlength is the length of your longest number in the set and
      place is the current number

      and then ...

      ^!Set %numbers%=003;008;329;001;001.25;007;010.34;004;009;006;002;005;011
      ^!SetArray %numbers_ordered%=^$StrReplace(";";"^P";"^%numbers%";0;0)$
      ^!SetArray %numbers_ordered%=^$StrSort("^%numbers_ordered%";0;1;0)$
      ^!SetArray
      %numbers_ordered%=^$StrReplace("^P";";";"^%numbers_ordered%";0;0)$
      ;long line follows
      ^!InsertText The lowest number in this set: ^%numbers%^%NL%is
      ^%numbers_ordered1%^%NL%and sorted list: ^%numbers_ordered%


      flo.gehrke wrote:
      > Hi all,
      >
      > What's the best way to find the minimum value in an array of numbers?
      >
      > For example, in: 3,8,329,1,1.25,7,10.34,4,9,6,2,5,11.
      >
      > To do it with commands like 'If A > B' etc seems to be a complicated undertaking. Also the MIN or MAX operators to be used with ^$Calc$ are not very useful. As far as I can see, they compare two numbers only:
      >
      > ^!Info ^$Calc(MIN(3;6))$
      >
      > outputs '3' but 'MIN(3;6;9)' returns an "error in expression" message.
      >
      > I found a small utility called EVAL.EXE that you can download from
      >
      > http://tp.lc.ehu.es/jma/msdos.html
      >
      > for free. It's from Juan M. Aguirregabiria, teaching Theoretical Physics at Euskal Herriko Unibertsitatea, Bilbao. I copied the EVAL.EXE and EVAL.HLP to C:\Windows, and it seemes to work fine.
      >
      > For example...
      >
      > ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Info ^$GetDosOutput(EVAL MIN(^%Array%))$
      >
      > correctly outputs '1'.
      >
      > The only problem I've seen so far: It seems to return a CRNL with the output. So we get into trouble when using the result in a command like ^!IfDiff. So...
      >
      >
      > ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Set %Min%=^$GetDosOutput(EVAL MIN(^%Array%))$
      > ^!IfDiff "^%Min%" "1" Next Else Skip_2
      > ^!Info Different!
      > ^!Goto End
      > ^!Info Not different!
      >
      >
      > provides a wrong result. But when removing the CRNL, the result will be correct:
      >
      >
      > ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Set %Min%=^$GetDosOutput(EVAL MIN(^%Array%))$
      > ^!Set %Min%=^$StrReplace("^P";"";^%Min%;0;0)$
      > ^!IfDiff "^%Min%" "1" Next Else Skip_2
      > ^!Info Different!
      > ^!Goto End
      > ^!Info Not different!
      >
      >
      > Please let me know if there are more problems with this utility or if there is any better solution. Thanks!
      >
      > Flo
      >
      >
      > P.S. I created a little "Sorting Machine" for a more intensive testing of Eval:
      >
      >
      > ^!SetListDelimiter ,
      > ^!SetArray %Dig%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Set %Nr%=1; %From%=1; %Final%=^%Dig0%
      >
      > :Loop
      > ^!Set %Val%=^%Dig^%Nr%%
      > ^!Append %SubArray%=^%Val%,
      > ^!Inc %Nr%
      > ^!If ^%Nr% > ^%Final% Check
      > ^!Goto Loop
      >
      > :Check
      > ; Remove last comma in SubArray
      > ^!Set %SubArray%=^$StrDeleteRight(^%SubArray%;1)$
      > ; Use EVAL.EXE to find minimum value in SubArray
      > ^!Set %Min%=^$GetDosOutput(EVAL MIN(^%SubArray%))$
      > ; Remove CRNL produced by EVAL
      > ^!Set %Min%=^$StrReplace("^P";"";"^%Min%";0;0)$
      > ^!ClearVariable %SubArray%
      > ^!Set %ANr%=1
      >
      > :ANrLoop
      > ; Find number of minimum value in array
      > ^!IfDiff "^%Min%" "^%Dig^%ANr%%" Next Else Reorder
      > ^!Inc %ANr%
      > ^!Goto ANrLoop
      >
      > :Reorder
      > ^!Set %Park%=^%Dig^%ANr%%
      > ^!Set %Dig^%ANr%%=^%Dig^%From%%
      > ^!Set %Dig^%From%%=^%Park%
      > ^!Inc %From%
      > ^!If ^%From%=^%Final% End
      > ^!Set %Nr%=^%From%
      > ^!Goto Loop
      >
      > :End
      > ^!Info Sorted: ^%Dig%
      > ^!ClearVariable %SubArray%
      >
      >
      > (Please note: No duplicate numbers allowed)
      >
      >
      >
      > ------------------------------------
      >
      > Fookes Software: http://www.fookes.com/
      > NoteTab website: http://www.notetab.com/
      > NoteTab Discussion Lists: http://www.notetab.com/groups.php
      >
      > ***
      > Yahoo! Groups Links
      >
      >
      >
      >
    • Don - HtmlFixIt.com
      well we have ansi and false_ansi so why not numeric as a choice? Ask the man ...
      Message 2 of 15 , Nov 1, 2009
      • 0 Attachment
        well we have ansi and false_ansi so why not numeric as a choice? Ask
        the man ...

        flo.gehrke wrote:
        > Though I'm still wondering why NT can't provide a numeric sorting. We always see users padding lines with zeros and tricks like that in order to achieve a numeric sorting on numbers. But this may be another topic...
        > > Flo
      • flo.gehrke
        ... Harvey, I agree with you. ^$StrTrim is another solution though it s geared towards removing blank spaces. From... ^!Set
        Message 3 of 15 , Nov 1, 2009
        • 0 Attachment
          --- In ntb-clips@yahoogroups.com, hsavage <hsavage@...> wrote:
          >
          > Flo,
          >
          > Small command line utilities are very handy. To get rid of the
          > crlf try this line.
          >
          > ^!Set %Min%=^$StrTrim("^$GetDosOutput(EVAL MIN(^%Array%))$")$

          Harvey,

          I agree with you. ^$StrTrim is another solution though it's geared towards removing blank spaces.

          From...

          ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
          ^!Toolbar New Document
          ^$GetDosOutput(EVAL MIN(^%Array%))$


          I got the impression that EVAL.EXE is adding one or more CRNL to the output.

          Flo
        • Hugo Paulissen
          Hi Flo, You can do this without evaluation. In the past I have used something along these lines: ^!SetListDelimiter , ^!SetArray
          Message 4 of 15 , Nov 2, 2009
          • 0 Attachment
            Hi Flo,

            You can do this without evaluation. In the past I have used something along these lines:

            ^!SetListDelimiter ,
            ^!SetArray %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
            ^!SetListDelimiter ^%NL%
            ^!SetArray %Array%=^$StrSort("^$StrReplace(",";"^%NL%";"^%Array%";0;0)$";False;True;False)$
            ^!Info ^%Array1%

            Regards,

            Hugo




            ________________________________
            From: flo.gehrke <flo.gehrke@...>
            To: ntb-clips@yahoogroups.com
            Sent: Mon, November 2, 2009 7:57:18 AM
            Subject: Re: [Clip] Finding Minimum Value in Array

             
            --- In ntb-clips@yahoogrou ps.com, hsavage <hsavage@... > wrote:
            >
            > Flo,
            >
            > Small command line utilities are very handy. To get rid of the
            > crlf try this line.
            >
            > ^!Set %Min%=^$StrTrim( "^$GetDosOutput( EVAL MIN(^%Array% ))$")$

            Harvey,

            I agree with you. ^$StrTrim is another solution though it's geared towards removing blank spaces.

            From...

            ^!Set %Array%=3,8, 329,1,1.25, 7,10.34,4, 9,6,2,5,11
            ^!Toolbar New Document
            ^$GetDosOutput( EVAL MIN(^%Array% ))$

            I got the impression that EVAL.EXE is adding one or more CRNL to the output.

            Flo







            [Non-text portions of this message have been removed]
          • Don - HtmlFixIt.com
            Doesn t work Hugo, ^!SetListDelimiter , ^!SetArray %Array%=3,8,329,7,10.34,4,9,6,2,5,11 ^!SetListDelimiter ^%NL% ^!SetArray
            Message 5 of 15 , Nov 2, 2009
            • 0 Attachment
              Doesn't work Hugo,

              ^!SetListDelimiter ,
              ^!SetArray %Array%=3,8,329,7,10.34,4,9,6,2,5,11
              ^!SetListDelimiter ^%NL%
              ^!SetArray
              %Array%=^$StrSort("^$StrReplace(",";"^%NL%";"^%Array%";0;0)$";False;True;False)$
              ^!Info ^%Array1% ^%NL% ^%Array%

              You just got lucky :-)

              We always zero pad because I use your method to zero pad. We need a
              numeric sort I think in addition to ANSI and false_ANSI

              Hugo Paulissen wrote:
              > Hi Flo,
              >
              > You can do this without evaluation. In the past I have used something along these lines:
              >
              > ^!SetListDelimiter ,
              > ^!SetArray %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
              > ^!SetListDelimiter ^%NL%
              > ^!SetArray %Array%=^$StrSort("^$StrReplace(",";"^%NL%";"^%Array%";0;0)$";False;True;False)$
              > ^!Info ^%Array1% ^%NL% ^%Array%

              >
              > Regards,
              >
              > Hugo
              >
            • Hugo Paulissen
                Hi Don,   Quite possible that I just got lucky ;-), but I m not so sure we need a numeric sort for this. I do not recall the reason for the zero-padding,
              Message 6 of 15 , Nov 2, 2009
              • 0 Attachment
                 
                Hi Don,
                 
                Quite possible that I just got lucky ;-), but I'm not so sure we need a numeric sort for this. I do not recall the reason for the zero-padding, but it doesn't seem necessary in this case.
                 
                In any case: if I add some other values to the array, it appears that the lowest value is returned every single time.
                 
                ^!SetArray %Array%=03,8,329,1,1.25,7,10.34,4,9,6,2,5,11,0.25,0.1,10000,20,0.0119,10,0.012
                 
                Hugo
                 
                 
                >>> 
                Doesn't work Hugo,

                ^!SetListDelimiter ,
                ^!SetArray %Array%=3,8, 329,7,10. 34,4,9,6, 2,5,11
                ^!SetListDelimiter ^%NL%
                ^!SetArray
                %Array%=^$StrSort( "^$StrReplace( ",";"^%NL% ";"^%Array% ";0;0)$"; False;True; False)$
                ^!Info ^%Array1% ^%NL% ^%Array%

                You just got lucky :-)

                We always zero pad because I use your method to zero pad. We need a
                numeric sort




                [Non-text portions of this message have been removed]
              • Hugo Paulissen
                OK Don, Sorry Flo, Forget my last post; I see what you mean... Now I *do* remember the padding zero issue! It s getting rusty inside... Hugo
                Message 7 of 15 , Nov 2, 2009
                • 0 Attachment
                  OK Don, Sorry Flo,

                  Forget my last post; I see what you mean... Now I *do* remember the padding zero issue! It's getting rusty inside...

                  Hugo




                  ________________________________
                  From: Hugo Paulissen <hugopaulissen@...>
                  To: ntb-clips@yahoogroups.com
                  Sent: Mon, November 2, 2009 4:28:06 PM
                  Subject: Re: [Clip] Finding Minimum Value in Array

                   
                   
                  Hi Don,
                   
                  Quite possible that I just got lucky ;-), but I'm not so sure we need a numeric sort for this. I do not recall the reason for the zero-padding, but it doesn't seem necessary in this case.
                   
                  In any case: if I add some other values to the array, it appears that the lowest value is returned every single time.
                   
                  ^!SetArray %Array%=03,8, 329,1,1.25, 7,10.34,4, 9,6,2,5,11, 0.25,0.1, 10000,20, 0.0119,10, 0.012
                   
                  Hugo
                   
                   
                  >>> 
                  Doesn't work Hugo,

                  ^!SetListDelimiter ,
                  ^!SetArray %Array%=3,8, 329,7,10. 34,4,9,6, 2,5,11
                  ^!SetListDelimiter ^%NL%
                  ^!SetArray
                  %Array%=^$StrSort( "^$StrReplace( ",";"^%NL% ";"^%Array% ";0;0)$"; False;True; False)$
                  ^!Info ^%Array1% ^%NL% ^%Array%

                  You just got lucky :-)

                  We always zero pad because I use your method to zero pad. We need a
                  numeric sort

                  [Non-text portions of this message have been removed]







                  [Non-text portions of this message have been removed]
                • flo.gehrke
                  ... Hugo, Don, I don t think that Hugo was wrong. There has always been some confusion about this issue (cf message #19038 ff). The trick is the ^%NL% as
                  Message 8 of 15 , Nov 2, 2009
                  • 0 Attachment
                    --- In ntb-clips@yahoogroups.com, Hugo Paulissen <hugopaulissen@...> wrote:
                    >
                    > OK Don, Sorry Flo,
                    >
                    > Forget my last post; I see what you mean... Now I *do* remember the padding zero issue! It's getting rusty inside...
                    >
                    > Hugo

                    Hugo, Don,

                    I don't think that Hugo was wrong. There has always been some confusion about this issue (cf message #19038 ff).

                    The trick is the ^%NL% as delimiter. With the semicolon as the default delimiter the array won't get sorted:

                    ^!SetArray %Array%=9;8;7;6;5
                    ^!Info Array unsorted:^P^%Array%
                    ^!SetArray %Array%=^$StrSort("^%Array%";0;1;0)$
                    ^!Info Sorting failed: ^%Array%
                    ^!Info Wrong minimum value: ^%Array1%

                    The clip outputs '9' which doesn't meet our intentions. With ^P as delimiter it doesn't work either.

                    With ^%NL% (which replaces the comma in Hugo's solution)...

                    ^!SetListDelimiter ^%NL%
                    ^!SetArray %Array%=9^%NL%8^%NL%7^%NL%6^%NL%5
                    ^!Info Array unsorted:^P^%Array%
                    ^!SetArray %Array%=^$StrSort("^%Array%";0;1;0)$
                    ^!Info Array correctly sorted:^P^%Array%
                    ^!Info Minimum value: ^%Array1%

                    the array gets sorted and the clip outputs the minimum value in the array. Nevertheless, it's still a normal ANSI sorting of digits and no numeric sorting.

                    Regards,
                    Flo
                  • Don - HtmlFixIt.com
                    I am pretty sure you are just picking lucky numbers to use and thus getting lucky :-) I agree that you need the line breaks as delimiter because we are sorting
                    Message 9 of 15 , Nov 2, 2009
                    • 0 Attachment
                      I am pretty sure you are just picking lucky numbers to use and thus
                      getting lucky :-)

                      I agree that you need the line breaks as delimiter because we are
                      sorting lines, not data elements. But it will not do what you wanted,
                      give the minimum unless you zero pad. You can however add zero padding,
                      sort, strip and output if you wish.

                      I mistakenly did not include the correct numbers in my prior example.

                      :Mine
                      ^!SetListDelimiter ,
                      ^!SetArray %Array%=8,329,7,4,9,6,5
                      ^!SetListDelimiter ^%NL%
                      ^!SetArray
                      %Array%=^$StrSort("^$StrReplace(",";"^%NL%";"^%Array%";0;0)$";False;True;False)$
                      ^!Info ^%Array1% ^%NL% ^%Array%


                      :Flo
                      ^!SetListDelimiter ^%NL%
                      ^!SetArray %Array%=8^%NL%329^%NL%7^%NL%4^%NL%9^%NL%6^%NL%5
                      ^!Info Array unsorted:^P^%Array%
                      ^!SetArray %Array%=^$StrSort("^%Array%";0;1;0)$
                      ^!Info Array correctly sorted:^P^%Array%
                      ^!Info Minimum value: ^%Array1%

                      Notice I also used NL in my example because we figured that one out many
                      years ago -- look up my username and zero padding ....

                      329 comes out on top in the above examples because 3 is the lowest
                      number. Hugo and I have done the zero padding bit many times over the
                      years as I use his method for it.

                      flo.gehrke wrote:
                      > --- In ntb-clips@yahoogroups.com, Hugo Paulissen <hugopaulissen@...> wrote:
                      >> OK Don, Sorry Flo,
                      >>
                      >> Forget my last post; I see what you mean... Now I *do* remember the padding zero issue! It's getting rusty inside...
                      >>
                      >> Hugo
                      >
                      > Hugo, Don,
                      >
                      > I don't think that Hugo was wrong. There has always been some confusion about this issue (cf message #19038 ff).
                      >
                      > The trick is the ^%NL% as delimiter. With the semicolon as the default delimiter the array won't get sorted:
                    • flo.gehrke
                      ... Don, ... Yes, Don, you are absolutely right. Now it was getting rusty inside for me ;-) I should have known better since I already said: Nevertheless,
                      Message 10 of 15 , Nov 2, 2009
                      • 0 Attachment
                        --- In ntb-clips@yahoogroups.com, "Don - HtmlFixIt.com" <don@...> wrote:
                        >
                        > I am pretty sure you are just picking lucky numbers to use and thus
                        > getting lucky :-)

                        Don,

                        > 329 comes out on top in the above examples because 3 is the
                        > lowest number.

                        Yes, Don, you are absolutely right. Now it was "getting rusty inside" for me ;-)

                        I should have known better since I already said: "Nevertheless, it's still a normal ANSI sorting of digits and no numeric sorting."

                        So a better numeric sorting remains on the wish list. Something like...

                        ^$StrSort("Str";Numeric;Ascending;RemoveDuplicates)$

                        (as you had in mind before). I hope Eric Fookes will read this...

                        Regards,
                        Flo
                      • Art Kocsis
                        I agree that NoteTab needs a numeric sort period, not just in the clip functions but in the editor itself. Under the assumption that the clip sort is a mirror
                        Message 11 of 15 , Nov 3, 2009
                        • 0 Attachment
                          I agree that NoteTab needs a numeric sort period, not just in the clip
                          functions but in the editor itself. Under the assumption that the clip sort
                          is a mirror of the editor sort you can easily see the results (and
                          fallacies), of all the arrays in this thread. Even zero padding is not an
                          answer:

                          Copy Hugo's last array to NTB, change all the commas to ^P and sort:
                          ^!SetArray %Array%=03,8,329,1,1.25,7,10.34,4,9,6,
                          2,5,11,0.25,0.1,10000,20,0.0119,10,0.012
                          Notice the "03" is less than "1", "11" is less than 2", and so on.

                          And while we're at it, NoteTab also sorely needs a major/minor column sort.
                          Jody's clip has never worked for me so I am left with going to Ultra Edit
                          for anything other than trivial column one sorts. The lack of column sorts
                          is a PITA and an unnecessary waste of time.

                          BTW, UE not only does the numeric sorts but preserved the leading zero
                          padding characters while correctly sorting the numeric values.

                          Art


                          At 02-11-2009 12:29, you wrote:
                          >--- In <mailto:ntb-clips%40yahoogroups.com>ntb-clips@yahoogroups.com, "Don
                          >- HtmlFixIt.com" <don@...> wrote:
                          > >
                          > > I am pretty sure you are just picking lucky numbers to use and thus
                          > > getting lucky :-)
                          >
                          >Don,
                          >
                          > > 329 comes out on top in the above examples because 3 is the
                          > > lowest number.
                          >
                          >Yes, Don, you are absolutely right. Now it was "getting rusty inside" for
                          >me ;-)
                          >
                          >I should have known better since I already said: "Nevertheless, it's still
                          >a normal ANSI sorting of digits and no numeric sorting."
                          >
                          >So a better numeric sorting remains on the wish list. Something like...
                          >
                          >^$StrSort("Str";Numeric;Ascending;RemoveDuplicates)$
                          >
                          >(as you had in mind before). I hope Eric Fookes will read this...
                          >
                          >Regards,
                          >Flo

                          ----------


                          No virus found in this outgoing message.
                          Checked by AVG - www.avg.com
                          Version: 8.5.424 / Virus Database: 270.14.47/2478 - Release Date: 11/03/09 07:36:00


                          [Non-text portions of this message have been removed]
                        Your message has been successfully submitted and would be delivered to recipients shortly.