I have three string arrays
dim one(-1), two(-1), three(-1) as string
dim all(-1) as string
....
all.append(one)
all.append(two)
all.append(three)
What’s the best way to concatenate these three arrays into one array?
I have three string arrays
dim one(-1), two(-1), three(-1) as string
dim all(-1) as string
....
all.append(one)
all.append(two)
all.append(three)
What’s the best way to concatenate these three arrays into one array?
Off the top of my head.
Sub AppendArray (Extends arr() As String, sourceArr() As String)
For i as integer = 0 to sourceArr.Ubound
arr.Append sourceArr( i )
Next i
End Sub
all.AppendArray one
all.AppendArray two
all.AppendArray three
And if you dont care about the order, use:
Sub AppendArray (Extends arr() As String, sourceArr() As String)
For each item as String in sourceArr
arr.Append item
Next
End Sub
It could be faster.
How could your method effect order?
for each
is guaranteed to process every element once, but does not guarantee order.
wow learn something new every day!
Egads - I never noticed that warning. If they ever change the implementation so that it doesn’t process the items in order, that will break lots of code…
I know!
I’ve heard reports that it has not gone in order already so that code might already be broken.
Is there some reason For Each doesn’t guarantee order? A cursory search shows Java, C#, php and Perl traverse in order, while VB is like Xojo. I don’t use For Each for this reason but I’d like to. Here’s a feedback request for order <https://xojo.com/issue/23798>
Depending on how array elements are stored internally, it might lead to an optimization (or potential optimization) that wouldn’t be available if order were guaranteed. Just a guess though.
A lazy way
delim = “,” // pick one which is never in your string
all = split(join(one,delim) + delim + join(two,delim) + delim + join(three,delim), delim)
You can never guarantee that so I would not recommend doing that. It is the source of very hard to find bugs.
For each traverses every item.
Guaranteeing order may tie our hands in certain ways - so we guarantee it goes through every item.
If you DO care about order use for .. next
and traversing either forward or backward is one way to not get caught by surprise IF for each ever does use some other ordering.
[quote=65625:@Hanspeter Bleuler]delim = “,” // pick one which is never in your string
all = split(join(one,delim) + delim + join(two,delim) + delim + join(three,delim), delim)[/quote]
Good idea, but I would choose another delimiter. Either a multicharacter like $$ or a higher ASCII. My personal preference goes for Logical Not “¬” which is pretty unlikely in regular text.
I’d lean toward lower ascii, like Bell or SOH.