3
Answers

comparing values

Hello,

I have come up with a new problem.

The task is: if there is an array, check each value in the array matches the values in the column A of the provided csv file. say, if ith value in the array is not present in the column , then put a null character, else put the corresponding value from column B, and show in a rich text box.

Can anybody help?

Regards
Answers (3)
0
csharp beginner
NA 38 9.4k 11y
Thank you Hanook, for your the help.

But, what i want is: when i open a file, i want read the first, say 300, and the last value, say 400, of the first column. then prepare an array where i put all numbers from 300 to 400. Then, compare each element of the column A with the array element one by one, from the file. If array element is present in the column A, then save it in an another file along with the corresponding value of it from column B. If it is not, then put a null value. And, do it till end of the file.

I have prepared some codes, check where i am going wrong.


please check the code below, that i have prepared.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace missing_values_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opd = new OpenFileDialog();

            opd.InitialDirectory = @"c:\";
            opd.Title = "Choose your file for finding max values";
            opd.CheckFileExists = false;
            opd.CheckPathExists = false;
            opd.DefaultExt = "csv";
            opd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            opd.FilterIndex = 2;

            //string filechose = opd.FileName;

            if (opd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var dataline = File.ReadAllLines(opd.FileName);
                    string[] start = dataline.First().Split(',');
                    string x = start[0];
                    int ix = Convert.ToInt32(x);
                    //label1.Text = x;

                    string[] end = dataline.Last().Split(',');
                    string y = end[0];
                    int iy = Convert.ToInt32(y);
                    //label1.Text = y;

                    List<string> list = new List<string>();
                    for (int runs = ix; runs <= iy; runs++)
                    {
                        string srun = runs.ToString();
                        list.Add(srun);
                    }
                    string[] terms = list.ToArray();
                    //string vterm = string.Join("",terms);
                    //richTextBox1.Text = vterm.ToString();
                    
                    List<string> listA = new List<String>();
                    List<string> listB = new List<String>();
                    foreach (string line in dataline)
                    {
                        while (line != null)
                        {
                            string[] myColumns = line.Split(',');

                            listA.Add(myColumns[0]);
                            listB.Add(myColumns[1]);
                        }
                        //i++;
                    }
                    string[] firstlistA = listA.ToArray();
                    string[] firstlistB = listB.ToArray();

                   SaveFileDialog sfd = new SaveFileDialog();

                        sfd.InitialDirectory = @"c:\";
                        sfd.Title = "Choose file for saving.";
                        //opd.CheckFileExists = false;
                        //opd.CheckPathExists = false;
                        sfd.DefaultExt = "csv";
                        sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                        sfd.FilterIndex = 2;

                        //string filechose = opd.FileName;

                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            var csv = new StringBuilder();

                            for (int i = 0; i <= (iy - ix); i++)
                            {
                                for (int j = 0; j <= firstlistA.Length; j++)
                                {
                                    for (int k = 0; k <= firstlistB.Length; k++)
                                    {
                                        if (terms[i] == firstlistA[j])
                                        {
                                            foreach(char a in firstlistA[j])
                                            {
                                                while (firstlistB[k] != null)
                                                {
                                                    string first = terms[i];
                                                    string second = firstlistB[k];
                                                    var newLine = string.Format("{0},{1}{2}", first, second, Environment.NewLine);
                                                    csv.Append(newLine);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            string first = terms[i];
                                            string second = null;
                                            var newLine = string.Format("{0},{1}{2}", first, second, Environment.NewLine);
                                            csv.Append(newLine);
                                        }
                                    }
                                }
                            }

                            sfd.FileName = csv.ToString();

                            //File.AppendAllText(@"c:\output.csv", csv.ToString());
                        }
                    
                }

                catch (Exception g)
                {
                }
            }
        }
    }
}

0
Hanook
NA 371 1.2k 11y
0
Hanook
NA 371 1.2k 11y
follow these steps
1) Import ColumnA, ColumnB of CSV file data to a table in database  ( for suppose  say CSVTable )
2) compare CSVTable with array values