fill array bidimensional

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

fill array bidimensional

Post by sal21 »

I read line by line a text file.
during the loop i fill My_var with a value.
And My_var can assume the value similar:

AAA
AAA
BBB
CCC
CCC
CCC
...

I need to store into bidimensional array in matrce1 the name of My_var and matrice2 the summ of repetition of same value in this case:

matrice1 matrice2
AAA 2
BBB 1
CCC 3

i hope understand my:-)

other way to store value and uor repettion are welcome

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

Re: fill array bidimensional

Post by HansV »

Is the text file sorted on the value of My_var?
Best wishes,
Hans

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

Re: fill array bidimensional

Post by sal21 »

HansV wrote:Is the text file sorted on the value of My_var?
I get the value of My_var similar:


sFileName = " C:\A.txt"
Open sFileName For Input As #1
Do Until EOF(1)
Input#1, sFirststring
If Mid(sFirststring, 1, 5) = "Hello" Then
My_var=Mid(myline, 4,4)...
End If
Loop

ecc....

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

Re: fill array bidimensional

Post by HansV »

So presumably the values of My_var do not occur in ascending order.
I'd enter each value in a new row in a worksheet, and at the end create a pivot table.
Or enter each value in a new record in an Access table and run a totals query to return the unique values with the number of occurrences.
Best wishes,
Hans

Becks
2StarLounger
Posts: 196
Joined: 31 Mar 2011, 03:41
Location: Perth, Western Australia

Re: fill array bidimensional

Post by Becks »

Here is a way of doing it without reference to "external" data structures. If the case of the names is important, then change vbTextCompare to vbBinaryCompare. After loop then can examine aNames in a FOR loop

Code: Select all

'outside loop
'declare vars
Dim sStore  As String
Dim sIndex  As String
Dim iPos    As Integer
Dim iCount  As Integer
Dim aNames()    As Variant

'initialise stores, counter
    iCount = 0
    sIndex = ""
    sStore = ";"

'inside if test
        iPos = InStr(1, sStore, vbTab & my_var & vbTab, vbBinaryCompare)
        If iPos = 0 Then
            'position in store
            sIndex = sIndex & Format(Len(sStore), "00000") & vbTab
            'add to store
            sStore = sStore & my_var & vbTab
            'add to array
            ReDim Preserve aNames(1, iCount)
            aNames(0, iCount) = my_var
            aNames(1, iCount) = 1
            'increment counter
            iCount = iCount + 1
        Else    'already found
            iPos = (InStr(1, sIndex, Format(iPos, "00000") & vbTab) - 1) / 6
            aNames(1, iPos) = aNames(1, iPos) + 1
        End If

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

Re: fill array bidimensional

Post by HansV »

That's clever!
But shouldn't sStore be initialized to

sStore = vbTab

since you use vbTab as delimiter?
Best wishes,
Hans

Becks
2StarLounger
Posts: 196
Joined: 31 Mar 2011, 03:41
Location: Perth, Western Australia

Re: fill array bidimensional

Post by Becks »

Oops - originally it was, but I changed it to semi-colon then back again but forgot to change that one :blush:

If the result had to be alphabetical order then there are a couple of extra steps :grin: