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

DIM, REDIM

Expand Messages
  • garpmore
    It looks like DIM does everything REDIM does. Is there any case where this is not true? If not, why both?
    Message 1 of 9 , Sep 5, 2008
      It looks like DIM does everything REDIM does. Is there any case where
      this is not true? If not, why both?
    • styskul
      I should probably let the experts speak here, but if I were to wager a guess I d say the difference is that doing a DIM on a variable that has already been
      Message 2 of 9 , Sep 5, 2008
        I should probably let the experts speak here, but if I were to wager a
        guess I'd say the difference is that doing a DIM on a variable that
        has already been DIMmed will not free excess memory. In other words,
        if you do:

        DIM s$(50)

        and then later on do

        DIM s$(10)

        in the best case scenario you're still using up the same amount of
        memory as when you did the DIM s$(50), and in worst case you're using
        more memory, because you've created a second instance of the variable.
        With REDIM, however, you're actually freeing memory if you REDIM from
        a larger array to a smaller one. Of course, this is just my
        speculation at this point.

        To those who might read this and have the power to make changes, what
        would be the possibility of getting a "preserve" qualifier to REDIM so
        that values already in the array don't get wiped out?

        --- In libertybasic@yahoogroups.com, "garpmore" <garpmore@...> wrote:
        >
        > It looks like DIM does everything REDIM does. Is there any case where
        > this is not true? If not, why both?
        >
      • Stefan Pendl
        ... Generally speaking, DIM is used for setting the initial size of an array, whereas REDIM is used to change the size and reinitialize (clear) the array. As
        Message 3 of 9 , Sep 5, 2008
          >
          > I should probably let the experts speak here, but if I were to wager a
          > guess I'd say the difference is that doing a DIM on a variable that
          > has already been DIMmed will not free excess memory. In other words,
          > if you do:
          >
          > DIM s$(50)
          >
          > and then later on do
          >
          > DIM s$(10)
          >
          > in the best case scenario you're still using up the same amount of
          > memory as when you did the DIM s$(50), and in worst case you're using
          > more memory, because you've created a second instance of the variable.
          > With REDIM, however, you're actually freeing memory if you REDIM from
          > a larger array to a smaller one. Of course, this is just my
          > speculation at this point.
          >
          > To those who might read this and have the power to make changes, what
          > would be the possibility of getting a "preserve" qualifier to REDIM so
          > that values already in the array don't get wiped out?
          >

          Generally speaking, DIM is used for setting the initial size of an array, whereas REDIM is used to change the size and reinitialize
          (clear) the array.

          As for the effect on memory usage, I have not done any tests in this area.

          ---
          Stefan Pendl
        • stefanhes
          ... array, whereas REDIM is used to change the size and reinitialize ... area. ... Almost 2 years ago I asked the same question at this forum. (32693
          Message 4 of 9 , Sep 5, 2008
            >
            > Generally speaking, DIM is used for setting the initial size of an
            array, whereas REDIM is used to change the size and reinitialize
            > (clear) the array.
            >
            > As for the effect on memory usage, I have not done any tests in this
            area.
            >
            > ---
            > Stefan Pendl
            >

            Almost 2 years ago I asked the same question at this forum.
            (32693 difference between DIM and REDIM? )
            I never got a satisfying answer.
            I even challenged anyone to write a LB-program that would react one
            bit different if one replaced a DIM statement with a REDIM statement
            (or vice versa)
            I have never seen such a program.

            But perhaps Stefan can write one with a "print memory in use".

            I truly hope so.


            Stefan Hesseling
          • laurie camion
            I have an array of 1440 x 300. Each minute I need to add a new record at the end, and drop the first (0) record. I know I can shift all the records with a
            Message 5 of 9 , Sep 5, 2008
              I have an array of 1440 x 300. Each minute I need to add a new record at the end, and drop the first (0) record. I know I can shift all the records with a couple of loops, but is there a simpler/faster way?
               
                for x = 0 to 1438
                for n = 0 to 299
                data(x,n) = data(x+1,n)
                next n
                next x


















              [Non-text portions of this message have been removed]
            • David Speck
              Laurie, The computer is not usually fussy about which actual array index you are starting from. You can create a subroutine that indexes the virtual starting
              Message 6 of 9 , Sep 5, 2008
                Laurie,

                The computer is not usually fussy about which actual array index you are
                starting from.

                You can create a subroutine that indexes the virtual starting array
                pointer each time a new entry is added. If the pointer passes the end
                of array, then it rolls over back to the beginning.

                Sample code follows:
                'watch for line wraps

                '=====================
                Global ArrayIndex 'pointer to virtual starting point in array
                ArrayIndex = 0 'start with virtual index = real index

                'your code goes here

                '=====================
                function TransIndex(index) 'translate virtual into real index
                TransIndex=index+ArrayIndex 'combine virtual and actual index
                if TransIndex > 1440 then
                TransIndex = TransIndex-1440 'wrap around if beyond end
                end if
                end function
                '=====================

                Whenever you update your data array, increment the global variable
                ArrayIndex, and be sure to roll it back to zero if it is >= 1440.

                When you want to read a data value, just use the result of the
                TransIndex function operating on your virtual index value as the row
                subscript on your data array for all accesses to your array. E. G.

                result = data(TransIndex(x),n)

                That way, only the pointers move, not all the data.

                HTH,

                Dave


                laurie camion wrote:
                > I have an array of 1440 x 300. Each minute I need to add a new record at the end, and drop the first (0) record. I know I can shift all the records with a couple of loops, but is there a simpler/faster way?
                >
                > for x = 0 to 1438
                > for n = 0 to 299
                > data(x,n) = data(x+1,n)
                > next n
                > next x
              • Stefan Pendl
                ... I think Carl might have used the same code for DIM and REDIM accidently, so now you can swap the two commands without generating an error. The below won t
                Message 7 of 9 , Sep 5, 2008
                  > >
                  > > Generally speaking, DIM is used for setting the initial size of an
                  > array, whereas REDIM is used to change the size and reinitialize
                  > > (clear) the array.
                  > >
                  > Almost 2 years ago I asked the same question at this forum.
                  > (32693 difference between DIM and REDIM? )
                  > I never got a satisfying answer.
                  > I even challenged anyone to write a LB-program that would react one
                  > bit different if one replaced a DIM statement with a REDIM statement
                  > (or vice versa)
                  > I have never seen such a program.
                  >
                  > But perhaps Stefan can write one with a "print memory in use".
                  >
                  > I truly hope so.
                  >

                  I think Carl might have used the same code for DIM and REDIM accidently, so now you can swap the two commands without generating an
                  error.
                  The below won't raise an error, nor is it crashing.
                  I have posted a memory usage function some weeks ago, so any Stefan can write a check, if he likes ;-)

                  '---code start (watch for line wraps)
                  redim array(20)

                  for i = 1 to 20
                  array(i) = i
                  next

                  for i = 1 to 20
                  print array(i); ",";
                  next
                  print

                  dim array(20)

                  for i = 1 to 20
                  print array(i); ",";
                  next
                  print
                  end
                  '---code end

                  ---
                  Stefan Pendl
                • stefanhes
                  Perhaps two lines in the HELP-file must be rewritten: Arrays that contain more than 10 elements must be dimensioned before they can be used. Double dimensioned
                  Message 8 of 9 , Sep 6, 2008
                    Perhaps two lines in the HELP-file must be rewritten:


                    Arrays that contain more than 10 elements must be dimensioned before
                    they can be used.

                    Double dimensioned arrays must always be dimensioned with the DIM
                    statement before use.

                    Should be:

                    Arrays that contain more than 10 elements must be dimensioned
                    SOMEWHERE in your BAS-file with the DIM statement.

                    Double dimensioned arrays must always be dimensioned SOMEWHERE in your
                    BAS-file with the DIM statement.


                    It looks as if LB first checks the whole BAS-file and searches for dim
                    statements to allocate some memory for it. After that it starts
                    "running" the first line
                    DIM-ming afterwards can only be done with a constant integer, not with
                    a variable. SO:

                    dim array(20) is working
                    dim array(20*n) is not working, even if n is global and n=1


                    So: this program works correctly:

                    d(17)=666
                    print d(17)
                    end

                    dim d(20)



                    And this program is not working:

                    global n
                    n=1

                    d(17)=666
                    print d(17)
                    end

                    dim d(20*n)



                    Stefan Hesseling

                    P.S. initial DIMming with REDIM is NOT possible, so there is a
                    difference between DIM and REDIM. But I do belief all REDIM statements
                    can be replaced by DIM. If not, please give an example.
                  • laurie camion
                    Aha, that ll do it. Thank you! ... From: David Speck Subject: Re: [libertybasic] Shifting an array To:
                    Message 9 of 9 , Sep 6, 2008
                      Aha, that'll do it. Thank you!

                      --- On Fri, 9/5/08, David Speck <MedelecInstruments@...> wrote:

                      From: David Speck <MedelecInstruments@...>
                      Subject: Re: [libertybasic] Shifting an array
                      To: libertybasic@yahoogroups.com
                      Date: Friday, September 5, 2008, 8:56 PM






                      Laurie,

                      The computer is not usually fussy about which actual array index you are
                      starting from.

                      You can create a subroutine that indexes the virtual starting array
                      pointer each time a new entry is added. If the pointer passes the end
                      of array, then it rolls over back to the beginning.

                      Sample code follows:
                      'watch for line wraps

                      '=========== ========= =
                      Global ArrayIndex 'pointer to virtual starting point in array
                      ArrayIndex = 0 'start with virtual index = real index

                      'your code goes here

                      '=========== ========= =
                      function TransIndex(index) 'translate virtual into real index
                      TransIndex=index+ ArrayIndex 'combine virtual and actual index
                      if TransIndex > 1440 then
                      TransIndex = TransIndex-1440 'wrap around if beyond end
                      end if
                      end function
                      '=========== ========= =

                      Whenever you update your data array, increment the global variable
                      ArrayIndex, and be sure to roll it back to zero if it is >= 1440.

                      When you want to read a data value, just use the result of the
                      TransIndex function operating on your virtual index value as the row
                      subscript on your data array for all accesses to your array. E. G.

                      result = data(TransIndex( x),n)

                      That way, only the pointers move, not all the data.

                      HTH,

                      Dave

                      laurie camion wrote:
                      > I have an array of 1440 x 300. Each minute I need to add a new record at the end, and drop the first (0) record. I know I can shift all the records with a couple of loops, but is there a simpler/faster way?
                      >
                      > for x = 0 to 1438
                      > for n = 0 to 299
                      > data(x,n) = data(x+1,n)
                      > next n
                      > next x

















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