Add Items into different arrays based on string content

How can I take an existing arraylist of strings, and depending on the content of said strings, group them into their own arraylist?

The strings are file names with a naming convention:
<ItemTypeCode>-<BagNumber>-<ItemNumber>-<Date>-<UniqueLetter>.jpg

For example:
The arraylist contains 8 items
c-4-101-09152009-a.jpg
c-4-101-09152009-b.jpg
c-4-101-09152009-c.jpg
c-4-101-09152009-d.jpg
c-4-102-09152009-a.jpg
c-4-102-09152009-b.jpg
c-4-102-09152009-c.jpg
c-4-102-09152009-d.jpg

What I want to do is put the items having a ItemNumber of 101 into one array and have items with a ItemNumber of 102 in its own, separate array.

I am able to use the split() function to get the individual parts of the filename.  I have attached the code that I am using for that.

Private Sub CreateDescription_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim FileNames As ArrayList = SelectionForm.prpImageFilenames
        For Each PictureFile As FileInfo In FileNames
            Dim AllFiles As Object = Split(PictureFile.Name, "-")
            Dim ItemPrefix As String = AllFiles(0)
            Dim ItemBagNumber As String = AllFiles(1)
            Dim ItemNumber As String = AllFiles(2)
            Dim ItemDate As String = AllFiles(3)
            Dim ItemPicSequence = AllFiles(4).Remove(1, 4)
 
        Next
    End Sub

Open in new window

LVL 1
abenageAsked:
Who is Participating?
 
abelCommented:
The following example grows automatically. It uses the ItemNumber as the key in a dictionary object. If it is exists, it adds to the arraylist that belongs to that key. If it does not exist, it add a new empty arraylist and adds the FileInfo object to that arraylist as well.

Dim FileNames As New ArrayList
FileNames.Add(New FileInfo("c-4-101-09152009-a.jpg"))
FileNames.Add(New FileInfo("c(-4-101-9152009-a.jpg)"))
FileNames.Add(New FileInfo("c(-4-101-9152009-b.jpg)"))
FileNames.Add(New FileInfo("c(-4-101-9152009-c.jpg)"))
FileNames.Add(New FileInfo("c(-4-101-9152009-d.jpg)"))
FileNames.Add(New FileInfo("c(-4-102-9152009-a.jpg)"))
FileNames.Add(New FileInfo("c(-4-102-9152009-b.jpg)"))
FileNames.Add(New FileInfo("c(-4-102-9152009-c.jpg)"))
FileNames.Add(New FileInfo("c(-4-102-9152009-d.jpg)"))
 
Dim AssortedDict As New Dictionary(Of String, ArrayList)
 
 
For Each PictureFile As FileInfo In FileNames
    Dim AllFiles As Object = Split(PictureFile.Name, "-")
    Dim ItemPrefix As String = AllFiles(0)
    Dim ItemBagNumber As String = AllFiles(1)
    Dim ItemNumber As String = AllFiles(2)
    Dim ItemDate As String = AllFiles(3)
    Dim ItemPicSequence = AllFiles(4).Remove(1, 4)
 
    If Not AssortedDict.ContainsKey(ItemNumber) Then
	AssortedDict.Add(ItemNumber, New ArrayList())
    End If
    AssortedDict.Item(ItemNumber).Add(PictureFile)
Next

Open in new window

0
 
abelCommented:
Here you go:


Private Sub CreateDescription_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim FileNames As ArrayList = SelectionForm.prpImageFilenames
        Dim FileNames101 As New ArrayList()
        Dim FileNames102 As New ArrayList()
        
        For Each PictureFile As FileInfo In FileNames
            Dim AllFiles As Object = Split(PictureFile.Name, "-")
            Dim ItemPrefix As String = AllFiles(0)
            Dim ItemBagNumber As String = AllFiles(1)
            Dim ItemNumber As String = AllFiles(2)
            Dim ItemDate As String = AllFiles(3)
            Dim ItemPicSequence = AllFiles(4).Remove(1, 4)
 
            If ItemNumber = "101" Then
                FileNames101.Add(PictureFile)
            ElseIf ItemNumber = "102" Then
                FileNames102.Add(PictureFile)
            End If
        Next
    End Sub

Open in new window

0
 
abenageAuthor Commented:
OK.  I see how that works, and I guess I didn't really expound enough.  I will never know how many ItemNumbers there will be, nor what they are called.  The arrays will need to be dynamically created, and the number of arrays will be the number of different ItemNumbers which exist.

I apologize for the lack of info in my original post.

Aaron
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net are you working with?...
0
 
abelCommented:
If you want to search through all items that belong to a certain ItemNumber, you can use

   Dim myArrayListFor102 As ArrayList = AssortedDict.Item("102")

If you want to know how many different itemnumbers there are, you can do this:

   Dim intAmountItemNumbers as Integer = AssortedDict.Keys.Count

etc...
0
 
abenageAuthor Commented:
Thanks a lot.  That is exactly what I'm looking for.  I appreciate the help!!
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.