?
Solved

VB.NET Parsing Number Combinations

Posted on 2009-02-10
4
Medium Priority
?
664 Views
Last Modified: 2012-05-06
Hi All,

I'm using vb.net 2008. I have an array with numbers seperated by spaces. Example array lines:
Line 1: 34 644 33 232 55 435 344 2234 234 343 222 54545 2332 324234 4 5 4545 4 4 454545 44533 334334 33 22 334 435
Line 2: 34
Line 3: 34 644
Line 4: 34 644 33 232 55 435 344 2234 234 343 222 54545 2332 324234 4 5 4545 4 4 454545 44533 334334 33 22 334 435 34 644 33 232 55 435 344 2234 234 343 222 54545 2332 324234 4 5 4545 4 4 454545 44533 334334 33 22 334 435
There could be upward of 50 numbers seperated by spaces in each line of the array.
My goal is to get them into a database and count the frequency of each number combination from 1-10.
The problem I'm having is trying to figure out how to effeciently seperate the number combinations.

Example 1 number combinations for Line 1:
34
644
33
Ect...


Example 2 number combinations for Line 1:
34 644
644 33
33 232
Ect...

Example 3 number combinations for Line 1:
34 644 33
644 33 232
33 232 55
Ect...

And all the way up to 10 number combinations. What would be the best way to do this?

My thought is something like a seperate do loop for 1 - 10 combinations.
Example for parsing 2 number combinations:
Dim MyArray As Array = MyNumberLineArray(Integer).ToString.Split(" ")
IntegerTwo = 0
Do Until IntegerTwo = MyArray.Length - 1
      InsertCommandToUpdateDataBase Where Feild = MyArray(IntegerTwo) & MyArray(IntegerTwo + 1)
      IntegerTwo = IntegerTwo + 1
Loop

Is there an easier way to do this? Any help would be greatly appreciated.
0
Comment
Question by:kanus
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
Thunder724 earned 2000 total points
ID: 23605273
If i'm understanding the question correctly....

For each line in file 'sudo code, iterate through each line as you like/need.
  DIM Numb() As String = Split(<linedata>," ")
  For x as Int16 = 0 To UBound(Numb) -1
     InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
  Next
next
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23605848
Hi kanus;

You state that, "My goal is to get them into a database and count the frequency of each number combination from 1-10. ", If you are looking to get the frequency of each value in each line VS 2008 can do this without the use of the database. See code snippet below.

Fernando
' Your string data
Dim myArray(3) As String
myArray(0) = "34 644 33 232 55 435 344 2234 234 343 222 54545 2332 324234 4 5 4545 4 4 454545 44533 334334 33 22 334 435"
myArray(1) = "34"
myArray(2) = "34 644"
myArray(3) = "34 644 33 232 55 435 344 2234 234 343 222 54545 2332 324234 4 5 4545 4 4 454545 44533 334334 33 22 334 435 34 644 33 232 55 435 344 2234 234 343 222 54545 2332 324234 4 5 4545 4 4 454545 44533 334334 33 22 334 435 "
 
' Used to display results
Dim output As String = ""
' Used to hold the string values as integer
Dim myList As New List(Of Integer)()
' Using Linq to order and group all like values
Dim freq = From n In myList _
           Order By n _
           Group n By n Into gp = Group _
           Select gp
 
' Execute the Linq query and display the results
For Each strNum In myArray
    ' Parse each line into its individual string values
    For Each strVal As String In strNum.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
        ' Convert to an integer and place into a List object
        myList.Add(CType(strVal, Integer))
    Next
    ' Execute the Linq query with the values in myList
    For Each n In freq
        ' Place the results in to the string in the format of ( X : Y ) where X is the value and Y 
        ' is the Frequency of that value
        output &= "( " & n(0).ToString() & " : " & n.Count & ") "
    Next
    ' Display a single array line
    MessageBox.Show(output)
    ' Clear the output for the next line
    output = String.Empty
    ' Clear the list for the next query
    myList.Clear()
Next

Open in new window

0
 

Author Comment

by:kanus
ID: 23607156
Thank you Thunder, That makes it a bit easier to implement and I dont have to worry about if some of the lines dont have 10 numbers in  the line, it will skip that part. Here is what I did to implement.

                    Dim N() As String = Split(FTSplit(b), " ")
                    '1 Number Combinations
                    For x As Int16 = 0 To UBound(N)
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x))
                    Next
                    '2 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 1
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1))
                    Next
                    '3 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 2
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2))
                    Next
                    '4 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 3
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3))
                    Next
                    '5 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 4
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3) & " " & N(x + 4))
                    Next
                    '6 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 5
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3) & " " & N(x + 4) & " " & N(x + 5))
                    Next
                    '7 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 6
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3) & " " & N(x + 4) & " " & N(x + 5) & " " & N(x + 6))
                    Next
                    '8 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 7
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3) & " " & N(x + 4) & " " & N(x + 5) & " " & N(x + 6) & " " & N(x + 7))
                    Next
                    '9 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 8
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3) & " " & N(x + 4) & " " & N(x + 5) & " " & N(x + 6) & " " & N(x + 7) & " " & N(x + 8))
                    Next
                    '10 Number Combinations
                    For x As Int16 = 0 To UBound(N) - 9
                        'InsertCommandToUpdateDataBase Where Feild = Numb(x).Tostring
                        MsgBox(N(x) & " " & N(x + 1) & " " & N(x + 2) & " " & N(x + 3) & " " & N(x + 4) & " " & N(x + 5) & " " & N(x + 6) & " " & N(x + 7) & " " & N(x + 8) & " " & N(x + 9))
                    Next

Fernando, your solution may have fit my need if I didnt have an extremely large amount of data parse.

Thank you.  
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23608159
@ kanus;

This For Each loop is the only parsing of the strings

    For Each strVal As String In strNum.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
        ' Convert to an integer and place into a List object
        myList.Add(CType(strVal, Integer))
    Next

The Linq query only processes the data to get the results you wanted without duplicating code.
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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

809 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