Announcement

Collapse
No announcement yet.

Delete one element from VARIANT-ARRAY, impossibly?

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

  • Delete one element from VARIANT-ARRAY, impossibly?

    Hallo,
    how can I delete one element from VARIANT array?
    e.g.

    Code:
    LOCAL vVar()   AS VARIANT
    
    REDIM vVar(1 TO 10)
    ...
    ARRAY DELETE vVar(5)  '<-- Error 482 in ....:  Data type mismatch



    Clearly, one can use sub, e.g.:
    Code:
    SUB DelElVarArray(BYVAL lDelElementNr AS LONG, _
                      vVarArray() AS VARIANT)
    
    LOCAL lFirstNr  AS LONG
    LOCAL lElements AS LONG
    LOCAL lNr       AS LONG
    LOCAL lNr2      AS LONG
    LOCAL vVar()    AS VARIANT
    
    lFirstNr  = LBOUND(vVarArray)
    lElements = UBOUND(vVarArray)
    REDIM vVar(lFirstNr TO lElements)
    
    lNr = lFirstNr
    DO
      vVar(lNr) = vVarArray(lNr)
      INCR lNr
    LOOP UNTIL lNr > UBOUND(vVarArray)
    
    lElements = UBOUND(vVarArray) - 1
    REDIM vVarArray(lFirstNr TO lElements)
    
    lNr  = lFirstNr
    lNr2 = lFirstNr
    DO
      IF lNr <> lDelElementNr THEN
        vVarArray(lNr2) = vVar(lNr)
        INCR lNr2
      END IF
      INCR lNr
    LOOP UNTIL lNr > lElements
    
    END SUB
    but, why ARRAY DELETE does not work???


    Thanks
    Last edited by Alexander Holzer; 7 Aug 2009, 07:34 AM.
    Yours sincerely

  • #2
    Variant variables are now supported by PowerBASIC, but their use is limited to that of a parameter assignment for conversion of data
    is noted in the docs, so ARRAY DELETE is apparently part of that limitation. To match the functionality of the ARRAY DELETE statement exactly, the last element isn't eliminated, but set to zero/empty. Here's an example below.
    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
    
        DIM vArr(10) AS VARIANT
        varArrDel(vArr(), 5)
    
    END FUNCTION
    
    FUNCTION varArrDel(vVarArray() AS VARIANT, element AS LONG) AS LONG
       LOCAL ii, x AS LONG
    
       x = UBOUND(vVarArray()) - 1
       FOR ii = LBOUND(vVarArray()) TO x        'shift all elements down 1 once past deleted element
          IF ii = element THEN
             FOR ii = element TO x
                vVarArray(ii) = vVarArray(ii+1)
             NEXT
             EXIT FOR
          END IF
       NEXT
       vVarArray(x + 1) = 0                     'set last element to 0
       
    END FUNCTION

    Comment


    • #3
      Here's a way using ARRAY DELETE:
      Code:
      #COMPILE EXE
      #DIM ALL
      
      FUNCTION PBMAIN () AS LONG
      
          DIM vArr(10) AS VARIANT
          DIM vArrDelete(10) AS STRING * 16 AT VARPTR(vArr(0)) 'since VARIANT is 16 long, overlay it in memory then delete
                                                               'element from vArrDelete() array
          ARRAY DELETE vArrDelete(5)
                                                               
      END FUNCTION
      Last edited by John Gleason; 7 Aug 2009, 11:10 AM.

      Comment


      • #4
        Code:
        DIM vArrDelete(10) AS STRING * 16 AT VARPTR(vArr(0)) 'since VARIANT is 16 long, overlay it in memory then delete
                                                                 'element from vArrDelete() array
            ARRAY DELETE vArrDelete(5)
        With variants, a deallocation of memory may be required depending on VT_xxxxx.

        Above "works" only if you don't care about leaks.

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

        Comment


        • #5
          Above "works" only if you don't care about leaks.
          My understanding is that you only have to worry about leaks if you are manipulating the internal structure of the variant. No?

          Comment


          • #6
            >.... the internal structure of the variant. No?

            No.

            The PB compiler is doing all that messy stuff for you. It's not a little. Be grateful.

            Get answer on ARRAY DELETE from PB support. In the help file, arrays of INTERFACEs are clearly excluded from ARRAY DELETE support, but that is not the same as an array of VARIANTs.

            I would try to find out directly from the publisher if the compiler is buggy or the help file is in error, for (at least) one of those statements must be true.

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

            Comment


            • #7
              Set the Empty keyword first then.
              hellobasic

              Comment


              • #8
                >Set the Empty keyword first then

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

                Comment


                • #9
                  with ARRAY DELETE from VARIANT's ARRAY's -> one BUG!
                  The interpreter should do all...
                  Yours sincerely

                  Comment


                  • #10
                    The interpreter should do all.
                    Define 'all'. If a compiler could do all, there would be no need for programmers.

                    From the LET statement (with Variants) help:
                    This flexibility comes at a great price in performance, so PowerBASIC limits their use to data storage and parameters only. You may assign a numeric value, a string value, or even an entire array to a Variant with the LET statement, or its implied equivalent. In the same way, you may assign one Variant value to another Variant variable, or even assign an array contained in a Variant to a compatible PowerBASIC array, or the reverse.
                    This implies that an over reliance on Variants, and anything that you do with them would be hurting your program's performance except in the simplest of circumstances. I would have a hard time having faith in the solution that works IN THIS CASE.
                    This is not a 'bug'.
                    Rod
                    In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                    Comment


                    • #11
                      I think he means, if arrays of variants are not valid objects of the ARRAY DELETE statement, then this restriction should be on the help file page for ARRAY DELETE along with the other restrictions, not buried in some obtuse discussion of how the VARIANT data type is implemented and requiring inference to realize.

                      OTOH... if the compiler is designed to support VARIANT arrays, then there is a compiler bug.

                      That's the problem when the observed behavior does not equal the documented behavior: you can't know if the compiler, the help file, or both the compiler and the help file are in error. All you do know is there is an error somewhere.
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Using a variable type without reading all there is on it is a good way to get bad data, whether its strings, numeric, or, has a name that implies the type is at variance with all other types.

                        It(VARIANT) does not sell itself as a catchall type of variable, but as a means to accommodate a concept foreign to PowerBASIC that gives PowerBASIC users extra ability.

                        The part I quoted is in at least two places in the help file 'Variant Data Types' and 'LET statement (with Variants)' and both are hyperlinked quite well to almost any mention of Variant.
                        And this sentence in the ARRAY DELETE help:
                        If expression is not present, the last element that the data is shifted out of will contain zero if array is a numeric array, or an empty string if array is a string array.
                        tells me that I have to look deeper to find out what to do with a Variant array.
                        But since they were specific with the following comment they could have added an extra word or two.
                        ARRAY DELETE cannot be used on arrays within UDT structures or on an array of Interfaces.
                        Rod
                        In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                        Comment


                        • #13
                          It must be described correctly in help file.
                          Yours sincerely

                          Comment


                          • #14
                            It is described correctly in the help file. It tells you what happens with numeric and string arrays.

                            That you tried it with VARIANTs, kudos to you for experimenting.
                            Rod
                            In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                            Comment

                            Working...
                            X