Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find and count commas in array

Posted on 2010-11-12
10
Medium Priority
?
404 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
0
Comment
Question by:Joan_Held
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 34120884
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34121108
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
 

Author Comment

by:Joan_Held
ID: 34121231
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 27

Accepted Solution

by:
Shaun Kline earned 200 total points
ID: 34121337
So, if you are trying to count the commas, this should work:
Dim CommaCount as long = UnsortedArray.Join("|").Split(",").Length
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34121354
Ok...so you want to skip the first line, then count how many consecutive lines after that begin with commas?
0
 

Author Comment

by:Joan_Held
ID: 34121366
Yes, that is what I am trying to do.
0
 
LVL 27

Assisted Solution

by:Shaun Kline
Shaun Kline earned 200 total points
ID: 34121579
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34121876
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
 

Author Comment

by:Joan_Held
ID: 34121915
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
 

Author Comment

by:Joan_Held
ID: 34122834
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

596 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