[Gambas-user] import data from text file and popolate a 2D array

Jussi Lahtinen jussi.lahtinen at gmail.com
Wed Sep 1 00:08:24 CEST 2021


Or if you really want normal 2d array, then it is easy IF all the rows
(here MAX_ROX is integer constant, which should have the information) have
the same length:

Private Function ParseData(sData As String) As String[]

  Dim sTemp As String[] = Split(Replace(sData[2, Len(sData) - 5], "}},",
""), "{")
  Dim sCache As String[]
  Dim sParsed As New String[sTemp.Count, MAX_ROW]

  For ii As Integer = 0 To sTemp.Max
    sCache = Split(sTemp[ii], ",")
    For jj As Integer = 0 To MAX_ROW
    sParsed[ii, jj] = sCache[jj]
    Next
  Next

  Return sParsed

End


Jussi

On Wed, Sep 1, 2021 at 1:00 AM Jussi Lahtinen <jussi.lahtinen at gmail.com>
wrote:

> Private Function ParseData(sData As String) As String[][]
>
>   Dim sTemp As String[] = Split(Replace(sData[2, Len(sData) - 5], "}},",
> ""), "{")
>   Dim sParsed As New String[][sTemp.Count]
>
>   For ii As Integer = 0 To sTemp.Max
>     sParsed[ii] = Split(sTemp[ii], ",")
>   Next
>
>   Return sParsed
>
> End
>
>
>
> Jussi
>
> On Tue, Aug 31, 2021 at 2:54 PM Roberto Premoli <
> roberto.premoli at tiscali.it> wrote:
>
>> hello. i run gambas 3.12-2 (debian 10 stable)
>>
>> I have a list of raw data into a text file. It is a database as follow:
>>
>> [{field1,field2,...fieldN}},{field1,field2,...fieldN}},...{field1,field2,...fieldN}}]
>>
>> each line starts with "{", end with "}}" and inside each line, the
>> fields are separated by ",".
>>
>> i wish to read the text file as input, get each "block" {...}}, and
>> split it into fields to populate a database bidimentiona array (about
>> 6000 linees and 20 fields in each line).
>> I can elaborate all file database searching the "marker" ( the "{", the
>> "," and the "}},") but I suppose it will be slow compared to a
>> dedicated command (if it exist).
>> I write here asking if there is some "magic trick" to elaborate strings
>> that allow me to speed up all the process and avoid me to write
>> a dedicated funcion.
>>
>> Thanks,
>> Roberto
>>
>> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210901/22b30c05/attachment.htm>


More information about the User mailing list