Home > Blockchain >  From a txt file with entries, create an array and from the array search element and print out
From a txt file with entries, create an array and from the array search element and print out

Time:07-09

My Issue

I'm building a simple tool in .NET using VB.NET in VisualStudio 2019 which, from a .txt file formatted like this, with an "n" number of entries (like idk 130 or more):

IDelement1;Text Like this 
IDelement2;Some More Text Like This 1 
IDelement3;Hello again!

and so on...

What does my code?

  1. Reads C:/.../file-0.txt

  2. Creates an array based from the input elements, by splitting them line-by-line and knowing when separate them with ";" char:

    (IDelement1, Text Like this) (IDelement2, Some More Text Like This 1) (IDelement3, Hello again!) ...

  3. Once created the array, I need to search for the associated "IDelement1"'s object, then bring up "Text Like this" and place it on a label, a tabledatagrid, popup, etc. I don't know why my code isn't working properly :( I tried a lot of steps. If someone could help me, any kind of help, would be really appreciated...

I leave you here both my tries (DivideTranslSource, DivideTranslSource1)

FindTextTranslation("IDelement1") --> Text Like this

the TXT file needs to be also changed by other people easily, that's why I chose this kind of file format. In the next version, I'll need to include a switch case function that automatically select the language by reading a value in a file, like file-0 --> Italian, file-1 english etc.

The Code

Dim translArray(,) As String = DivideTranslSource("C:\VisualStudio2019\LangFiles\comments-0.txt", 2, CChar(";"))

    Dim arrTranslationID() As String
    Dim arrTranslationText() As String

    Public Function FindTextTranslation(indexTranslationNumber As String) As String
        For i As Integer = 0 To translArray.GetUpperBound(0) - 1
            If translArray(0, i) = indexTranslationNumber Then
                Return translArray(1, i)
            End If
        Next
        Return String.Empty
    End Function


    Public Function DivideTranslSource1(ByVal strFilename As String, ByVal intColumns As Integer, ByVal splitChar As Char) As String(,)
        Dim strArray(intColumns, 0) As String
        Dim y As Integer = 0
        Using sr As New IO.StreamReader(strFilename)
            While Not sr.EndOfStream
                Dim splitValues() As String = sr.ReadLine.Split(splitChar)
                ReDim Preserve strArray(intColumns, y)
                For x As Integer = 0 To intColumns - 1 
                    strArray(x, y) = splitValues(x)  
                Next
                y  = 1 
            End While
        End Using
        Return strArray
    End Function

    Public Function DivideTranslSource(ByVal strFilename As String, ByVal intColumns As Integer, ByVal splitChar As Char) As String(,)
        Dim strArray(intColumns, 0) As String
        Dim y As Integer = 0 
        Using sr As New IO.StreamReader(strFilename)          
            While Not sr.EndOfStream

                Dim currentRow As String = sr.ReadLine()
                Dim currentRowKey = currentRow.Split(splitChar)
                Dim currentRowValue As String
                Dim currentRowKeyLength As Integer
                currentRowKeyLength = currentRowKey.Length
                currentRowValue = currentRow.Substring(currentRow, currentRow.Length - currentRowKey)
                'currentRowValue = currentRow.Substring(currentRowKeyLength, currentRowKey.Length)
                ReDim Preserve strArray(currentRowValue, currentRow)                      
                'y  = 1                                                    
            End While
        End Using
        Return strArray
    End Function

CodePudding user response:

Here you go. The secret us to make use of a dictionary.

Private Sub btn_TestTranslations_Click(sender As Object, e As EventArgs) Handles btn_TestTranslations.Click
    ' Import the translations
    ImportTranslations("c:\temp\translations.txt")
    ' Lookup a translation
    MessageBox.Show(GetTranslation("IDelement3"))
End Sub

' Make sure we have a translation and then return it
Private Function GetTranslation(ID As String) As String
    If Translations.ContainsKey(ID) Then
        ' Return the translation (look it up in the dictionary)
        Return Translations(ID)
    Else
        ' We don't have a translation for this ID, return ""
        Return ""
    End If
End Function

' Dictionary to store the translations in
Private Translations As Dictionary(Of String, String)

' Load the dictionary from disk
Private Sub ImportTranslations(FilePath As String)
    Dim CurrentRow As String
    Dim Items() As String
    ' Create the dictionary
    Translations = New Dictionary(Of String, String)
    Using sr As New IO.StreamReader(FilePath)
        While Not sr.EndOfStream
            CurrentRow = sr.ReadLine()
            ' Split the row based on ";" character
            Items = CurrentRow.Split(";")
            ' Make sure we have 2, non-blank items
            If Items.Length = 2 AndAlso Items(0).Trim <> "" AndAlso Items(1).Trim <> "" Then
                ' Add the translation to the dictionary
                Translations(Items(0).Trim) = Items(1).Trim
            End If
        End While
    End Using
End Sub
  • Related