Avatar of Joan_Held
Joan_Held
Flag for United States of America asked on

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
.NET Programming

Avatar of undefined
Last Comment
Joan_Held

8/22/2022 - Mon
Shaun Kline

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 Tomlinson

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?...
Joan_Held

ASKER
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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Shaun Kline

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Tomlinson

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

ASKER
Yes, that is what I am trying to do.
SOLUTION
Shaun Kline

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mike Tomlinson

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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Joan_Held

ASKER
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.
Joan_Held

ASKER
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