Solved

Compare 2 string arrays?

Posted on 2006-10-25
4
408 Views
Last Modified: 2010-04-23
Hello

I have 2 string arrays with the following data format in every item:

FILENAME>DATE

Now I want to compare them. As result I want to have one array containing the following:

If FILENAME is in both arrays, but the datetime is different         (Then I want to have: "<FILENAME" if datetime is bigger in first array, else ">FILENAME")
if FILENAME is only in one array                                               ("<FILENAME" if it is in array 1, ">FILENAME" if its in array 2")

How can I do that?

Thanks

Sven
0
Comment
Question by:Ruttensoft
4 Comments
 
LVL 3

Expert Comment

by:GeneM
ID: 17808877
Sven,
Can you post some actual entries from the arrays.  I am especially interested in how the dates are formatted.  
GeneM
0
 
LVL 12

Expert Comment

by:sandip132
ID: 17808949
Split each element of both arrays. Compare the FileName and date values of the splitted values Also look for your condiations get the resulted array....


Refer : http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_20999009.html

Happy Programming.
0
 
LVL 5

Accepted Solution

by:
xersoft earned 500 total points
ID: 17813137
Sven,

Here is some code that will do what you want. I suggest you spend some time cleaning it up however.
For example I don't like how ToString on the FileDates object returns empty string when the dates are the same...
Written for 2.0 but could be 1.0 with some casting instead of the generics.
I am only assuming I understood the inputs correctly.
I think the output is correct, but double check.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dates1 As New List(Of String)
        Dim dates2 As New List(Of String)
        Dim output As New List(Of String)

        'fill some input dates
        dates1.Add("FileBoth.txt>4-13-1981")
        dates2.Add("FileBoth.txt>4-19-1981")
        dates1.Add("FileList1.txt>4/26/1981")
        dates2.Add("FileList2.txt>4-1-1981")


        'get the output
        output.AddRange(Process(dates1.ToArray, dates2.ToArray))

        'put the output into a string so we can see it
        Dim message As String = String.Empty
        For Each itm As String In output
            message &= itm & System.Environment.NewLine
        Next

        'show the output
        MessageBox.Show(message, "Output", MessageBoxButtons.OK)
    End Sub
    Private Function Process(ByVal Input1() As String, ByVal Input2() As String) As String()
        Dim Files As New Dictionary(Of String, FileDates)
        Dim rg As New System.Text.RegularExpressions.Regex("([^>]+) *> *(.+)")

        'process first array
        For Each input As String In Input1
            Dim m As System.Text.RegularExpressions.Match = rg.Match(input)
            If m.Groups.Count = 3 Then
                Dim FileName As String = m.Groups.Item(1).Value
                Dim FileDate As String = m.Groups.Item(2).Value

                Dim fd As New FileDates(FileName)
                fd.Date1 = FileDate
                Files.Add(FileName, fd)
            End If
        Next

        'process second array
        For Each input As String In Input2
            Dim m As System.Text.RegularExpressions.Match = rg.Match(input)
            If m.Groups.Count = 3 Then
                Dim FileName As String = m.Groups.Item(1).Value
                Dim FileDate As String = m.Groups.Item(2).Value

                If Files.ContainsKey(FileName) Then
                    Files.Item(FileName).Date2 = FileDate
                Else
                    Dim fd As New FileDates(FileName)
                    fd.Date2 = FileDate
                    Files.Add(FileName, fd)
                End If
            End If
        Next


        'process results
        Dim Output As New List(Of String)

        For Each itm As KeyValuePair(Of String, FileDates) In Files
            Dim s As String = itm.Value.ToString
            If Not String.IsNullOrEmpty(s) Then
                Output.Add(s)
            End If
        Next

        Return Output.ToArray
    End Function
    Public Class FileDates
        Public FileName As String
        Public Date1 As String = String.Empty
        Public Date2 As String = String.Empty


        Public Sub New(ByVal inFileName As String)
            FileName = inFileName
        End Sub


        Public Overrides Function ToString() As String
            'ensure we have both dates
            If Not IsDate(Date1) Then
                Return ">" & FileName
            End If
            If Not IsDate(Date2) Then
                Return "<" & FileName
            End If

            'process both dates
            Dim dt1 As DateTime = CDate(Date1)
            Dim dt2 As DateTime = CDate(Date2)

            If dt1 = dt2 Then
                Return ""
            ElseIf dt1 > dt2 Then
                Return "<" & FileName
            Else
                Return ">" & FileName
            End If
        End Function
    End Class
0
 

Author Comment

by:Ruttensoft
ID: 17813736
Thanks alot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now