Solved

Dynamically Create/Name Lists inside a Loop

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bank transaction downloader ? 1 122
Word Mac 2011: changing the language assignment 2 115
Problem to open text file 11 145
Developing a front end to SPLUNK 1 104
I'd like to talk about something that is near and dear to my heart: build systems. Without them, building software is all about compiling locally, with software versions everywhere. It can be a mess. Today we are going to discuss building a small di…
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 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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

734 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