Reassigning an array all at once

New to FreeBASIC? Post your questions here.
Post Reply
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Reassigning an array all at once

Post by thebigh »

How do I assign all the elements of an array after DIMming it? The following code shows what I want to do, and two possible workarounds that are not very good.

Code: Select all

dim as integer arr(10) = {1,2,3,4,5,6,7,8,9,10}

'arr = {2,3,5,7,11,13,17,19,23,29}  'ALAS, does not work!

arr(0) = 1
arr(1) = 4
arr(2) = 9 ' Y U K ! ! ! ! !
'...
arr(9) = 100

dim as integer newarr(10) = {1,1,2,3,5,8,13,21,34,55} 'do not want a new array
Is there a good way?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reassigning an array all at once

Post by fxm »

Preliminary remark:

- When you size a static array, the general syntax is:
Dim name ( [lbound To] ubound [, ...] ) As DataType
where lbound (0 by default) refers to the lower bound or the smallest index, and ubound refers to the upper bound or the largest index.

- In your shortcuted syntax used:
dim as integer arr(10)
10 is not the number of elements (numbered from 1 to 10), but the value of the largest index (the smallest index being fixed to 0 per default).
So your array has 11 elements numbered from 0 to 10.
Last edited by fxm on Feb 19, 2019 8:45, edited 1 time in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reassigning an array all at once

Post by fxm »

Currently in FB, there is no compact syntax for assigning the array elements at once (only for the initializer at the declaration level).
You must reassign element per element.

Note:
Another workaround should be to encapsulate your array in a structure like a Type, and to use the built-in assignment defined for its instances (the Let operator).
Example:

Code: Select all

type UDT
  dim as integer arr(1 to 10)
end type

dim as UDT u = ({1,2,3,4,5,6,7,8,9,10})
print u.arr(10)

u = type({2,3,5,7,11,13,17,19,23,29})
print u.arr(10)

sleep
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reassigning an array all at once

Post by fxm »

Variance of the previous workaround, using such a Type structure containing a similar array, but in addition to the user array, the structure being placed at the same address as the user array itself:

Code: Select all

dim as integer arr(1 to 10) = {1,2,3,4,5,6,7,8,9,10}
Print arr(10)

type UDT
  dim as integer array(1 to 10)
end type
dim as UDT ptr parr = cptr(UDT ptr, @arr(1))

*parr = type({2,3,5,7,11,13,17,19,23,29})
print arr(10)

*parr = type({3,5,8,11,16,19,24,27,32,39})
print arr(10)

sleep
or (more generic code):

Code: Select all

dim as integer arr(1 to 10) = {1,2,3,4,5,6,7,8,9,10}
Print arr(10)

type UDTarr
  dim as typeof(arr) array(lbound(arr) to ubound(arr))
end type
dim as UDTarr ptr parr = cptr(UDTarr ptr, @arr(lbound(arr)))

*parr = type({2,3,5,7,11,13,17,19,23,29})
print arr(10)

*parr = type({3,5,8,11,16,19,24,27,32,39})
print arr(10)

sleep
or (if you prefer use a reference instead of a dereferenced pointer):

Code: Select all

dim as integer arr(1 to 10) = {1,2,3,4,5,6,7,8,9,10}
Print arr(10)

type UDTarr
  dim as typeof(arr) array(lbound(arr) to ubound(arr))
end type
dim byref as UDTarr rarr = *cptr(UDTarr ptr, @arr(lbound(arr)))

rarr = type({2,3,5,7,11,13,17,19,23,29})
print arr(10)

rarr = type({3,5,8,11,16,19,24,27,32,39})
print arr(10)

sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Reassigning an array all at once

Post by Tourist Trap »

thebigh wrote: Is there a good way?
Hello,

I think that when you want to extend the syntax of Freebasic with the tools of the language, you have to use MACROS. Unfortunatly, this is not too much easy when it's the first tries. But when you get it, you really can come to a code that is more like you want it to be from a syntactic point of view. With arrays, I have this example here: viewtopic.php?p=225712#p225712. It parses the curled braces of an array initializer so it may be interesting here as a possible thing among others.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reassigning an array all at once

Post by fxm »

Similar to my last workarounds, but with macro-generation as proposed by Tourist Trap:

Code: Select all

#macro assign1Darray(array, init...)
  scope
    type UDT
      dim as typeof(array) array(lbound(array) to ubound(array))
    end type
    dim as UDT ptr parray = cptr(UDT ptr, @array(lbound(array)))
    *parray = type(##init##)
  end scope
#endmacro

dim as integer arr(1 to 10) = {1,2,3,4,5,6,7,8,9,10}
Print arr(10)

assign1Darray(arr, {2,3,5,7,11,13,17,19,23,29})
print arr(10)

assign1Darray(arr, {3,5,8,11,16,19,24,27,32,39})
print arr(10)

sleep
Post Reply