Solved

Compare 2 string arrays?

Posted on 2006-10-25
4
404 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks alot
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

15 Experts available now in Live!

Get 1:1 Help Now