Click here to Skip to main content
16,012,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello !

I want to associate different strings [] in a same string [].
My different strings :
- string[] names
- string[] axes
- string[] datas

Every 3 datas of datas corresponds to 3 datas of axes and every group of 3 datas of axes corresponds to an only data of name. It's in the order.

How can I do that ?

Here my current programm which allows me to create my different strings as I want :

C#
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;

public class Recup_donnees_6 : MonoBehaviour {

    // When the script instance is being loaded
    void Awake ()
    {
        Application.targetFrameRate = 25; // To indicate : 25 Fps (Frame per rate)
    }

            // Use this for initialization
         void Start() // We need to get datas from a text file (so only once time)
        {

            // Create an instance of StreamReader to read from a file
            StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");

            using (reader) { // Automatic Closing of the Stream after working with it , otherwise we would have to write "reader.Close" at the end to close the stream, beneath the last command's line of this part "class FileReader"

                string line = " ";
                int lineNumber = 10; // The first line of the text file is the number 0
                // skip first 10 lines
                for (int i = 0; i < lineNumber; i++) {


                    line = reader.ReadLine();
                    if (line == null) return; // this ensures that the file has at least 12 lines
                }

                // At this point our line contains row #10 (Suj01:RIAS,,,Suj01:LIAS,,,Suj01:LIPS,,,Suj01:RIPS,,,Suj01:DSTR,,,Suj01:C7,,,Suj01:RACR,,,)
                string line_10;
                line_10 = line; // To indicate that the line #10 we read with the string "line" corresponds to the string "line_10"
                string[] names = line_10.Split (new String[] {",",",,,"},StringSplitOptions.RemoveEmptyEntries); // Every name is separating by ',,,' and the first is writing behind ','

                line = reader.ReadLine(); // Here it's line #11 (Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X)
                // To parse it and use it however you need
                string line_11;
                line_11 = line;
                string[] axes = line_11.Split(new String[] {"Field #",","},StringSplitOptions.RemoveEmptyEntries); // Every axe is separating by ',' and the first is writing behind 'Field #,'
           

                int counter = 0;
                while (line != null) { // Until the end of the text file
                    counter++;
                    line = reader.ReadLine(); // Here the loop starts getting data
                    if ((counter %3) != 1)
                    continue;
                    // if it is exactly 1 (lines 1, 5, 9 etc) continue working
                    // here you're beginning with the lines you need to enter into the matrix
                    // (1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,)
                    string lines_datas; // To create a matrix which contains 1 line / 4 from text file
                    lines_datas = line;
                    string[] datas;
                    datas = lines_datas.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries); 

                    line = reader.ReadLine(); // This needs to be last line in the while so you can continue the loop


                }
            }

    }
}




I need some helps.
I want to understand to how I can programm !

Thank you :)
Posted
Updated 3-Mar-15 4:32am
v2
Comments
Sergey Alexandrovich Kryukov 3-Mar-15 10:41am    
This is 1) way to vague, 2) the code sample seems to be unrelated, 3) you are only talking about strings, which does not seem to make any sense.

Aren't you the one who try to use strings representing data instead of data itself? If you are, you should better check yourself. Do you know value and references types and members, references? Anyway, so far, you did not express any comprehensible concerns, still are talking about nothing.

Perhaps, to ask a sensible question, you need to learn more on general programming: types and members, static and instance, references, access, primitive design patterns. If you can improve the question, use "Improve question".

—SA
BillWoodruff 3-Mar-15 10:44am    
Where are you stuck ? How does your code here not work for you ? Do you get specific error messages: if so, please describe them.

Do you have control over the structure of the data file you read: if so, have you considered writing out the data and meta-data in a more easily parseable form, like CVS.
Coralie B 4-Mar-15 2:22am    
My code runs. I showed only my current program.

And my question is how can I gather these datas ?

Maciej Los 3-Mar-15 13:06pm    
Sorry, i read it 3. time but still unable to understand what you mean by saying: "associate different strings [] in a same string []"...
Coralie B 4-Mar-15 2:28am    
I want to gather datas which come from different strings.

For example :

The first data of names is : Suj01:RIAS
The first 3 datas of axes are : X, Y and Z
The first 3 datas of datas are : -242.807816,1106.551270 and 1097.119385

(The same schema for the rest)

I want to gather that :
Suj01:RIAS
X : -242.807816
Y : 1106.551270
Z : 1097.119385
etc ...

The string names has only 1 line with 63 datas
The string axes has only 1 line with (63*3) datas (ever X, Y and Z repeat 3 times each)
The string datas has several lines with (63*3) datas at each line (per line : there is so 63 groups of 3 datas, and each data corresponds to a data from axes, and each group corresponds to a name)

Do you understand what I explain ?

I'm going to be honest here, and say...um...that's not very good.

Don't do this:
C#
StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");

 using (reader) { // Automatic Closing of the Stream after working with it , otherwise we would have to write "reader.Close" at the end to close the stream, beneath the last command's line of this part "class FileReader"

The advantage of the using block is that it takes the variable out of scope and destorys it on completion. So always do using blocks this way:
C#
using (StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt"))
   {
   ...
   }


Don't comment what the code does, comment where it is going to explain code. The using block you show doesn't need comments to say why you are using it: everyone who has learned C# show know what it does!

Don't bother user a StreamReader at all: use
C#
string[lines] = File.ReadAllLines("Suj01_PI_DP_C00_1.txt");
instead, and work with the array of data it returns. Then you just use a for loop to run through the lines, in groups if that's how they are organised.

If you are creating a class, and need one-time-executed code, then create a constructor for the class. When an instance is created, the constructor is called. If you need it executed once per application, then create a static constructor: that will be called the first time you class it used, and never again.

"Associating data" is what classes are all about: So create a class that has a Name, and three Axes, and three Datas - then you create an instance of the class for each Name you need to create and fill in it's data. You then keep a collection of the new class to hold all the instances as you create them.
 
Share this answer
 
Comments
Andreas Gieriet 3-Mar-15 10:41am    
My 5! Right, that's what classes are for ;-)
Cheers
Andi
Sinisa Hajnal 4-Mar-15 2:37am    
I totally agree with the solution, my 5. This question is actually result of two pages of learning file reading (actually adjusting his parsing routine). I've advised him to ask the question again now that he has working sample of file parsing so we can move on using the parsed lines.
Coralie B 4-Mar-15 3:02am    
Thank you for your reply.
I'm a beginner in C#, so I have commented that like I thought it was good to learn for myself. Then, I'm not english, so I try to understand what you mean but it's difficult some times.

Can you give me a simple example, please, to how create a class with a Name, three Axes and three Datas ?

P-s : I'm a (young) girl, so at the beginning Sinisa Hajnal, I don't understand that you spoke about me at your reply.
P-p-s : What is "My 5" ? I don't see it anywhere except in your replys
OriginalGriff 4-Mar-15 5:33am    
In reverse order:
Pps: "My 5" and "+5" and suchlike are to do with the CodeProject Reputation system: a "5" is good, a "1" is bad - if you look at the top of this solution, there are little orange stars which indicate what people have thought of it as an answer. "5"s add to your reputation, a little bit, "1"s reduce it.
Don't worry about it too much - you can't convert rep points to real world money! :laugh:

Ps: If you are young, a girl, or both is irrelevant: you are on the internet where you can "be" whatever you want to be (though advertising yourself as a young girl is probably not a good move - there are a lot of predatory people out there who could be attracted to that combination. Not here (as far as I know) but it's a good idea to be careful at all times on the internet. I'm sure the TV news in your country has plenty of bad examples of why!
Sinisa Hajnal wasn't talking direct to you - his comment was a reply to me. If you look at the page, you will see a structure:
Solution
Andreas
Sinisa
Member 11481605
OriginalGriff
Member 11481605

And each reply is to the member it is directly indented from.
So this message is indented to the right from one of yours - because it is a reply to your message.
And Sinisa replied to me, so his is indented to the right of my solution. (I assume from the context that you and he have discussed other matters around this subject)

Your example of a class in the message below is getting the wrong idea, it seems.
I think before you go any further, you want to read your course notes rather carefully - it looks like you haven't understood what classes are and what / how they are used yet - and I don't want to tread on your tutors toes by explaining things he hasn't got to yet.

So have a look, have a think, and if you can't see what to do, ask again afterwards. If you can, then let me know and show me your new version and I'll see if you have got the right idea, OK?
Coralie B 5-Mar-15 10:23am    
Don't worry about me ;)

I think I need your : using (StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt"))
{
...
}
But how can I use that in my code ?
I have to erase each "line = reader.Readline ()" ?

Here my new question http://www.codeproject.com/Answers/883314/Csharp-Some-errors-in-void-update#answer2
Thank you for clarification. I think you need to create two classes:
C#
class Axe
{
    private double x = .0;
    private double y = .0;
    private double z = .0;

    public Axe(double _x, double _y, double _z)
    {
        x = _x;
        y = _y;
        z = _z;
    }

    public double X
    {
        get{return x;}
        set{x = value;}
    }

    public double Y
    {
        get{return y;}
        set{y = value;}
    }

    public double Z
    {
        get{return z;}
        set{z = value;}
    }

}


class MyData
{
    private string aName = string.Empty;
    private List<Axe> axes = new List<Axe>();

    public MyData(string _name, List<Axe> _axes)
    {
        aName = _name;
        axes = _axes;
    }

    public string Name
    {
        get{return aName;}
        set {aName = value;}
    }

    public List<Axe> Axes
    {
        get{return axes;}
        set{axes = value;}
    }

}

As you can see, the first one (Axe) is used to store single X, Y and Z values. The second one (MyData) is a holder for single name Suj01:RIAS and list of Axe's ;)

Usage:
C#
List<MyData> datas = new List<MyData>
{
    new MyData("Suj01:RIAS", new List<Axe>{new Axe(.22, 5.12, 2.33), new Axe(8.22, 15.12, 22.33), new Axe(4.22, 9.12, 9.33)}),
    new MyData("Suj02:RIAS", new List<Axe>{new Axe(.11, 5.32, 2.55), new Axe(8.11, 15.32, 22.55), new Axe(4.11, 9.32, 9.55)})
};


[EDIT]
Imagine, that now you're able to get statistical data via using Linq[^] queries, for example:
C#
var qry = from d in datas
    select new
    {
        Name = d.Name,
        AvgX = d.Axes.Average(a=>a.X).ToString("#0.00"),
        AvgY = d.Axes.Average(a=>a.Y).ToString("#0.00"),
        AvgZ = d.Axes.Average(a=>a.Z).ToString("#0.00"),
        SumX = d.Axes.Sum(a=>a.X).ToString("#0.00"),
        SumY = d.Axes.Sum(a=>a.Y).ToString("#0.00"),
        SumZ = d.Axes.Sum(a=>a.Z).ToString("#0.00"),
        Total = d.Axes.Sum(a=>a.X+a.Y+a.Z).ToString("#0.00")
    };


Result:
tex
Name       AvgX AvgY AvgZ  SumX  SumY  SumZ  Total
Suj01:RIAS 4.22 9.79 11.33 12.66 29.36 33.99 76.01
Suj02:RIAS 4.11 9.99 11.55 12.33 29.96 34.65 76.94


[/EDIT]

I'd sugest to read this: Walkthrough: Creating Your Own Collection Class[^]


Note that i'm not sure i understand you correctly, but i hope that my answer will be good point to start discuss about OOP programming.
 
Share this answer
 
v4
Comments
Coralie B 4-Mar-15 4:13am    
Thank you for what you have made.

But, do you think this code is on track ?

// To define one table and its schema
DataTable dt = new DataTable();
dt.column.Add("Name",typeof(string));
dt.column.Add("Axes",typeof(string));
dt.column.Add("Datas",typeof(string));

// To integrate rows
DataRow dr = dt.NewRow();

for (int i=0; i < names.Length; ++i)
dr["Name"]= names[i];

for (int i=0; i < axes.Length; i=i+3)
dr["Axes"]= axes[i];

for (int i=0; i < datas.Length; i=i+3)
dr["Datas"]= datas[i];

dt.Rows.Add(dr);

It's what I try to do...
And if it's possible, I want to do something with my code because I understand this method.

Thank you a much again
Maciej Los 4-Mar-15 4:17am    
You're going wrong way...
Coralie B 4-Mar-15 4:20am    
It's not possible to use this base for making the code ?
Coralie B 4-Mar-15 4:43am    
And that ?

List<myawesomeclass> listOfAwesome = new List<myawesomeclass>();

for(int i = 0; i < names.Length; i++)

{
var myClassVar = new MyAwesomeClass();
myClassVar.name = names[i];
myClassVar.axisX = axes[(i*3)];
myClassVar.axisY = axes[(i*3)+1];
myClassVar.axisZ = axes[(i*3)+2];
myClassVar.dataA = datas[(i*3)];
myClassVar.dataB = datas[(i*3)+1];
myClassVar.dataC = datas[(i*3)+2];
listOfAwesome.Add(myClassVar);

}
Maciej Los 4-Mar-15 5:50am    
You're barking up the wrong tree...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900