We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

VB.NET Parsing Number Combinations

kanus
kanus asked
on
Medium Priority
678 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.
Comment
Watch Question

Senior SQL DBA
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Author

Commented:
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.  
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
@ 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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.