L leongcc Member Licensed User Longtime User Jun 18, 2016 #1 What is the best way to code a function (in B4A) that returns a variable size array of string ? Using Regex.Split as an example : Code: Sub Split(delimit as string, s as string) as string() ... return x() End Sub A number of strings are created and they are returned as x(). I would think List is used to store the strings and then converted to an array before returning ? Or Is x() continuously being re-dimensioned during processing ? Or The strings are stored a file and then read-back into an array ?
What is the best way to code a function (in B4A) that returns a variable size array of string ? Using Regex.Split as an example : Code: Sub Split(delimit as string, s as string) as string() ... return x() End Sub A number of strings are created and they are returned as x(). I would think List is used to store the strings and then converted to an array before returning ? Or Is x() continuously being re-dimensioned during processing ? Or The strings are stored a file and then read-back into an array ?
DonManfred Expert Licensed User Longtime User Jun 18, 2016 #2 Code: Sub Split(delimit as string, s as string) as string() ' calculate the amount of items you need to return dim count as int = 10 ' 10 in this example dim x(count) as string ' Fill x(0) to x(9) ..... return x End Sub Upvote 0
Code: Sub Split(delimit as string, s as string) as string() ' calculate the amount of items you need to return dim count as int = 10 ' 10 in this example dim x(count) as string ' Fill x(0) to x(9) ..... return x End Sub
L leongcc Member Licensed User Longtime User Jun 20, 2016 #3 DonManfred said: Code: Sub Split(delimit as string, s as string) as string() '*** ' calculate the amount of items you need to return dim count as int = 10 ' 10 in this example dim x(count) as string ' Fill x(0) to x(9) ..... return x End Sub Click to expand... Thanks for responding. Actually my query is more about the portion of codes marked with '***'. While calculating the amount of items I need to return, I will at the same time capturing the item 1 by 1. This is happening before I declare the array 'Dim x(count) as string'. From your expert opinion, where should the captured items to be stored before they are loaded to the array for returning. Is it better to do this: ' create a large array Dim x(99999) as string '... capture items to x and count it, let say count=10 'redim array dim x(count) as string return x Or do this: ' create a list Dim y as list '... capture items to y, let say count = list size = 10 'create array with list size dim x(count) as string ' Fill x(0) to x(count-1) ..... return x Upvote 0
DonManfred said: Code: Sub Split(delimit as string, s as string) as string() '*** ' calculate the amount of items you need to return dim count as int = 10 ' 10 in this example dim x(count) as string ' Fill x(0) to x(9) ..... return x End Sub Click to expand... Thanks for responding. Actually my query is more about the portion of codes marked with '***'. While calculating the amount of items I need to return, I will at the same time capturing the item 1 by 1. This is happening before I declare the array 'Dim x(count) as string'. From your expert opinion, where should the captured items to be stored before they are loaded to the array for returning. Is it better to do this: ' create a large array Dim x(99999) as string '... capture items to x and count it, let say count=10 'redim array dim x(count) as string return x Or do this: ' create a list Dim y as list '... capture items to y, let say count = list size = 10 'create array with list size dim x(count) as string ' Fill x(0) to x(count-1) ..... return x
klaus Expert Licensed User Longtime User Jun 20, 2016 #4 Do you really need an array of strings? Why not using directly a List? Upvote 0
L leongcc Member Licensed User Longtime User Jun 20, 2016 #5 I have been using ready made functions that returns array of strings, such as Regex.Split(). I know how they can be coded in C, but I am curious how they can be properly coded in B4A. Upvote 0
I have been using ready made functions that returns array of strings, such as Regex.Split(). I know how they can be coded in C, but I am curious how they can be properly coded in B4A.
klaus Expert Licensed User Longtime User Jun 20, 2016 #6 Is it better to do this: ' create a large array Dim x(99999) as string '... capture items to x and count it, let say count=10 'redim array dim x(count) as string return x Click to expand... This will not work because when you Dim the array again it is empty! You will need to either: - go two times through the capturing, first for the count second for the filling. - use a List for intermediate storing. Upvote 0
Is it better to do this: ' create a large array Dim x(99999) as string '... capture items to x and count it, let say count=10 'redim array dim x(count) as string return x Click to expand... This will not work because when you Dim the array again it is empty! You will need to either: - go two times through the capturing, first for the count second for the filling. - use a List for intermediate storing.