c# - Assigning variables of an array -


i trying assign variables(var1-4) 4 parts of second(lines2) array.

static void main(string[] args) {     string var1;     string var2;     string var3;     string var4;      string readcontents;     using (streamreader streamreader = new streamreader(@"file.txt"))     {         readcontents = streamreader.readtoend();         string[] lines = readcontents.split('\r');         foreach (string s in lines)         {             string[] lines2 = s.split('\t');             foreach (string s2 in lines2)             {                 var1 = lines2[0];                 var2 = lines2[1];                 var3 = lines2[2];                 var4 = lines2[3];                 console.writeline(var4);             }         }     } } 

at moment, having issue var1 returns data want, console showing 8x more of value exists within file. , getting indexoutofrangeexception on every var after first 1 during run-time. not sure causing either of these errors.

edit: data text file - each separated tab:

2015-04-19 00:00:00  hostname.errorlevel    ip address  "error message" 2015-04-19 00:00:00  hostname.errorlevel    ip address  "error message" 2015-04-19 00:00:01  hostname.errorlevel    ip address  "error message" 

you can either iterate on results of split , each or access results index. code doing both , while have chance correct repeat assignment multiple times.

additionally not checking number of parts in string , getting "index out of range" result.

there method reads lines enumerable - file.readalllines, thing left split each string:

    foreach (string s in lines)     {         string[] lines2 = s.split('\t');         if (lines2.length == 4)         {           var1 = lines2[0];           var2 = lines2[1];           var3 = lines2[2];           var4 = lines2[3];           console.writeline(var4);         }         else         {               // report error?         }       }     } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -