0

I want to change/check x(5) using an if statement
if x(5) = 270 then x(5) = -90
Is there a way to access the array.

Sub Main()
    Dim filePath, filePathOut As String
    Dim fileName As String
    fileName = "test.txt"
    filePath =    System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, fileName)
    filePathOut = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "out\" & fileName)
    ' Create the IEnumerable data source.
    Dim lines As String() = System.IO.File.ReadAllLines(filePath)


    Dim lineQuery = From line In lines
                    Let x = line.Split(New Char() {","})
                    Order By x(0)
                    Select  x(0) & ", " & x(3) & ", " & x(5)

    System.IO.File.WriteAllLines(filePathOut, lineQuery.ToArray())

    Console.WriteLine("Spreadsheet2.csv written to disk. Press any key to exit")
    Console.ReadKey()
End Sub

3 Answers3

1

One thing to consider with the current method using linq is if there are lines that don't conform to your expected data in the text file. With that the following allows for issues e.g. line does not have expected number of columns or the line is blank.

There is more code than a one line lambda or linq statement but as indicated above to allow for use of assertion.

Imports Microsoft.VisualBasic.FileIO
Public Class Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim readFile As String =
            IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Textfile1.txt")

        Dim writeFile As String =
            IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Textfile2.txt")

        Dim Lines As New List(Of Line)

        Using reader As New TextFieldParser(readFile)

            reader.TextFieldType = FieldType.Delimited
            reader.Delimiters = New String() {","c}

            Dim currentRow As String()

            While Not reader.EndOfData
                '
                ' Feel free to discard the try/catch if so be it
                '
                Try
                    currentRow = reader.ReadFields()
                    Lines.Add(New Line(currentRow))

                Catch ex As MalformedLineException
                    ' decide how to handle
                End Try
            End While
        End Using

        IO.File.WriteAllLines(writeFile,
            Lines.OrderBy(Function(item) item.Column0) _
            .Select(Function(item) item.ItemArray).ToArray)

    End Sub
End Class
''' <summary>
''' Used to create a line item that will
''' be inserted into a List(Of Line) in the caller
''' reading a text file.
''' </summary>
''' <remarks>
''' Gave generic names to properties, we could do an
''' an anonymous type inside of Lambda or Linq and done
''' away with the class but felt this is simply another
''' option
''' </remarks>
Public Class Line
    Public Property Column0 As String
    Public Property Column1 As String
    Public Property Column2 As String
    Public Property Column3 As String
    Public Property Column4 As String
    Public Property Column5 As String
    Public Sub New(ByVal sender As String())
        If sender.Length = 6 Then
            Column0 = sender(0)
            Column1 = sender(1)
            Column2 = sender(2)
            Column3 = sender(3)
            Column4 = sender(4)
            If sender(5) = "270" Then
                Column5 = "-90"
            Else
                Column5 = "270"
            End If
        Else
            ' decide how to handle incorrect columns in a line
        End If
    End Sub
    Public Function ItemArray() As String
        Return String.Format("{0},{1},{2},{3},{4},{5}",
            Column0, Column1, Column2, Column3, Column4, Column5)
    End Function
End Class
Karen Payne
  • 4,341
  • 2
  • 14
  • 31
0

Maybe something like this will work.

Dim lineQuery = From line In lines
                    Let x = line.Split(New Char() {","})
                    Order By x(0)
                    Where x(5) = 270
                    Select x(5) = -90 And x(0) & ", " & x(3) & ", " & x(5)

A more detailed answer can be found here .

Community
  • 1
  • 1
Som Bhattacharyya
  • 3,972
  • 35
  • 54
  • This only selects records where x(5) = "270" . I needed all records but change x(5) to -90 if it was 270 – Daniel Groulx Mar 27 '16 at 18:46
  • Try this : Dim lineQuery = From line In lines Let x = line.Split(New Char() {","}) Order By x(0) Where x(5) = 270 Select x(5) = -90 And x(0) & ", " & x(3) & ", " & x(5) – Som Bhattacharyya Mar 28 '16 at 02:39
0

Did you mean to return value conditionally for the 3rd CSV column, something like this?

Dim lineQuery = From line In lines
                Let x = line.Split(New Char() {","})
                Order By x(0)
                Select  x(0) & ", " & x(3) & ", " & If(x(5) = "270", "-90", x(5))

For Reference : Is there a conditional ternary operator in VB.NET?

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137