Page 1 of 1 1
Topic Options
#47242 - 03/10/23 06:19 PM Arrays
Saad Offline
Lurker

Registered: 03/10/23
Posts: 1
How can I either add multiple elements to an existing array with elements, append one array to another or merge two arrays.

Thanks.

Top
#47243 - 03/10/23 06:23 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
You will want to REDIM PRESERVE the existing array to the new size, then iterate over the second array and append the elements to the end.
Top
#47244 - 03/10/23 06:45 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
Heres a quick little UDF for it. Call it like this:

code:
$Array1 = 0,1,2,3,4

$Array2 = 5,6

$Array3 = AddArrays($Array1, $Array2)

For $i = 0 to UBound($Array3)
?"Element=" $Array3[$i]
Next

exit 1

Heres the source, not sure if there is already a UDF for this or not ...

code:
Function AddArrays($Array1, $Array2)

; Returns a new $Array1

Dim $n,$i

$n = UBound($Array1) + 1

REDIM PRESERVE $Array1[$n+UBound($Array2)]

For $i = 0 to UBound($Array2)
$Array1[$n+$i] = $Array2[$i]
Next

$AddArrays = $Array1

EndFunction



[ 23. October 2003, 19:27: Message edited by: Shawn ]

Top
#47245 - 03/10/23 06:54 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 01/04/25
Posts: 11161
Loc: Boston, MA, USA
Alternative way to join two arrays:
code:
$array=split(join($array1,chr(1))+chr(1)+join($array2,chr(1)),chr(1))

_________________________
There are two types of vessels, submarines and targets.

Top
#47246 - 03/10/23 06:55 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
btw, this UDF doesn't change either of the two source arrays, because parms are passed by value the returned array is a just merged version of the two passed arrays ... you could replace the source array by doing this though:

$Array1 = AddArrays($Array1, $Array2)

Top
#47247 - 03/10/23 07:03 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
Saad, I've been up-staged by a JoinSplit ... you should use the JoinSplt ... got to get the JoinSplit ... seriously, if your arrays are large the JoinSplit will probably be faster but I have no data to back that up ...
Top
#47248 - 03/10/23 07:10 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 01/04/25
Posts: 11161
Loc: Boston, MA, USA
The drawback of the JOINSPLIT is that it requires CHR(1) not be present inside any of the array elements and that you might exceed the string length limit for large arrays.
_________________________
There are two types of vessels, submarines and targets.

Top
#47249 - 03/10/23 07:36 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
hmmm, unfortunately the JoinSplit method is an order of magnitude slower than the REDIM PRESERVE ... my benchmarks for joining two 50,000 element arrays ...

REDIM = 1.712 seconds

JOINSPLIT = 17.646 seconds

I still like the JoinSplit though ...

[ 23. October 2003, 19:37: Message edited by: Shawn ]

Top
#47250 - 03/10/23 07:50 PM Re: Arrays
Bryce Offline
KiX Supporter
*****

Registered: 00/02/29
Posts: 3164
Loc: Houston TX
one of those cases where golfing can hurt your code preformance [Wink]
Top
#47251 - 03/10/23 07:53 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
ja, it's golf score kixass
Top
#47252 - 03/10/23 08:02 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 01/04/25
Posts: 11161
Loc: Boston, MA, USA
I never stated that KiXgolfing is a performance enhancer [Big Grin]
_________________________
There are two types of vessels, submarines and targets.

Top
#47253 - 03/10/23 08:29 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
I don't know, depends on what kinda performance your talking about ... did we lose Saad ?
Top
#47254 - 03/10/24 04:44 AM Re: Arrays
Allen Moderator Offline
KiX Supporter
*****

Registered: 03/04/19
Posts: 4261
Loc: USA
[Eek!] I can't speak for Saad but THANKS! I was just needing this very thing. I did a quick search through the UDFs on Wed and didn't find anything, so unless anyone knows differently, I think this is a prime addition to the UDFs. [Smile]
Top
#47255 - 03/10/24 01:31 PM Re: Arrays
Radimus Moderator Offline
Moderator
*****

Registered: 00/01/06
Posts: 5186
Loc: Tampa, FL
hmmm... I just jumped on the UDF post without reading this one...

It would be a better internal kix function

$array3=$array1+$array2

But Shawn's UDF works just as well... and gets past the CHR limitation
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#47256 - 03/10/27 03:10 AM Re: Arrays
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 01/06/05
Posts: 22294
Loc: Claremore, OK
where is an limitation?
_________________________
!

download KiXnet

Top
#47257 - 03/10/27 03:13 AM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 01/04/25
Posts: 11161
Loc: Boston, MA, USA
The limitation of using a unique delimiter that is not present in any of the array elements.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Chris S., Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (green78) and 144 anonymous users online.
Newest Members
Keris2112, Jorromans, qrstuv140696, Bloodboy, NIJAKIX
17451 Registered Users

Generated in 0.094 seconds in which 0.045 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org