Solved

Dynamically Create/Name Lists inside a Loop

Posted on 2014-02-04
2
318 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
Comment Utility
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
Comment Utility
I am an c# amateur programmer and I was complicating what was simple. Thks Jason!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now