Copying arrays?

I have a question about arrays. Yes, I read the docs already :slight_smile:

Say I have an array with 40 elements, qual(40). Is there a way to easily duplicate the array into a different array, copying it in effect, to qualwork(40)?

For i As Integer = 0 To qual.Ubound otherArray(i) = qual(i) // Caution: if qual(i) is an object, the object is not copied, both // otherArray(i) and qual(i) will point to the same object Next

Note that qual(40) is an array with 41 elements - arrays are 0-based.

I knew how to do it this way, was hoping for a “built-in” solution. Thanks Eli for the help though! :slight_smile:

One more thing I was curious about: How do I find the highest number in an array, and store its index in a variable? For example, if the highest number in a 40 element array was the #21 element in the array, how would I find it and store 21 in a variable?

I would simply do

for each e as type in qual
qualwork.append e
next

type must be the type for the elements.

[quote=152883:@Derek DiBenedetto]I have a question about arrays. Yes, I read the docs already :slight_smile:

Say I have an array with 40 elements, qual(40). Is there a way to easily duplicate the array into a different array, copying it in effect, to qualwork(40)?[/quote]

This works for string arrays :

[code] dim a(40), b(-1) as string
for i as integer = 1 to 40
a(i) = str(i)
next
// Duplicate a into b
b = a

for i as integer = 1 to 40
Listbox1.addrow b(i)
next[/code]

b = a will reference one array with two variables. Probably not what someone wants.

Indeed not.

What about using join and split ? That should be much faster. For strings, of course.

[code] dim qual(40), qualwork(-1), dupli as string
for i as integer = 1 to 40
qual(i) = str(i)
next

dupli = join(qual,chr(9))

qualwork = split(dupli,chr(9))

for i as integer = 1 to 40
Listbox1.addrow qualwork(i)
next
[/code]

no, join/split is bad if text contains delimiter.

Also you make unnecessary copies.

Quickest will be:

redim array
copy each item in a loop

and disable background tasks.

I have read time and again split and join where much faster than other methods. Whatever…

Anyone have a solution for this one: One more thing I was curious about: How do I find the highest number in an array, and store its index in a variable? For example, if the highest number in a 40 element array was the #21 element in the array, how would I find it and store 21 in a variable?

FindTheHighestMBS :wink:

More seriously,

[code] dim qual(40), zindex, oldhighest as integer

//Do whatever you need to fill qual with values

for i as integer = 0 to 40
if qual(i) > oldhighest then
oldhighest = qual(i)
zindex = i
end if
next

msgbox str(zindex)[/code]

Thanks, Michel! A little adaptation, and your solution worked great. Thanks to Eli too, who supplied the solution I first came up with and seems the most straightforward.

But only works for string arrays.

IF the array is of items of a fixed size, such as integers, then it could prove faster to keep them in memory blocks.

A memory block holding 40 integers can be treated much like an array

[code]
dim i as Int32
const blocksize as integer = 4 * 40

dim m as New MemoryBlock(blocksize)
dim n as new memory block (blocksize)
m.Int32value(6)=256495
i=m.Int32Value(6)

n =m.leftb(blocksize) //copy the contents in one call

i = m.Int32Value(6)[/code]

That uses several implicit conversions and throws away the memoryblock allocated by

dim n as new memory block (blocksize)

Better would be

n.StringValue(0, blocksize) = m.StringValue(0,blocksize)

Hello,
please, is a way, how to read javascript array and insert it into xojo array? I use javascript component…
Firstly I load data into xojo array from database, than I call javascript, where I insert data from xojo array. In javascript array I do some calculations and I need now get back data from javascript array into xojo array… I really don’t know how. I know how to get one value - from HashTag or from textfield, but array, which is class?

[quote=167103:@Krystof Jurecka]Hello,
please, is a way, how to read javascript array and insert it into xojo array? I use javascript component…
Firstly I load data into xojo array from database, than I call javascript, where I insert data from xojo array. In javascript array I do some calculations and I need now get back data from javascript array into xojo array… I really don’t know how. I know how to get one value - from HashTag or from textfield, but array, which is class?[/quote]

You probably do it the way you would in Xojo, by looping and presenting each element through HashTag back to Xojo.

I use now hastag for number of visible days on webpage and arrays are as classes (item[0].name, item[0].date,…). Or can I have more than one #?