Resize array so that ubound gives -1?

New to FreeBASIC? Post your questions here.
ecxjoe
Posts: 92
Joined: Aug 08, 2009 6:01
Contact:

Resize array so that ubound gives -1?

Postby ecxjoe » May 09, 2021 22:02

Can you redim an array back to where ubound() returns -1?

Example:

Code: Select all

redim items() as integer

print ubound( items ) '- shows -1

redim preserve items( 0 )

print ubound( items ) '- shows 0

'- move array back to zero elements?
redim items() '- compile error
redim items( -1 ) '- runtime error

Instead of adding an additional variable to keep track of the number of items in the array ( e.g. "itemCount += 1: redim preserve items( itemCount-1 )" ), I was experimenting with keeping track of the item count by using only ubound() instead ( e.g. grab the item count when needed with "ubound( items )+1" ). But, if I need to remove items from the array, I like to resize it back down, ideally back to zero elements if needed, so that ubound() reports -1 again.
badidea
Posts: 2333
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Resize array so that ubound gives -1?

Postby badidea » May 09, 2021 22:09

You can erase the array:

Code: Select all

dim as long b() 'no elements
print ubound(b)

redim b(0) '1 element with index 0
print ubound(b)

erase(b) 'no elements
print ubound(b)

So you have to add an 'if' statement. Example (part of a larger class):

Code: Select all

'remove from end of list
function pt_list.pop() as pt_dbl
   dim as pt_dbl pt_
   dim as integer ub = ubound(pt)
   if ub >= 0 then
      pt_ = pt(ub)
      if ub = 0 then
         erase pt
      else
         redim preserve pt(ub - 1)
      end if
   end if
   return pt_
end function
Last edited by badidea on May 09, 2021 22:17, edited 1 time in total.
ecxjoe
Posts: 92
Joined: Aug 08, 2009 6:01
Contact:

Re: Resize array so that ubound gives -1?

Postby ecxjoe » May 09, 2021 22:16

Awesome! Thanks. :-)
Pierre Bellisle
Posts: 43
Joined: Dec 11, 2016 17:22

Re: Resize array so that ubound gives -1?

Postby Pierre Bellisle » May 31, 2021 4:54

...keeping track of the item count...

Note that having ubound() as -1 does not always mean there is zero item in an array...

lbound may be negative or above zero
redim items(-5 to -1)
redim items(5 to 9)

ubound may be negative or equal zero
redim items(-5 to -1)
redim items(-4 to 0)

In those cases, to get item count, you may use
itemCount = ubound(items) - lbound(items) + 1

Code: Select all

redim items(-5 to -1) : print lbound(items), ubound(items), ubound(items) - lbound(items) + 1; " element in array" '-5 -1 5
redim items(5 to 9)   : print lbound(items), ubound(items), ubound(items) - lbound(items) + 1; " element in array" ' 5  9 5
redim items(-4 to 0)  : print lbound(items), ubound(items), ubound(items) - lbound(items) + 1; " element in array" '-4  0 5
fxm
Moderator
Posts: 10369
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Resize array so that ubound gives -1?

Postby fxm » May 31, 2021 7:36

Another variant to test if an 'array' is empty:
    If @array(Lbound(array)) = 0
      (no data buffer allocated)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests