?
Solved

Add Items into different arrays based on string content

Posted on 2009-04-17
6
Medium Priority
?
167 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:abenage
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24168156
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
 
LVL 1

Author Comment

by:abenage
ID: 24168356
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24168667
What version VB.Net are you working with?...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24168893
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
 
LVL 39

Expert Comment

by:abel
ID: 24168913
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
 
LVL 1

Author Comment

by:abenage
ID: 24185065
Thanks a lot.  That is exactly what I'm looking for.  I appreciate the help!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

809 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