3 Replies - 238 Views - Last Post: 23 October 2017 - 03:03 AM Rate Topic: -----

#1 james27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-May 12

compare each element in list and combine result

Posted 22 October 2017 - 10:05 PM

hello i stuck somewhere in my coding.. please help me

i have a text file contains following

A,B,C,
B,D,
E,C,D,A,
i want to find duplicate element word in a text file then

save to another text file.

for instance start to compare A word with another lines

B,D and E,C,D,A,

and after finished A word compare with all other line

then goes next word B and compare all other lines

and C and next lines etc...

if found A word in another line which in this case third line i want combine first line and third line and save result like following

A,B,C,E,C,D,A,

i have error index related but difficult find where is wrong.

    dim lines as New List(Of string)
    dim linesfile() as string = File.ReadAllLines("temp333nodu.txt")
    dim lineVal() as string
    for each line in linesfile
        lines.Add(line)
    Next
    using saveres as New StreamWriter("finaldic.txt")
        for iii = lines.Count - 1 to 0 step - 1
            lineval = lines(iii).Split(",")
            for each value in lineVal
                if Not value = ""
                    If lines(iii).Contains(value)
                        Try
                            iii -= 1
                            saveres.WriteLine(lines(iii) & lineval.tostring())
                            lines.RemoveAt(iii)
                        Catch ex As Exception
                            Console.WriteLine("some error")
                        End Try

                    End If
                End If
            Next
        Next
        saveres.Close()
    End Using


Is This A Good Question/Topic? 0
  • +

Replies To: compare each element in list and combine result

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: compare each element in list and combine result

Posted 22 October 2017 - 11:25 PM

What error? On what line?
Was This Post Helpful? 0
  • +
  • -

#3 james27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-May 12

Re: compare each element in list and combine result

Posted 23 October 2017 - 02:54 AM

View Postandrewsw, on 23 October 2017 - 06:25 AM, said:

What error? On what line?

hello
i have error in this line and error is index out of range error
saveres.WriteLine(lines(iii) & lineval.tostring())
thanks !

This post has been edited by james27: 23 October 2017 - 02:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: compare each element in list and combine result

Posted 23 October 2017 - 03:03 AM

At a glance, you have a for loop that is decrementing iii, but also decrement it within the loop, so I suspect that the value is probably reaching -1, which is not a valid index to lines().

You have the advantage, though, as you can step through your code and at least discover what value of iii is causing the error. There is a debugging tutorial linked in my signature below.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1