-1

How to make console app to read csv file that has row IsHidden( isHidden = false for to be shown )

The point is I have made everything up and running but cannot think of the logic for the true(hidden) and false(true) row to be read into console app and shows it those who should :D - sorry for my bad English :)

the code I'm using

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PreInterviewTask
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the data from path.
            string sampleCSV = @"C:\Users\Tomas\source\repos\PreInterviewTask\PreInterviewTask\HistoricalData\HistoricalData.csv";

            string[,] values = LoadCSV(sampleCSV);
            int num_rows = values.GetUpperBound(0) + 1;
            int num_cols = values.GetUpperBound(1) + 1;

            // Display the data to show we have it.

            for (int c = 0; c < num_cols; c++)
                Console.Write(values[0, c] + "\t");

            //Read the data.
            for (int r = 1; r < num_rows; r++)
            {
                //  dgvValues.Rows.Add();
                Console.WriteLine();
                for (int c = 0; c < num_cols; c++)
                {
                    Console.Write(values[r, c] + "\t");
                }
            }

            Console.ReadLine();

        }

        private static string[,] LoadCSV(string filename)
        {
            // Get the file's text.
            string whole_file = System.IO.File.ReadAllText(filename);

            
            // Split into lines.
            whole_file = whole_file.Replace('\n', '\r');
            string[] lines = whole_file.Split(new char[] { '\r' },
                StringSplitOptions.RemoveEmptyEntries);

            // See how many rows and columns there are.
            int num_rows = lines.Length;
            int num_cols = lines[0].Split(',').Length;

           
            // Allocate the data array.
            string[,] values = new string[num_rows, num_cols];
            
            // Load the array.
            for (int r = 0; r < num_rows; r++)
            {
                string[] line_r = lines[r].Split(',');
                for (int c = 0; c < num_cols; c++)
                {
                    values[r, c] = line_r[c];
                }
                
            }

            

            // Return the values.
            return values;
        }
    }

}

the output i get :

ID;MenuName;ParentID;isHidden;LinkURL
1;Company;NULL;False;/company
2;About Us;1;False;/company/aboutus
3;Mission;1;False;/company/mission
4;Team;2;False;/company/aboutus/team
5;Client 2;10;False;/references/client2
6;Client 1;10;False;/references/client1
7;Client 4;10;True;/references/client4
8;Client 5;10;True;/references/client5
10;References;NULL;False;/references

and what should look like : Example Output

. Company
.... About Us
....... Team
.... Mission
. References
.... Client 1
.... Client 2
  • 1
    Does not directly answer your question, but please take a look [here](https://stackoverflow.com/a/33796861/2590375). Adding `bool isHidden = fields[3] == "True";` should do the trick for you. Happy coding! – nilsK Dec 11 '20 at 15:50
  • You need to clarify what you are asking. It is difficult to understand what you mean by… _”hiding columns or rows that have true or false value”_ … this does not make sense. A row and a column are two different things. If a row or a column has ‘false’, then how would you know to hide the row or the just the column? Also, it is nice that you show what your code outputs and the expected output, however, it is meaningless without seeing the CSV file you are reading from, or at least a couple of lines from it. Please edit your question and clarify what you are asking. – JohnG Dec 11 '20 at 16:13
  • The .csv is : ID;MenuName;ParentID;isHidden;LinkURL 1;Company;NULL;False;/company 2;About Us;1;False;/company/aboutus 3;Mission;1;False;/company/mission 4;Team;2;False;/company/aboutus/team 5;Client 2;10;False;/references/client2 6;Client 1;10;False;/references/client1 7;Client 4;10;True;/references/client4 8;Client 5;10;True;/references/client5 10;References;NULL;False;/references so the client 4 and 5 are supposed to be hidden and that`s what I meant :) sorry – Tome Jovanov Dec 11 '20 at 20:04

1 Answers1

0

See if following helps. I used your output as the input since I do not have the actual input. :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication176
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            Menu menu = new Menu(FILENAME);

            List<Menu> sortedRows = Menu.items.OrderBy(x => x).ToList();
            menu.Print(sortedRows);
            Console.ReadLine();
        }
    }
    public class Menu : IComparable
    {
        public static List<Menu> items { get; set; }
        public int ID { get; set; }
        public string name { get; set; }
        public int? parent { get; set; }
        public Boolean hidden { get; set; }
        public string[] linkUrl { get; set; }

        public Menu() { }
        public Menu(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            string line = "";
            int rowCount = 0;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    if (++rowCount  == 1)
                    {
                        items = new List<Menu>();
                    }
                    else
                    {
                        Menu newMenu = new Menu();
                        items.Add(newMenu);
                        string[] splitArray = line.Split(new char[] { ';' }).ToArray();
                        newMenu.ID = int.Parse(splitArray[0]);
                        newMenu.name = splitArray[1];
                        newMenu.parent = (splitArray[2] == "NULL")? null : (int?)int.Parse(splitArray[2]);
                        newMenu.hidden = Boolean.Parse(splitArray[3]);
                        newMenu.linkUrl = splitArray[4].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    }

                }
            }
        }
        public int CompareTo(object obj)
        {
            Menu other = (Menu)obj;
            int min = Math.Min(this.linkUrl.Length, other.linkUrl.Length);

            for (int i = 0; i < min; i++)
            {
                int compare = this.linkUrl[i].CompareTo(other.linkUrl[i]);
                if (compare != 0) return compare;
            }
            return this.linkUrl.Length.CompareTo(other.linkUrl.Length);
        }
        public void Print(List<Menu> rows)
        {
            foreach (Menu menu in rows)
            {
                if (!menu.hidden)
                {
                    int length = menu.linkUrl.Length - 1;
                    Console.WriteLine(".{0} {1}", new string('.', 3 * length), menu.name);
                }
            }
        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Need to remove ".ToArray()". Should be `string[] splitArray = line.Split(new char[] { ';' });` and `string[] pathLength = splitArray[4].Split(new char[] { '/' });` – Tu deschizi eu inchid Dec 11 '20 at 17:08
  • i see what you have done and I like it, but the main problem is that client 4 and 5 are hidden, forgot to say what was on the csv. here it is : ```ID;MenuName;ParentID;isHidden;LinkURL 1;Company;NULL;False;/company 2;About Us;1;False;/company/aboutus 3;Mission;1;False;/company/mission 4;Team;2;False;/company/aboutus/team 5;Client 2;10;False;/references/client2 6;Client 1;10;False;/references/client1 7;Client 4;10;True;/references/client4 8;Client 5;10;True;/references/client5 10;References;NULL;False;/references``` – Tome Jovanov Dec 11 '20 at 19:59
  • The menu items should be indented depending on the parent they belong under. -Some items are hidden, and should not be presented -The items should be ordered alphabetically – Tome Jovanov Dec 11 '20 at 20:45
  • Very BIG Thanks <3 i can see what`ve missing – Tome Jovanov Dec 12 '20 at 22:52