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

Registered: 2003-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 - 2003-10-23 06:23 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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 - 2003-10-23 06:45 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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 - 2003-10-23 06:54 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
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 - 2003-10-23 06:55 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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 - 2003-10-23 07:03 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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 - 2003-10-23 07:10 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
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 - 2003-10-23 07:36 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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 - 2003-10-23 07:50 PM Re: Arrays
Bryce Offline
KiX Supporter
*****

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

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

Registered: 2001-04-25
Posts: 11164
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 - 2003-10-23 08:29 PM Re: Arrays
Shawn Administrator Offline
Administrator
*****

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

Registered: 2003-04-19
Posts: 4545
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 - 2003-10-24 01:31 PM Re: Arrays
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
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 - 2003-10-27 03:10 AM Re: Arrays
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
where is an limitation?
_________________________
!

download KiXnet

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

Registered: 2001-04-25
Posts: 11164
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, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 507 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.069 seconds in which 0.025 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