Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Find and count commas in array

Hello,

I'm trying to count the number of commas an array has in it. I do know that the array has commas in it, but I keep running into the problem that it doesn't count the commas. What can I do to get it to count the commas?

Thanks
0
Joan_Held
Asked:
Joan_Held
  • 4
  • 3
  • 3
2 Solutions
 
Shaun KlineLead Software EngineerCommented:
You could join the array, say on the pipe character (|) and then split the array on the comma. The number of commas would be the length minus 1.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Other than that, iterate over each element, Split() each item as Shaun_Kline suggests, and keep a running total.
*You can also use a While loop and IndexOf().

Can you shows us how your Array is declared along with some examples of its contents?...
0
 
Joan_HeldAuthor Commented:
The text file it's coming from has a header, list of variables, and then numbers. Lots of numbers.
Ex:
blah blah header
 ,var
 ,var
 ,var
 
     123   5495  56034  24  59
  3958  4859  959
and those two lines repeated until the end with different numbers.
ofdOpenit.InitialDirectory = "S:\"
        ofdOpenit.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        ofdOpenit.FilterIndex = 2
        ofdOpenit.RestoreDirectory = True
        ofdOpenit.ShowDialog()
        Dim path As String = ofdOpenit.FileName
        Dim UnsortedArray() As String = File.ReadAllLines(path)

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Shaun KlineLead Software EngineerCommented:
So, if you are trying to count the commas, this should work:
Dim CommaCount as long = UnsortedArray.Join("|").Split(",").Length
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...so you want to skip the first line, then count how many consecutive lines after that begin with commas?
0
 
Joan_HeldAuthor Commented:
Yes, that is what I am trying to do.
0
 
Shaun KlineLead Software EngineerCommented:
The String.Join method has an overloaded method that allows you to specify where in the array that you want to start the join operation:

Dim joinedValue as string
String.Join(joinedValue, UnsortedArray, 1, UnsortedArray.length - 2)
(http://msdn.microsoft.com/en-us/library/tk0xe5h0.aspx)
This would allow you to skip the header line in your array.
Then you can do the split on the comma.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can either try to do it in one shot like Shaun proposes, or go line by line and count them.  Which is better really depends on how you need to manipulate the data...
0
 
Joan_HeldAuthor Commented:
I original wanted to go through each element and count them, but see as I have be trying to do that for three days now I will take what I can get.
0
 
Joan_HeldAuthor Commented:
So, I finally go it to work.  
Dim UnsortedArray() As String = File.ReadAllLines(path)
        Dim CommaCount As Long = Join(UnsortedArray, ",").Split(",").Length
        MessageBox.Show(CommaCount)
        MessageBox.Show(UnsortedArray.Length)
        Dim Var As Integer
        Var = CommaCount - UnsortedArray.Length + 1
        MessageBox.Show(Var)

Open in new window

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now