|
-
Oct 6th, 2011, 12:57 PM
#1
Thread Starter
Addicted Member
Sort into Piles?
It's a simple problem, but cant find the answer. I'm pulling in file names. The file names are 123A, 123B, 124A, 125A. There can be multiple files with the same number, but different letters (like part 123, revision A, revision B, etc). They all come in as a single 1-dimensional string array. I need to place all number 123's in the same array (or list), so I can pass all files with the same number over to the next function as a whole.
How do I create a "bin" with a "label" so I know if the name goes in that bin or if the bin even exist yet? Like file "123A", do I have a "bin" for 123? No, so make a bin, and add 123A to the bin. Now number "123B". Do I have a "bin" 123? yes, add "123B" to that bin. Next number "124A". Do I have a bin "124"? No, make bin, add number to bin.
I was thinking about making a bin object, but how do I efficiently see if my list of bins has a "123" without polling each bin and asking "are you bin "123"?
-
Oct 6th, 2011, 01:38 PM
#2
Re: Sort into Piles?
Code:
Dim bins As New Dictionary(Of String, List(Of String))
Dim re As New System.Text.RegularExpressions.Regex("^\d+", System.Text.RegularExpressions.RegexOptions.Compiled)
For Each s As String In TheFileNames 'An enumerable of strings
Dim baseName As String = re.Match(s).Value 'Might want to add error-handling, but this assumes it's already gone through checking or is able to fail
If Not bins.ContainsKey(baseName) Then bins.Add(baseName, New List(Of String))
bins(baseName).Add(s)
Next
Voilà, bins by base filename. For example, 123A would be in bins("123"). You can loop through the keys using the Keys property, or even do keys and values at once.
-
Oct 6th, 2011, 01:57 PM
#3
Thread Starter
Addicted Member
Re: Sort into Piles?
minitech - I think I see whats going on here. Your making the name the key for each bin... This is exactly what I wanted... Thanks!
But Im not sure whats going on here:
"Dim re As New System.Text.RegularExpressions.Regex("^\d+", System.Text.RegularExpressions.RegexOptions.Compiled)".
Whats it doing?
-
Oct 6th, 2011, 02:01 PM
#4
Re: Sort into Piles?
It's a regular expression that captures the digits (\d+) at the beginning (^) of the filename. The System.Text.RegularExpressions.Compiled part isn't necessary, but it will speed up the matching. Google "regular expressions" and you'll see what I mean. They're useful for a lot of things. I could have used Substring and Chars, but this way is more open to changes in filename structure.
-
Oct 6th, 2011, 04:30 PM
#5
PowerPoster
Re: Sort into Piles?
Indeed. Regex is used for pattern matching and is preferred rather than doing hack jobs/string manipulation etc... and is more efficient than doing the string "hacks". Pattern matching, as also stated, is exposed to you for more options - i.e you can just give it a pattern and it will match it, otherwise the other way using strings/parsing means you need to keep re-writing the code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|