[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamically Create/Name Lists inside a Loop

Posted on 2014-02-04
2
Medium Priority
?
335 Views
Last Modified: 2014-02-04
I am reading lines from a textfile that have several slashes "/"  in each line. I need to know the "tags",   that are in a  List {pencil, rubber, ink, paper}

I read the line    "pencil 120 rubber 10 / ink 100 / paper and pencil"

ad I use Split  

string[] words = str2.Split('/');

words[0] will be   "pencil 120 rubber 10"   "
words[1] will be   "ink 100"
words[2] will be    "paper and pencil"      

now I need to know the tags in each string (3 in this case)


In the non elegant way I needed to make (pseudocode)

switch NumberOfSlashes

{
case 2:
List<string> results0 = new List<string>();
List<string> results1 = new List<string>();
List<string> results2 = new List<string>();

results0 = GetTags(words[0])     will give me 2 results "pencil" and "rubber"
results1 = Gettags(words[1])     will give me 1 result "ink""
results0 = GetTags(words[2])     will give me 2 results "paper" and "pencil"
break:

case 3:
List<string> results0 = new List<string>();
List<string> results1 = new List<string>();
List<string> results2 = new List<string>();
List<string> results3 = new List<string>();
.....
.....
....
....
break:
case 4:

}


As I don't know how many slashes I will have in each line, this will be a nightmare without a Loop

I need something inside a loop


 for (g = 0; g < words.Count();g++ )
{
 
 create a list when g = 0 called results0
 get the tags inside words[0]

create a list when g = 1 called results1
 get the tags inside words[1]

create a list when g = 2 called results2
 get the tags inside words[2]

etc
}

I can have more than 3 slashes but I needed an example (this code is not good)
0
Comment
Question by:novreisb
2 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 2000 total points
ID: 39833029
List<string> allTags = new List<string>();
string[] words = str2.Split('/');
foreach(string word in words)
{
   List<string> tags= GetTags(word);
   allTags = allTags.Union(tags).ToList();
}

Open in new window

0
 

Author Closing Comment

by:novreisb
ID: 39833223
I am an c# amateur programmer and I was complicating what was simple. Thks Jason!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 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