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
Solved

Compare 2 string arrays?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

860 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