Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Ignore a field and set the ignored field as a global variable within the function?

I have this code to combine 2 different csv files.

try
{
    var jobStartLine = File.OpenText(PackAuftrag).ReadLine();
    var comparisonField = jobStartLine.Split(';')[4];

    foreach (var line in File.ReadAllLines(BoxData))
    {
        var fields = line.Split(new char[] {';'}, 2);

        if (comparisonField == fields[0])
        {
            File.WriteAllLines(JobStart, 
                               new string[] {jobStartLine + ";" + fields[1]});
            break;
        }
    }
}

My BoxData = data1;data2;data3;data4;data5

At the moment data2 to data5 is getting in JobStart file.

Data5 shouldn't be included inside JobStart file.

I want to set data5 as a global variable.

How can I do this, I just cannot figure this out, need help.

This can help understanding my problem: How to compare 2 .csv files and create a new .csv containing parts from both csv files?

like image 272
RamHS Avatar asked Nov 15 '13 11:11

RamHS


1 Answers

var foo = string.Empty;

try
{
    var jobStartLine = File.OpenText(PackAuftrag).ReadLine();
    var comparisonField = jobStartLine.Split(';')[4];

    foreach (var line in File.ReadAllLines(BoxData))
    {
        var fields = line.Split(new char[] {';'}, 5).ToList();
        foo = fields[4];
        fields.RemoveAt(4);

        if (comparisonField == fields[0])
        {
            File.WriteAllLines(JobStart, 
                           new string[] {jobStartLine + ";" + String.Join(';', fields.ToArray())});
            break;
        }
    }
}

This kinda resolves what I understood to be your problem.

like image 77
Ivan Peric Avatar answered Oct 11 '22 22:10

Ivan Peric