Solved

parsing a comma delimited field

Posted on 2011-09-27
16
282 Views
Last Modified: 2012-05-12
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.  
0
Comment
Question by:sherbug1015
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36710836
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
 

Author Comment

by:sherbug1015
ID: 36710848
Can you give me an example?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36710875
Are you doing this in VBScript, or in VB.Net?  The syntaxes are different.
0
 

Author Comment

by:sherbug1015
ID: 36710886
In a VB.Net app
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36711027
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36711034
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36711046
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
 

Author Closing Comment

by:sherbug1015
ID: 36711531
Thank you.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40
ID: 36711814
@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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36711944
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
 
LVL 40
ID: 36712499
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36712710
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
 
LVL 40
ID: 36712863
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36713156
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
 
LVL 40
ID: 36713349
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36713500
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now