[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

parse string of id's for number of occurances

Posted on 2011-09-20
15
Medium Priority
?
371 Views
Last Modified: 2012-06-27
Experts,

This was my first attempt at this question, I should have tried to use some of the offered approaches instead of my own:

http://www.experts-exchange.com/Programming/Languages/.NET/Q_27291428.html

I have a string that looks like the following, let's call it strCards = "1,1,2,2,2,3,1,3,34,34,34,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99,34,34"

These are card_ids. the cards are listed like this (I'd like to include this data within the app, I don't want to make any db calls during this routine.):

id       Card Name
1          cardone
2          cardtwo
3          cardthree
4          cardfour
5          cardfive
6          cardsix
etc

I need to be able to parse the strCard and list the CardName and the number of times that card appears in the string in a listbox, so:

cardone          6
cardtwo          3
cardthree        2
cardfour          3
cardsix            3

and so on...

these seem to be the start of what i'm looking for, but i don't understand how to implement\get them going:

from x77:

You can use a Dictionary(of string,Integer) where Id is strin(key), Count is integer

  dim  sep As Char() = New Char() {","c}
  dim cnt as integer
  for each v as string in strCardIDs.split(sep,StringSplitOptions.RemoveEmptyEntries)
      if myDict.TryGetValue(v,cnt) then
         cnt += 1
         myDict(v) = cnt
      else
         MyDict.Add(v,1)
      end if
  next

Next enumerate the dictionary (Values, Count) to list values.

from EaswaranP (unfortunately, I need this in VB.NET):

List<int,string> objList = new List<int,string>();

objList.Add(1,"CardOne");
objList.Add(2,"CardTwo");
and so on...

string[]  keys = strCardIDs.split(",".ToCharArray());
List<int,string> objListRest = new List<int,string>();
int val;
for each (string str in  keys )
{
   int key = Convert.Toint32(str);
   
   if(objListRest.ContainsKey(key))
{
    val = objListRest[key];
    val ++;
objListRest[key] = val;
}
else
  objListRest[key] = 1;

}


0
Comment
Question by:crafuse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36565831
How about this?

            List<int> idList = "1,1,2,2,2,3,1,3,34,34,34,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99,34,34".Split(",".ToCharArray()).ToList().ConvertAll(c => { return Convert.ToInt32(c); }).ToList<int>();
            idList.Sort();
            Dictionary<int, string> cards = new Dictionary<int, string>
            {
                {1,"cardone"},
                {2,"cardtwo"},
                {3,"cardthree"},
                {4,"cardfour"},
                {6,"cardsix"},
                {34,"cardthirtyfour"},
                {23,"cardtwentythree"},
                {99,"cardninetynine"}
            };
            Dictionary<string,int> countList = new Dictionary<string,int>();
            cards.Keys.ToList().ForEach(f => { countList.Add(cards[f], idList.Where(w => w == f).ToList().Count); });
0
 

Author Comment

by:crafuse
ID: 36565857
cool, just heading home from work, will try that when i get home...

btw, is this VB or C#? can't tell...

also, i need to use this in a few different places, can i set that dictionary up once somewhere?

thanks...
0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36565873
This is c#, but it's almost entirely LINQ so it should translate pretty easily.  
You should be able to set the dictionary of cards up once, and pass whatever idList you want to into it and get the result counts the same way.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:crafuse
ID: 36566273
so, you can't make it VB? if not, what changes do I need to make?

i'm pretty much looking for a complete answer, or at least precise instruction...

thanks,

crafuse
0
 

Author Comment

by:crafuse
ID: 36566308
nixkuroi -

could i trouble you to make this answer complete so that it works in VB, including declaring variables, etc?

thnx.
0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36567991
Sure,

Dim cards = New Dictionary(Of Integer, String) From {{1, "cardone"}, {2, "cardtwo"}, {3, "cardthree"}, {4, "cardfour"}, {6, "cardsix"}, {34, "cardthirtyfour"}, {23, "cardtwentythree"}, {99, "cardninetynine"}}
        Dim idList = "1,1,2,2,2,3,1,3,34,34,34,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99,34,34".Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()
        Dim countList = New Dictionary(Of String, Integer)

        For Each k As Integer In cards.Keys
            countList.Add(cards(k), idList.Where(Function(w) w = k).ToList().Count)
        Next
0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36568003
Sorry, that didn't paste correctly

Dim cards = New Dictionary(Of Integer, String) From
        {
            {1, "cardone"},
            {2, "cardtwo"},
            {3, "cardthree"},
            {4, "cardfour"},
            {6, "cardsix"},
            {34, "cardthirtyfour"},
            {23, "cardtwentythree"},
            {99, "cardninetynine"}
        }
        Dim idList = "1,1,2,2,2,3,1,3,34,34,34,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99,34,34".Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()

        Dim countList = New Dictionary(Of String, Integer)

        For Each k As Integer In cards.Keys
            countList.Add(cards(k), idList.Where(Function(w) w = k).ToList().Count)
        Next

Open in new window

0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36568009
Make sure you import the following:

Imports System.Collections.Generic
0
 

Author Comment

by:crafuse
ID: 36568336
i modified the code as below. but it's not precisely what i'm looking for. if the cardid does not appear in the string it should not appear as, for example,

CardThirtyFour      0

That's what i get with the code below, take out a card id from the string, but it appears with cardname and 0 as the count.
Dim cards = New Dictionary(Of Integer, String) From
        {
            {1, "cardone"},
            {2, "cardtwo"},
            {3, "cardthree"},
            {4, "cardfour"},
            {6, "cardsix"},
            {34, "cardthirtyfour"},
            {23, "cardtwentythree"},
            {99, "cardninetynine"}
        }
        Dim idList = "1,1,2,2,2,3,1,3,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99".Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()

        Dim countList = New Dictionary(Of String, Integer)

        For Each k As Integer In cards.Keys
            countList.Add(cards(k), idList.Where(Function(w) w = k).ToList().Count)
            ListBox1.Items.Add(cards(k) & " " & idList.Where(Function(w) w = k).ToList().Count)
        Next

    End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36568385
So in that case, replace the For Each line with this:

For Each k As Integer In cards.Keys.Where(Function(c) idList.Contains(c) = True)
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 36568882
Hi crafuse;

The following code snippet should do what you want.

Dim cards = New Dictionary(Of Integer, String) From
{
    {1, "cardone"},
    {2, "cardtwo"},
    {3, "cardthree"},
    {4, "cardfour"},
    {6, "cardsix"},
    {34, "cardthirtyfour"},
    {23, "cardtwentythree"},
    {99, "cardninetynine"}
}

Dim idList = "1,1,2,2,2,3,1,3,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99".Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()

Dim countList = New Dictionary(Of String, Integer)

Dim result = (From id In idList
              Where cards.Keys.Contains(id)
              Group id By Key = id Into Group
              Order By Key
              Select String.Format("{0,-20}{1}{2}", cards(Key), vbTab, Group.Count())).ToArray()

ListBox1.Items.AddRange(result)

Open in new window


Fernando
0
 
LVL 3

Assisted Solution

by:nixkuroi
nixkuroi earned 1000 total points
ID: 36571317
Here, I added it for you.

Dim cards = New Dictionary(Of Integer, String) From
        {
            {1, "cardone"},
            {2, "cardtwo"},
            {3, "cardthree"},
            {4, "cardfour"},
            {6, "cardsix"},
            {34, "cardthirtyfour"},
            {23, "cardtwentythree"},
            {99, "cardninetynine"}
        }
        Dim idList = "1,1,2,2,2,3,1,3,34,34,34,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99,34,34".Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()

        Dim countList = New Dictionary(Of String, Integer)

       For Each k As Integer In cards.Keys.Where(Function(c) idList.Contains(c) = True)
            countList.Add(cards(k), idList.Where(Function(w) w = k).ToList().Count)
            ListBox1.Items.Add(cards(k) & " " & idList.Where(Function(w) w = k).ToList().Count)
        Next
0
 

Author Closing Comment

by:crafuse
ID: 36571424
sweet, thanks fellas. i've learned a lot.

went with the dictionary approach instead of enums for a few different reasons.
0
 

Author Comment

by:crafuse
ID: 36572604
Fernando -

wondering if you could help me here, trying to get your code to work so that i can dump the results into a string so that i can display in a rich textbox. can't seem to get it to work...

Dim idList = "1,1,2,2,2,3,1,3,23,23,23,2,4,4,4,6,6,6,1,1,1,99,99".Split(",".ToCharArray()).ToList().ConvertAll(Function(c) Convert.ToInt32(c)).ToList()

Dim countList = New Dictionary(Of String, Integer)

Dim result = (From id In idList
              Where cards.Keys.Contains(id)
              Group id By Key = id Into Group
              Order By Key
              Select String.Format("{0,-20}{1}{2}", cards(Key), vbTab, Group.Count())).ToArray()

 * * * * * ListBox1.Items.AddRange(result) * * * * * at this point i'd like to assign the result value to a string, but can't seem to get it to work...

Thanks...
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 36575718
Hi crafuse;

The result variable already holds a array of String data type. To place the information into a RichTextBox just do the following:

RichTextBox1.Lines = result

Fernando
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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