TXT into array i'm on Vb6

User avatar
sal21
PlatinumLounger
Posts: 4534
Joined: 26 Apr 2010, 17:36

TXT into array i'm on Vb6

Post by sal21 »

Code: Select all


Option Explicit
Private Sub ARRAY_TXT()

Dim FF As Integer, TXTARRAY() As String
FF = FreeFile
Close #FF
Open "C:\Lavori_Vb6\MAPPA_ITALIA\FILES\RCS\Dati_cittadinanza_2023.csv" For Input As #FF
    TXTARRAY = Split(Input(LOF(FF), FF), vbLf)
Close #FF

'Show the array...
Dim I As Long
For I = 0 To UBound(TXTARRAY)
    Debug.Print TXTARRAY(I)
Next I

End Sub

I use this code to store txt into array and loop it

But is possible to store into the array only a filtered lines whit:

example for second line:
2002;001001;Agliè;100;Italia;Unione Europea;Europa;1205;1325;2530

condition:

Len(Split(strLine, ";")(1)) = 6 and Len(Split(strLine, ";")(3)) = 3

part of txt file

...
yes
2002;001001;Agliè;100;Italia;Unione Europea;Europa;1205;1325;2530
2002;001001;Agliè;201;Albania;Europa Centro-Orientale;Europa;2;0;2
2002;001001;Agliè;314;Cina;Asia Orientale;Asia;1;0;1
2002;001001;Agliè;215;Francia;Unione Europea;Europa;0;1;1
2002;001001;Agliè;436;Marocco;Africa Settentrionale;Africa;4;0;4
2002;001001;Agliè;443;Nigeria;Africa Occidentale;Africa;1;2;3
2002;001001;Agliè;615;Perù;America Centro-Meridionale;America;0;1;1

no
2002;026;Treviso;342;Nepal;Asia Centro-Meridionale;Asia;0;2;2
2002;026;Treviso;529;Nicaragua;America Centro-Meridionale;America;1;3;4
2002;026;Treviso;442;Niger;Africa Occidentale;Africa;2;2;4
2002;026;Treviso;443;Nigeria;Africa Occidentale;Africa;333;302;635
2002;026;Treviso;231;Norvegia;Altri paesi europei;Europa;2;0;2
2002;026;Treviso;719;Nuova Zelanda;Oceania;Oceania;1;4;5
2002;026;Treviso;232;Paesi Bassi;Unione Europea;Europa;27;39;66
2002;026;Treviso;344;Pakistan;Asia Centro-Meridionale;Asia;50;19;69
2002;026;Treviso;324;Palestina;Asia Occidentale;Asia;2;2;4

yes
2002;111106;Villasor;254;Moldova;Europa Centro-Orientale;Europa;0;1;1
2002;111106;Villasor;232;Paesi Bassi;Unione Europea;Europa;0;2;2
2002;111106;Villasor;233;Polonia;Unione Europea;Europa;0;1;1
2002;111106;Villasor;235;Romania;Unione Europea;Europa;0;1;1
2002;111107;Villaspeciosa;100;Italia;Unione Europea;Europa;943;976;1919
2002;111107;Villaspeciosa;514;Cuba;America Centro-Meridionale;America;0;1;1
2002;111107;Villaspeciosa;215;Francia;Unione Europea;Europa;2;3;5
2002;111107;Villaspeciosa;436;Marocco;Africa Settentrionale;Africa;3;2;5
2002;111107;Villaspeciosa;615;Perù;America Centro-Meridionale;America;2;2;4
2002;111107;Villaspeciosa;450;Senegal;Africa Occidentale;Africa;1;0;1
2002;111107;Villaspeciosa;239;Spagna;Unione Europea;Europa;1;0;1
2002;111107;Villaspeciosa;311;Sri Lanka;Asia Centro-Meridionale;Asia;0;1;1
...

User avatar
HansV
Administrator
Posts: 79671
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TXT into array i'm on Vb6

Post by HansV »

Instead of reading the text file in one go, read the lines of the text file one by one and add them to the array only if the two conditions are met.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4534
Joined: 26 Apr 2010, 17:36

Re: TXT into array i'm on Vb6

Post by sal21 »

HansV wrote:
18 Apr 2024, 17:39
Instead of reading the text file in one go, read the lines of the text file one by one and add them to the array only if the two conditions are met.
With redimpreserve...

User avatar
HansV
Administrator
Posts: 79671
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TXT into array i'm on Vb6

Post by HansV »

Yes, indeed.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4534
Joined: 26 Apr 2010, 17:36

Re: TXT into array i'm on Vb6

Post by sal21 »

HansV wrote:
18 Apr 2024, 18:15
Yes, indeed.
MY test withouth condition , only for a test:

Option Explicit
Private Sub ARRAY_TXT()

Dim FF As Integer, TXTARRAY() As String, K As Long
FF = FreeFile
Close #FF
Open "C:\Lavori_Vb6\MAPPA_ITALIA\FILES\RCS\Dati_cittadinanza_2023.csv" For Input As #FF
TXTARRAY = Split(Input(LOF(FF), FF), vbLf)
Close #FF

Dim I As Long
Dim RESULT As String

For I = 0 To UBound(TXTARRAY)

ReDim Preserve TXTARRAY(K)

K = K + 1

Next I

For I = 1 To UBound(TXTARRAY)
RESULT = TXTARRAY(I)
Next I

End Sub

Not sure.....

User avatar
HansV
Administrator
Posts: 79671
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TXT into array i'm on Vb6

Post by HansV »

I wouldn't use two loops, and I would use two arrays:

Code: Select all

Private Sub ARRAY_TXT()
    Dim FF As Integer, InputArray() As String, OutputArray() As String, I As Long, K As Long
    FF = FreeFile
    Close #FF
    Open "C:\Lavori_Vb6\MAPPA_ITALIA\FILES\RCS\Dati_cittadinanza_2023.csv" For Input As #FF
    InputArray = Split(Input(LOF(FF), FF), vbLf)
    Close #FF

    For I = 0 To UBound(InputArray)
        If conditions_are_true Then
            ReDim Preserve OutputArray(K)
            OutputArray(K) = InputArray(I)
            K = K + 1
        End If
    Next I

    Erase InputArray
    'OutputArray now contains the lines you need
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4534
Joined: 26 Apr 2010, 17:36

Re: TXT into array i'm on Vb6

Post by sal21 »

HansV wrote:
18 Apr 2024, 21:44
I wouldn't use two loops, and I would use two arrays:

Code: Select all

Private Sub ARRAY_TXT()
    Dim FF As Integer, InputArray() As String, OutputArray() As String, I As Long, K As Long
    FF = FreeFile
    Close #FF
    Open "C:\Lavori_Vb6\MAPPA_ITALIA\FILES\RCS\Dati_cittadinanza_2023.csv" For Input As #FF
    InputArray = Split(Input(LOF(FF), FF), vbLf)
    Close #FF

    For I = 0 To UBound(InputArray)
        If conditions_are_true Then
            ReDim Preserve OutputArray(K)
            OutputArray(K) = InputArray(I)
            K = K + 1
        End If
    Next I

    Erase InputArray
    'OutputArray now contains the lines you need
End Sub
Tks Bro!

User avatar
SpeakEasy
5StarLounger
Posts: 666
Joined: 27 Jun 2021, 10:46

Re: TXT into array i'm on Vb6

Post by SpeakEasy »

So, ios this the same csv as in your other thread (https://eileenslounge.com/viewtopic.php ... c7c35ed445)?

If so, then is double down on my suggestion of treating the csv as a database (using a text ISAM), since you seem to be doing various very db-like queries against it