parsing a comma delimited field

I have a field in a table that will contain for example:

33:N/A,32:3G,202767:4G,202768:LTE,202766:2G,202764:Non-3G,

The user will be passing me a parameter say for example LTE.  Then  I will need to grab the value associated with LTE which in this example would be  202768 and write this value to the database..

How would I parse out this field to pull out the value that matches the parameter I am passed.  
sherbug1015Asked:
Who is Participating?
 
Jacques Bourgeois (James Burger)PresidentCommented:
Dim line As String = "33:N/A,32:3G,202767:4G,202768:LTE,202766:2G,202764:Non-3G,"
Dim data() As String
Dim position As Integer
Dim result As Integer
Dim parameter As String = "LTE"

data = line.Split(","c)
parameter = ":" & parameter

For Each s As String In data
   position = s.IndexOf(parameter)
   If position > 0 Then
      result = CInt(s.Substring(0, position))
   Exit For
End If

Open in new window

           Next
0
 
Patrick MatthewsCommented:
If the value is always a string of digits, then your pattern should be:

(\d+):(insert parameter here)

and then take the 0th submatch.
0
 
sherbug1015Author Commented:
Can you give me an example?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Patrick MatthewsCommented:
Are you doing this in VBScript, or in VB.Net?  The syntaxes are different.
0
 
sherbug1015Author Commented:
In a VB.Net app
0
 
nepaluzCommented:
here goes
Dim Mystring As String() = "33:N/A,32:3G,202767:4G,202768:LTE,202766:2G,202764:Non-3G".Split(",")
Dim xDict As New Dictionary(Of String, String)
Array.ForEach(Mystring, Sub(x As String) xDict.Add(x.Split(":").Last, 

Open in new window

x.Split(":").First))
To get the value for LTE do
Dim LTE_Value = xDict.Item("LTE")

Open in new window

0
 
nepaluzCommented:
Sorry, that came out a bit wonky .... here goes:
Dim Mystring As String() = "33:N/A,32:3G,202767:4G,202768:LTE,202766:2G,202764:Non-3G".Split(",")
Dim xDict As New Dictionary(Of String, String)
Array.ForEach(Mystring, Sub(x As String) xDict.Add(x.Split(":").Last, x.Split(":").First))

Open in new window

To get the value for LTE do
Dim LTE_Value = xDict.Item("LTE")

Open in new window

0
 
sherbug1015Author Commented:
Thank you.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
@nepaluz

I do not want to criticize (is that how it is spelled?), just help you become a better programmer. You did something that too many programmers do these days, disregarding arrays and always using collections.

Don't get me wrong. Collections are wonderful, and I use 19 collections for each array I use in my own coding. But for simple things, arrays take up less ressources and are a lot faster to work with.

Specially when you have methods in the framework that can do the job you are seeking to do (Split) and they return an array. By the way, why do you thing Split returns an array instead of a collection? And why convert into a collection something that is already an array?
0
 
nepaluzCommented:
Concise code is always easier to understand and follow. With regard to the overhead of collections versus arrays, that is up to the person using the code to optimize or leave as is.

And honestly, I do not see what the fuss is about. Can you get some performance metrics on the two above? I bet it is negligeable.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Basic computer stuff.

-----

For the speed:

Array elements are stored sequentially in the system, and do not require pointers to move from one element to another.

Collections (most of them) are linked lists. The elements are linked together by pointers that the system needs to navigate in order to get to an element. So navigation takes a lot longer.

Collections will be a better choice if you do a lot of Add and Remove while you are working with them, because you do not have to Redim everytime (Redim is bad on performance). That is why stuff such as DataTables, grids, Excel sheets and Word documents are built as a collection.

But if you are working with a fixed set of data, array are usually better as far as performance is concerned.

And since Split returns an array, I am sure that getting the result and keeping it in an array is faster than building a collection out of the array.

Sure, in a small thing such as the code we are discussing here, you would be able to measure the performance difference. But if you take the good habit of always using the best performer for a given situation, even if it is insignificant, you develop good reflexes that can often prevent you to have to deal with performances problems later on.

-----

For resources:

Arrays contain but one thing, the Data.

Collections, specially when you have a String for the key, need for each element the Data + the key + at least one pointer to link to the following element in the collection. And in many cases, when you use a String (depends on how the collection is implemented), they also need a lookup table to be able to know that you need to retrieve element 3 when "foo" is requested.

You easily double or triple the amount of memory used.

Once again, not a big deal in the current situation.

Once again, developing good reflexes is a good habit because who knows if the collection that holds 3 elements today won't grow up to 3000 in a year.

-----

Concise code is not always easier to understand. In fact, it is most often the reverse

Present this to somebody who does not have the same coding style as you have, specially if he is a newbie as it is often the case here at Expert-Exchange:
Dim output Trace.Listeners.Add(New TextWriterTraceListener(New IO.StreamWriter("PhotoCat.trace", True)))

Open in new window

Now present it this way:
Dim traceFile As New IO.StreamWriter("PhotoCat.trace", True)
Dim listener As New TextWriterTraceListener(traceFile)
Trace.Listeners.Add(listener)

Open in new window

I bet you that if you present both pieces of code to a group of programmers who ask the types of questions we receive here, most would rather choose the second one as the easiest to understand.

Another example. I know that Options Strict should be on, but a lot if not most programmers do not even know about that option and could see the following:
'Given the following
Dim x As Integer = 10
'Here is the concise code:
If x Then Beep
'Here is the long code:
If x<>0 Then Beep

Open in new window

Once again, a very large number of programmers would not understand what you are trying to achieve in the concise code. Anybody who has programmed for a week understands the less concise one.

0
 
nepaluzCommented:
James, thanks for the verbose response. I took a post-grad certificate in education (secondary) but chose not to pursue the calling, and my answers reflect that to an extent. I myself being a self taught programmer (barely 3 years from scratch!), I always assume that on these pages, people have a good (better than mine) understanding of VB.net. In those circumstances, concise code IS easier to follow. You clearly dis-agree and I have no issues with that. In the same breath I'll add that I find no gain in splitting hairs, after-all, users here select the answer they prefer (and just in case you missed it, in this case mine was NOT chosen!). EOF!
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
As a trainer with over 30 years in programming, there are 2 things I find important:

1. My experience as a programmer. To be a good programmer, you need to understand what code does. Simply pickup a solution over the other because it looks better does not help you to grow as a programmer. That is why I almost always take time to explain the whys when I help somebody. Some are here only to pick code fast, but many are here to learn.

2. My experience as a trainer. It is very hard to judge the level of a programmer, specially when you are not speaking directly with them, specially here where many people (that includes me), do not speak English as their main language. Unless it is very clear by the way they ask their question that they are of an advanced level, one must always assume that they are if they are here, they are learning .NET. And even if the programmer asking the question is a pro, a lot of other programmers that might not be of the same level but who are having the same problem are reading also reading those posts. Even advanced programmers that move from VB6 to VB.NET have a hard time coping at the beginning.
0
 
nepaluzCommented:
Of all places, I assumed EOF would stand for something at EE. Nevertheless, that I chose not to pursue the teaching calling, my experience as a trainer will remain, well ...., nothing actually. That said, feel free to explain as verbosely a possible when I ask a question here (and I do ask sometimes), but I find dissecting my contribution / responses to queries here rather patronising to an un-comfortable level. And given that I have no intention of persuing a career in teaching any more, Iwould rather you kept those comments / opinions to yourself. EndOfData
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Sorry, I was not aware that teaching was in your background and did not want to antagonize you. I simply am someone who like things to be straight.

Once again, sorry if I offended you in any way.
0
 
nepaluzCommented:
The way you like your things, straight, bent left or right, simply your choice and honestly a bit too much detail for my liking. Whichever way you like "your things", feel free to keep liking them in your very own privacy.

And no, you did not antagonise me at all. Just thought I'd put my opinion accross to you, just in case you wrongly assumed that it is only you who's got one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.