Categorizing using File Handling techniques

JohnDBCTX
NewLounger
Posts: 12
Joined: 18 Jun 2016, 06:14

Categorizing using File Handling techniques

Post by JohnDBCTX »

Slowly, I am learning how passing parameters from one routine to another.
However this code below runs well, but it did not create the two file names and categorize them.
Can anyone help me find a way to fix this?

Do I need two returning parameters for each routine (one each)?

Code: Select all

Sub CatagorizeNamesByGender()
Dim Jp As Long
Dim strZ(3), StaticArr(3) As String

Open "C:\Users\John\Documents\List of Names.txt" For Input As #100

For Jp = 0 To 3
    Line Input #100, strZ(Jp)
Next Jp

For Jp = 0 To 3
If strZ(Jp) = "Male" Then
    OutputMaleNames StAddX:=StaticArr
End If

If strZ(Jp) = "Female" Then
    OutputFemaleNames StArr:=StaticArr
End If

Next Jp
    

End Sub


Sub OutputFemaleNames(ByRef StArr() As String)

Dim Hp As Long

Open "C:\Users\John\Documents\Female Names.txt" For Output As #101
For Hp = LBound(Hp) To UBound(Hp)
    If InStr(1, StArr(Hp), "Female", vbTextCompare) > 0 Then
         Write #101, StArr(Hp)
    End If
Next Hp
Close #101



End Sub
Sub OutputMaleNames(ByRef StAddX() As String)
Dim Hp As Long

Open "C:\Users\John\Documents\Male Names.txt" For Output As #102
For Hp = LBound(Hp) To UBound(Hp)
    If InStr(1, StAddX(Hp), "Male", vbTextCompare) > 0 Then
         Write #102, StAddX(Hp)
    End If
Next Hp
Close #102
With regards,
JohnDBCTX

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

Re: Categorizing using File Handling techniques

Post by HansV »

1) You only call OutputMaleNames if one of the first 4 lines in the text file List of Names.txt equals (not just contains) "Male".
2) Nowhere in your code do you assign any value to the array StaticArr, so it will be an empty array.
3) The line

For Hp = LBound(Hp) To UBound(Hp)

makes no sense.
Best wishes,
Hans

JohnDBCTX
NewLounger
Posts: 12
Joined: 18 Jun 2016, 06:14

Re: Categorizing using File Handling techniques

Post by JohnDBCTX »

Code: Select all

Sub CatagorizeNamesByGender()
Dim Jp As Long
Dim strZ(2) As String
Open "C:\Users\John\Documents\List of Names.txt" For Input As #100
Open "C:\Users\John\Documents\Male Names.txt" For Output As #101
Open "C:\Users\John\Documents\Female Names.txt" For Output As #102


For Jp = 0 To 2
    Line Input #100, strZ(Jp)
Next Jp

For Jp = 0 To 2
    Select Case InStr(1, strZ(Jp), "Male", vbTextCompare)
        Case 0:
            Write #101, strZ(Jp)
    End Select
    Select Case InStr(1, strZ(Jp), "Female", vbTextCompare)
        Case Is > 0:
            Write #102, strZ(Jp)
        Case Else
            Write #101, strZ(Jp)
    End Select
Next Jp
Close #102, 101, 100


End Sub
Does this answer my problem just to eliminate the need for data entry? I do hope I am right.

Regards,
JohnDBCTX

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

Re: Categorizing using File Handling techniques

Post by HansV »

I don't think that it will do what you want.
Also, I don't see the need to use an array. You can process each line as it is read from the file:

Code: Select all

Sub CatagorizeNamesByGender()
    Dim Jp As Long
    Dim strZ As String

    Open "C:\Users\John\Documents\List of Names.txt" For Input As #100
    Open "C:\Users\John\Documents\Male Names.txt" For Output As #101
    Open "C:\Users\John\Documents\Female Names.txt" For Output As #102

    For Jp = 0 To 2
        Line Input #100, strZ
        If InStr(1, strZ, "Male", vbTextCompare) > 0 Then
            Write #101, strZ
        End If
        If InStr(1, strZ, "Female", vbTextCompare) > 0 Then
            Write #102, strZ
        End If
    Next Jp

    Close #100
    Close #101
    Close #102
End Sub
Best wishes,
Hans

JohnDBCTX
NewLounger
Posts: 12
Joined: 18 Jun 2016, 06:14

Re: Categorizing using File Handling techniques

Post by JohnDBCTX »

I have redone the code snippet this time within the While...Wend structure.

Here is the entire list:
1, John, Male
2, Jack, Male
3, Jill, Female



Code: Select all

 While Not EOF(100)
         Line Input #100, strZ
         If InStr(1, strZ, "Male", vbTextCompare) Then
             Write #102, strZ
         End If
     Wend


Retruns the output:

1, John, Male
2, Jack, Male
3, Jill, Female



Whereas:

Code: Select all

While Not EOF(100)
         Line Input #100, strZ
         If InStr(1, strZ, "Female", vbTextCompare) Then
             Write #102, strZ
         End If
     Wend
Returns the output: 3, Jill, Female


How can I fix this source code snippet so that it can only produce
the output:



1, John, Male
2, Jack, Male

JohnDBCTX

JohnDBCTX
NewLounger
Posts: 12
Joined: 18 Jun 2016, 06:14

Re: Categorizing using File Handling techniques

Post by JohnDBCTX »

Once again, I fixed it for myself.

I had to change it from vbTextCompare to vbBinaryCompare.

Code: Select all

Sub CatagorizeNamesByGender()
    Dim strZ, LkM As String

    Open "C:\Users\John\Documents\List of Names.csv" For Input As #100
    Open "C:\Users\John\Documents\Male Names.csv" For Output As #101
    Open "C:\Users\John\Documents\Female Names.csv" For Output As #102

    
    
    While Not EOF(100)
        Line Input #100, strZ
        If InStr(1, strZ, "Male", vbBinaryCompare) Then
            Write #101, strZ
        End If
        If InStr(1, strZ, "Female", vbBinaryCompare) Then
            Write #102, strZ
        End If
    Wend


Close #102, 101, 100

End Sub
Someone spread the word again.

Regards,
JohnDBCTX