Solved

Compare 2 string arrays?

Posted on 2006-10-25
4
412 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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