Solved

Dynamically Create/Name Lists inside a Loop

Posted on 2014-02-04
2
324 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
[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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 217
dialer.exe 4 68
Need to put a web footnote into a text book 7 50
Mobile apps - web/native or hybrid? 1 66
In this article I will provide some simple productivity hacks that will help you use Google to specifically show results from any web site (Experts-Exchange.com in my example), with minimal effort in Chrome and Firefox. I've seen a common theme a…
As freelancing is becoming more and more common in the tech industry, certain obstacles are proving to be a challenge to those who are used to more traditional, structured employment. This article is meant to help identify such obstacles and offer a…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

756 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