We help IT Professionals succeed at work.

Find and count commas in array

Joan_Held
Joan_Held asked
on
419 Views
Last Modified: 2013-11-26
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
Comment
Watch Question

Shaun KlineLead Software Engineer
CERTIFIED EXPERT

Commented:
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.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

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

Author

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

Lead Software Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Ok...so you want to skip the first line, then count how many consecutive lines after that begin with commas?

Author

Commented:
Yes, that is what I am trying to do.
Shaun KlineLead Software Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

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

Author

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.

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*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.