?
Solved

Compare 2 string arrays?

Posted on 2006-10-25
4
Medium Priority
?
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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