Announcement

Collapse
No announcement yet.

Sorting indexed arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sorting indexed arrays

    PB9 online Help describes multidimensional array sorting the following way:

    TYPE SalesType
    OrderNum AS LONG
    PartNumber(1 TO 20) AS STRING * 20
    END TYPE
    ...
    DIM Sales AS SalesType
    ...
    DIM Temp(1 TO 20) AS STRING * 20 AT VARPTR(Sales.Partnumber(1))
    ARRAY SORT Temp()
    ERASE Temp()

    The problem is always the need of using a type outside a function.
    So, if you need often to calculate a smallest/biggest value under variant conditions then you need a bunch of types. Functions with Min/Max calculations are as a consequence not simply portable.

    In PB there exists a one dimensional function Min/Max, which unfortunately cannot be used with arrays. It is quite useless for common math purposes. So, why not simply implement a Min/Max function like in C++ with at least one row containing an index() array and at least one row containing a value() array with hard codable relevance to the index() array?

    Currently, I have to use functions with built in bubble sorting or similiar. These are too much time consuming in some cases.
    Norbert Doerre

  • #2
    Send those new feature suggestions ... eg
    Code:
      Z =   SUM|MIN|MAX|MEAN|AVERAGE (numericarray ([Start element])[FOR count])
    .. to [email protected]
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      BTW, you don't need to sort arrays to find any of the aggregate functions listed.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Sent it to support slightly changed:

        I'd introduce a suggestion for handling indexed numeric arrays.

        I changed Michael's thoughts from
        Z = SUM|MIN|MAX|MEAN|AVERAGE (numericarray, ([Start element])[FOR count])

        to

        Z(index) = SUM|MIN|MAX|MEAN|AVERAGE (indexed_numericarray, index, ([Start element])[FOR count])
        index = the resulting index of the given back indexed_numericarray
        indexed_numericarray = element(index)

        indexed_numericarray Z(index) means that the array values have an index value attached similiar to a hidden item value in combo boxes.
        The index values must always remain unchangeable and "hard coded" to their specific array values. So it is possible
        to identify the values by their indices even after the values have been resorted.
        The elements of the array must therefore have the syntax
        element(index, value).
        Sample:
        Z(index) = MIN(indexed_numericarray(), index, 12, For 44)
        Norbert Doerre

        Comment


        • #5
          Yes, no sort is needed for min and max etc. aggregates. The below finds min and max of 3 fields in a 64MB file in a couple seconds.
          Code:
          #COMPILE EXE
          #DIM ALL
          
          TYPE partType
             partNum     AS QUAD
             partName    AS STRING * 20
             partCode    AS LONG
          END TYPE
          
          FUNCTION PBMAIN () AS LONG
              LOCAL xs AS STRING, y AS LONG, qMax, qMin AS QUAD
              LOCAL qMaxS, qMinS AS STRING * 20, qMaxL, qMinL AS LONG
              xs = SPACE$(64000032)
              OPEN "C:\boomBinFil03.dat" FOR BINARY SHARED AS #1 'any file > 64MB for demo purposes. A video file is a good stand-in.
              GET #1,, xs
              DIM x(2000000) AS partType AT STRPTR(xs)
          
              CALL quikMaxMin(x(), qMax, qMin, qMaxS, qMinS, qMaxL, qMinL, 4) 'find the max and min of chosen field, or all 3--CASE 4.
              REPLACE ANY CHR$(0,9,10,13) WITH "OBLC" IN qMaxS 'for display reasons only
              REPLACE ANY CHR$(0,9,10,13) WITH "OBLC" IN qMinS
              ? STR$(qMax) & $CRLF & STR$(qMin) & $CRLF & $CRLF & qMaxS & $CRLF & qMinS & $CRLF & $CRLF & _
                STR$(qMaxL) & $CRLF & STR$(qMinL)
          
          END FUNCTION
          
          FUNCTION quikMaxMin(x() AS partType, qMax AS QUAD, qMin AS QUAD, _
                                               qMaxS AS STRING * 20, qMinS AS STRING * 20, _
                                               qMaxL AS LONG, qMinL AS LONG, _
                                               choice AS LONG) AS LONG
          
             LOCAL ii AS LONG
          
             SELECT CASE choice
                CASE 1
                   qMax = &hffffffffffffffff
                   qMin = &h7fffffffffffffff
          
                   FOR ii = 0 TO UBOUND(x())
                      IF x(ii).partNum > qMax THEN qMax = x(ii).partNum
                      IF x(ii).partNum < qMin THEN qMin = x(ii).partNum
                   NEXT
          
                CASE 2
                   qMaxS = ""
                   qMinS = STRING$(20, 255)
          
                   FOR ii = 0 TO UBOUND(x())
                      IF x(ii).partName > qMaxS THEN qMaxS = x(ii).partName
                      IF x(ii).partName < qMinS THEN qMinS = x(ii).partName
                   NEXT
          
                CASE 3
                   qMaxL =  &hffffffff
                   qMinL =  &h7fffffff
          
                   FOR ii = 0 TO UBOUND(x())
                      IF x(ii).partCode > qMaxL THEN qMaxL = x(ii).partCode
                      IF x(ii).partCode < qMinL THEN qMinL = x(ii).partCode
                   NEXT
          
                CASE 4                    'all fields max and min
                   qMax = &hffffffffffffffff
                   qMin = &h7fffffffffffffff
                   qMaxS = ""
                   qMinS = STRING$(20, 255)
                   qMaxL =  &hffffffff
                   qMinL =  &h7fffffff
          
                   FOR ii = 0 TO UBOUND(x())
                      IF x(ii).partNum  > qMax  THEN qMax  = x(ii).partNum
                      IF x(ii).partNum  < qMin  THEN qMin  = x(ii).partNum
                      IF x(ii).partName > qMaxS THEN qMaxS = x(ii).partName
                      IF x(ii).partName < qMinS THEN qMinS = x(ii).partName
                      IF x(ii).partCode > qMaxL THEN qMaxL = x(ii).partCode
                      IF x(ii).partCode < qMinL THEN qMinL = x(ii).partCode
                   NEXT
             END SELECT
          
          END FUNCTION
          Last edited by John Gleason; 19 Jun 2009, 08:27 AM. Reason: eliminated pointers--use TYPE directly, simpler & faster

          Comment


          • #6
            Seems like those items (SUM|MIN|MAX|AVERAGE|MEAN|MEDIAN|STNDEV|VARIANCE|etc) would be more suited to the ARRAY SCAN statement.
            Rod
            In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

            Comment


            • #7
              " AVERAGE|MEAN"

              I've never understood the difference between "Average" and "Mean". Is ther any difference? I've just figured they were the same and 'In" people used "Mean" and real men used "average" (having no need to impress anyone).

              ==========================================
              "Most people would sooner die than think;
              in fact, they do so."
              Bertrand Russell (1872-1970)
              ==========================================
              It's a pretty day. I hope you enjoy it.

              Gösta

              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

              Comment


              • #8
                'Average' and 'Mean' are synonymous and 'Mean' is short for 'Arithmetic Mean' as opposed to Harmonic Mean and Geometric Mean.

                Comment


                • #9
                  In my accounting days, AVERAGE was the average, MEAN was the closest of the figures to the average, MEDIAN was the one in the middle. The only place I ever encountered the situation regarding MEAN was in the course itself. AVERAGE and MEDIAN I used outside the course in practice.
                  The courses were taken before the days of the personal computer, which may have a little bearing on the slight differences in what we're accustomed to now.

                  And Gösta,
                  Mediocre men use MEDIAN, mean men use MEAN, average men use AVERAGE, and real men tell someone else to do it!!
                  Rod
                  In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                  Comment


                  • #10
                    Originally posted by Rodney Hicks View Post
                    Seems like those items (SUM|MIN|MAX|AVERAGE|MEAN|MEDIAN|STNDEV|VARIANCE|etc) would be more suited to the ARRAY SCAN statement.
                    How about ARRAY STATS(arr([index]), SUM|MIN|MAX|ETC)

                    Comment


                    • #11
                      Like I said, send those new feature suggestions to the address above.

                      That way the PBPTB are looking at ALL the ideas and can pick and choose the best/most requested/easiest to use.

                      Me, I really don't need any more intrinsics which may not be used in the function position; that is I would much prefer

                      Code:
                         X =      functionname(arguments)
                      instead of
                      Code:
                         functionname (arguments)  [U]TO X [/U]
                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        FWIW, I don't see any value to supporting an "index" value attached to/associated with an array element.... just create another array of same size to hold related values OR change the array to a UDT array with an 'IndexValue' member.
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Originally posted by Michael Mattias View Post
                          FWIW, I don't see any value to supporting an "index" value attached to/associated with an array element....
                          I second that codemotion.

                          Comment


                          • #14
                            Norbert, you might be able to create your own library (or class) to encapsulate the new functions (or data type) that you need. I had a similar problem recently.
                            Bernard Ertl
                            InterPlan Systems

                            Comment


                            • #15
                              >indexed_numericarray

                              Just so we can be on the same page, what exactly do you mean by an "indexed array?"

                              That term has no meaning to me.

                              You might be speaking of an "index array", likely meaning "an array containing ordered subscripts of another array."

                              You might be using the word "index" as a pseudo-synonym for "subscript."

                              But the meaning of "indexed array" eludes me.

                              MCM
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                Like I said, send those new feature suggestions to the address above.
                                I sent mine in over a year ago. However the required calculations are so simple that I can see why something like that would be on the back burner.
                                Rod
                                In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                                Comment


                                • #17
                                  Originally posted by Rodney Hicks View Post
                                  In my accounting days, AVERAGE was the average, MEAN was the closest of the figures to the average,
                                  Okay Rodney I get it now. In an average of whole numbers for example, the Average could be 5.5 while the Mean would be 5 or 6 (if ether was used to get the Average.) Or perhaps a better way to say is the Mean is actual number nearest to the Average.

                                  And Gösta,
                                  Mediocre men use MEDIAN, mean men use MEAN, average men use AVERAGE, and real men tell someone else to do it!!
                                  And yet again, you clarifed a deep and puzzling mystery for me. Thanks. My gratification runs deeper than the widest river.

                                  ======================================================
                                  "How can I lose to such an idiot?"
                                  A shout from chessmaster Aaron Nimzovich (1886-1935)
                                  ======================================================
                                  It's a pretty day. I hope you enjoy it.

                                  Gösta

                                  JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                  LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                  Comment


                                  • #18
                                    Gösta,
                                    "Mean" is the correct term but average means the same thing. It's the sum of all your data divided by the number of data items.

                                    "Median" is the value nearest the middle of a sample.

                                    "Mode" is the most often occurring value.

                                    e.g. in the group 1,2,2,3,4,5,5,5,6

                                    The mean is (1+2+2+3+4+5+5+5+6)/9 = 33 / 9 = 3.666..
                                    The average is also 3.666..

                                    The median is 4 as it's the middle value of the sample when the values are put in numerical order.

                                    The Mode is 5 since the number 5 occurrs more than any other number in the sample.

                                    Paul.

                                    Comment


                                    • #19
                                      However the required calculations are so simple that I can see why something like that would be on the back burner.
                                      Well maybe, just maybe...

                                      ... the 'burner position' is based on a very rational number - the number of users who actually invest the five minutes it takes to submit a new feature suggestion.

                                      I know if I have five users request a new feature for The Provider Payment Partner (tm) System, it gets a better 'burner position' than does the feature requested by one user.

                                      Besides, if you can't take the time to send in a suggestion, how badly do you really want it?

                                      MCM
                                      Michael Mattias
                                      Tal Systems (retired)
                                      Port Washington WI USA
                                      [email protected]
                                      http://www.talsystems.com

                                      Comment


                                      • #20
                                        Originally posted by Paul Dixon View Post
                                        Gösta,
                                        "Mean" is the correct term but average means the same thing. It's the sum of all your data divided by the number of data items.

                                        e.g. in the group 1,2,2,3,4,5,5,5,6

                                        The mean is (1+2+2+3+4+5+5+5+6)/9 = 33 / 9 = 3.666..
                                        The average is also 3.666..

                                        Paul.
                                        Thanks, Paul. I guess my question is why have two different words if they "mean" exactly the same thing? Jeez, I was feeling all happy when Rodney differentiated between the two (his mean would be 4 and maybe 3), but now I'm back in the river of consternation. {deep sigh}

                                        ======================================
                                        "The things that will destroy us are:
                                        politics without principle;
                                        pleasure without conscience;
                                        wealth without work;
                                        knowledge without character;
                                        business without morality;
                                        science without humanity;
                                        and worship without sacrifice."
                                        Mahatma Gandhi
                                        ======================================
                                        It's a pretty day. I hope you enjoy it.

                                        Gösta

                                        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                        Comment

                                        Working...
                                        X