?
Solved

Save List<String[,]>

Posted on 2011-02-27
6
Medium Priority
?
219 Views
Last Modified: 2012-05-11
hello,
I have a List of 2D arrays.
for example as u can see in the code matrix contains a 2D array. And i want to save them in the file. The problem is that saving them is going by row not by Column!!!
How can i save them by column. these is something that i should change. But i can't figure it out.

Thank you.
StreamWriter data = new StreamWriter("caracteristiques.car");
        
        for (int i = 0; i < matrix.Count; i++)
        {
            for (int x = 0; x < matrix[i].GetLength(1); x++)
            {
                if (x > 0) data.WriteLine();
                for (int y = 0; y < matrix[i].GetLength(0); y++)
                {
                    string value = matrix[i][y, x];
                    if (y > 0) data.Write("    ");
                    data.Write(value);
                }
                
                
            }
        }
        data.Close();

Open in new window

0
Comment
Question by:dadadude
[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
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 34993418
Again, what you mean "by column"? Your arrays have different number of elements in different directions. Any examples?
0
 

Author Comment

by:dadadude
ID: 34993456
forget it
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 34993479
OK, I put some example together - it may help you to clarify what is required.

The output:

A_0_0 A_1_0 A_2_0
A_0_1 A_1_1 A_2_1
A_0_2 A_1_2 A_2_2
---------------------------
B_0_0 B_1_0 B_2_0
B_0_1 B_1_1 B_2_1
B_0_2 B_1_2 B_2_2
B_0_3 B_1_3 B_2_3
---------------------------
C_0_0 C_1_0 C_2_0
C_0_1 C_1_1 C_2_1
C_0_2 C_1_2 C_2_2
C_0_3 C_1_3 C_2_3
C_0_4 C_1_4 C_2_4
---------------------------

You may still continue with deleting the question if you want...
private void button2_Click(object sender, EventArgs e)
        {
            // example
            string[,] ar1 = new string[3, 3];
            string[,] ar2 = new string[3, 4];
            string[,] ar3 = new string[3, 5];
            initialiseArray(ar1, "A");
            initialiseArray(ar2, "B");
            initialiseArray(ar3, "C");

            List<string[,]> matrix = new List<string[,]>();
            matrix.Add(ar1);
            matrix.Add(ar2);
            matrix.Add(ar3);

            saveMatrix(matrix);
        }

        private void initialiseArray(string[,] ar, string identifier)
        {
            // initialise
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < ar.GetLongLength(1); j++)
                {

                    ar[i, j] = identifier + "_" + i + "_" + j;
                }
            }
        }

        private void saveMatrix(List<string[,]> matrix)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string[,] ar in matrix)
            {
                for (int j  = 0; j < ar.GetLength(1) ;j++){
                    for (int i = 0; i < ar.GetLength(0); i++)
                    {
                        sb.Append(ar[i,j] + " ");


                    }
                    sb.AppendLine("");// line break
                }
                // array divider - we are saving number of arrays...
                sb.AppendLine("---------------------------");
            }

            using(StreamWriter outfile =  	new StreamWriter(@"C:\Test\arrays\arrays.txt")){
                outfile.Write(sb.ToString());
            }

        }

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dadadude
ID: 34995284
hmmm it is still the same!!! it's not changing. as for the other question it's ok. It's a good answer
Thank you. I am trying to solve this question. Hope that it works.

thanks again.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34999221
In the future, please provide as much relevant detail as you can. You understand your problem better than anyone else; clearly explaining it and responding to questions about it is the only way you'll ever get a clever answer.    = )
0
 

Author Comment

by:dadadude
ID: 35005825
Question solved after all.
I need to grade it.

Thank You.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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