Solved

Dynamically Create/Name Lists inside a Loop

Posted on 2014-02-04
2
323 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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
Companies keep a much closer eye on costs today, so changing to new Technology – Microsoft Office 365 is the smartest move to take.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

829 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