Link to home
Start Free TrialLog in
Avatar of Dattu
DattuFlag for United States of America

asked on

Array Comparison

I have an array which consists of elemtents like 111,111,121,121,121,33,33,5,5,5,5
I need to compare each one of those. I have used aray.sort to sort them one by one.
Now, I need to compare each one of them and know how many times each times each one of them is appearing like
111=2 times
121=3
33=2
5=4 times
XML Format
<111>
<occurence=1>
<occurence=2>
</111>
<121>
<occurence=1>
<occurence=2>
<occurence=3>
</121>
<33>
<occurence=1>
<occurence=2>
</33>
<5>
<occurence=1>
<occurence=2>
<occurence=3>
<occurence=4>
</5>

Open in new window

Avatar of technofile
technofile
Flag of United States of America image

arry is your sorted arry
        Dim i, cnt, nums As Integer
 
        Dim arrycount(arry.Length - 1) As String
        nums = 1
        For i = 0 To arry.Length - 1
            cnt = 1
            If i > 0 Then
                If arry(i - 1) = arry(i) Then
                    cnt += 1
                    arrycount(nums) = (arry(i).ToString + "=" + cnt.ToString)
 
                Else
                    arrycount(nums) = (arry(i).ToString + "=" + cnt.ToString)
                    nums += 1
                    cnt = 0
                End If
            Else
                arrycount(nums) = (arry(i) + "=1")
            End If
        Next

Open in new window

Avatar of Dattu

ASKER

can you be a bit clearer. i need to write xml file in that format and i get a error at  arrycount(nums) = (arry(i) + "=1") saying arraylist is out of range.
i am using array list
Ok, well you said array.
The code below should work with an arraylist and give you an array arrycount with the strings
111=2
121=3
33=2
5=4


        Dim i, cnt, nums As Integer
 
        Dim arrycount(arry.Count - 1) As String
        nums = 0
        cnt = 1
        For i = 0 To arry.Count - 1
            If i > 0 Then
                If arry(i - 1) = arry(i) Then
                    cnt += 1
                    arrycount(nums) = (arry(i).ToString + "=" + cnt.ToString)
 
                Else
                    nums += 1
                    cnt = 1
                    arrycount(nums) = (arry(i).ToString + "=" + cnt.ToString)
                End If
            Else
                arrycount(nums) = (arry(i).ToString + "=1")
            End If
        Next

Open in new window

arry is your arralist
Avatar of Dattu

ASKER

actually as I said i Need it like
XML Format
to be more clear,
suppose there are documents with id's 111,121,33,5
and doc 111 has 2 pages
121= 3 pages
33 = 2 pages
5 = 4 pages
i need to print it as below
111 appear as parent node
and page 1 and page as child nodes
XML Format
<111>
<page=1>
< page =2>
</111>
<121>
< page =1>
< page =2>
< page =3>
</121>
<33>
< page =1>
< page =2>
</33>
<5>
< page =1>
< page =2>
< page =3>
< page =4>
</5>
Well what you are trying to do does not work with XML format it would have to be
<111>
<page=1></page=1>
<page=2></page=2>
</111>
The correct to XML format would be
<111>
<page>2</page>
</111>
That would allow you to look up the page element and get how many pages when someone reads the XML
The second format is the only readable XML format
Avatar of Dattu

ASKER

Thank For you help. I need on small favour from you.
i am getting the following error when I open the xml file in IE.

cannot view xml input using style sheet a string literal was expected but no opening quote character was found

WHn I open the xml file in notepad it appears as follows:
<?xml version=1.0 encoding=utf-8?><properties><scan_user>COMPASS</scan_user><ip_address>172.17.3.79</ip_address><web_app_number>s</web_app_number><target_system_cis>NO</target_system_cis><target_system_pelican>NO</target_system_pelican><target_system_caps>NO</target_system_caps><create_time>10/7/2009 9:55:51 AM</create_time><document verification_doc_id=\7\><page page_num=\1\><filename>7_0.tiff</filename></properties>



            Dim xmlBuilder As StringBuilder = New StringBuilder()
 
            'Dim dec As XmlDeclaration = xmlBuilder.CreateXmlDeclaration("1.0", Nothing, Nothing)
            'Dim DocRoot As XmlElement = Doc.CreateElement("Properties")
            'Doc.AppendChild(DocRoot)
 
            'Dim xmlWriter As XmlTextWriter = New XmlTextWriter(StreamWriter)
            xmlBuilder.Append("<?xml version='1.0' encoding='utf-8'?>")
 
            xmlBuilder.Append("<properties>")
 
            xmlBuilder.AppendFormat("<scan_user>{0}</scan_user>", "COMPASS")
 
            xmlBuilder.AppendFormat("<ip_address>{0}</ip_address>", clientIPAddress)
 
            xmlBuilder.AppendFormat("<web_app_number>{0}</web_app_number>", "s")
 
            xmlBuilder.AppendFormat("<target_system_cis>{0}</target_system_cis>", "NO")
            xmlBuilder.AppendFormat("<target_system_pelican>{0}</target_system_pelican>", "NO")
            xmlBuilder.AppendFormat("<target_system_caps>{0}</target_system_caps>", "NO")
 
            xmlBuilder.AppendFormat("<create_time>{0}</create_time>", DateTime.Now)
            'Dim i, cnt, nums As Integer
            ' '' ''Dim refType As String = row(4).ToString
            ' '' ''For Each Item As ASDVerificationDocumentsBO In verificationChecklist
            ' '' ''    Dim verifType As String = Item.VerificationType
            ' '' ''    If refType.Contains(verifType) Then
            ' '' ''        Item.VerificationName = row(1).ToString
            Dim folder As New System.IO.DirectoryInfo(MapPath("Images1\\") & "\" & DirName)
            Dim DocArray As New System.Collections.ArrayList
            'Dim DocArray As String
            Dim DocumentID As String = Nothing
            For Each File As System.IO.FileInfo In folder.GetFiles()
                Dim fileWithoutPath As String = Path.GetFileNameWithoutExtension(File.FullName)
                'Dim filenames As New System.Collections.ArrayList
                'filenames.Add("fileWithoutPath")
                Dim fileWithoutExtension() As String = fileWithoutPath.Split("_")
                Dim s As Integer = Nothing
                'Now check separated(0) and separated(1)
 
                DocumentID = fileWithoutExtension(0)
                DocArray.Add(DocumentID)
                DocArray.Sort()
         
            Next
            DocArray.Sort()
            Dim i, cnt, nums As Integer
            nums = 0
            cnt = 1
            For i = 0 To (DocArray.Count - 1)
 
                If i > 0 Then
                    If DocArray(i - 1) = DocArray(i) Then
                        cnt += 1
                        xmlBuilder.AppendFormat("<page page_num=\{0}\>", cnt)
                        pages = cnt - 1
                        Dim filenames As String = DocArray(i) + "_" + pages.ToString + ".tiff"
                        xmlBuilder.AppendFormat("<filename>{0}</filename>", filenames)
                    Else
                        xmlBuilder.AppendFormat("<document verification_doc_id=\{0}\>", DocArray(i).ToString)
                        nums += 1
                        cnt = 1
                        xmlBuilder.AppendFormat("<page page_num=\{0}\>", cnt)
                        pages = cnt - 1
                        Dim filenames As String = DocArray(i) + "_" + pages.ToString + ".tiff"
                        xmlBuilder.AppendFormat("<filename>{0}</filename>", filenames)
                    End If
                Else
                    xmlBuilder.AppendFormat("<document verification_doc_id=\{0}\>", DocArray(i).ToString)
                    xmlBuilder.AppendFormat("<page page_num=\{0}\>", cnt)
                    pages = cnt - 1
                    Dim filenames As String = DocArray(i) + "_" + pages.ToString + ".tiff"
                    xmlBuilder.AppendFormat("<filename>{0}</filename>", filenames)
                End If
        Next
 
 
 
            'For Each item As String In DocArray
            '    If Not DocArray.Contains(DocumentID) Then
            '        DocArray.Add(DocumentID)
            '        xmlBuilder.AppendFormat("<document verification_doc_id=\{0}\>", DocumentID)
            '        pages = 0
            '    ElseIf DocArray.Contains(DocumentID) Then
            '        pages = DocArray.LastIndexOf(DocumentID, 0, DocArray.Count)
 
            '        xmlBuilder.AppendFormat("<page page_num=\{0}\>", pages)
            '        Dim filenames As String = File.FullName.ToString
            '        xmlBuilder.AppendFormat("<filename>{0}</filename>", filenames)
 
            '    End If
 
            'Next
 
            xmlBuilder.Append("</properties>")
 
 
            Dim xmlFile As StreamWriter = New StreamWriter(MapPath("Images1\\" + DirName) & "\" & "Properties.xml")
 
            xmlFile.WriteLine(xmlBuilder.ToString())
            'xmlFile.Save(MapPath("Images1\\" + DirName) & "\" & "Properties.xml")
 
 
            xmlFile.Close()
 
 
        End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of technofile
technofile
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaufmed
If this is .NET 3.0 and up, the you could use Linq:
Dim arr() As Integer = {111, 111, 121, 121, 121, 33, 33, 5, 5, 5, 5}
 
Dim var = From i As Integer In arr _
          Group By i Into g = Group _
          Select New With {.Item = i, .Count = g.Count()}
 
For Each i In var
    Console.WriteLine("{0}={1} times", i.Item, i.Count)
Next

Open in new window

Avatar of Dattu

ASKER

SO,can you tell me how do ii do to get the structure you told. I tried couple of ways. I am getting page and filenames but document node incorrect. can you tell me exactly where i need to put the closing tag for document.
You should really open a new question since this isn't specific to your original question: "Array Comparison"