0

I have a lot of text files with values, although the lines in the text file should be partially scrambled.

An example of a text file is as follows: (see the edit for an easier example)

0.00;1.2;3;2015-20-06 13:33:33
0.00;1.2;3;2015-20-06 13:33:34
0.00;1.2;3;2015-20-06 13:33:35
0.00;1.2;3;2015-20-06 13:33:36
[RAND]
0.00;1.2;3;2015-20-06 12:05:05
0.00;1.2;3;2015-20-06 12:05:22
0.00;1.2;3;2015-20-06 12:06:27
0.00;1.2;3;2015-20-06 12:05:42
[/RAND]
0.00;1.2;3;2015-20-06 12:25:36
0.00;1.8;3;2015-20-06 12:26:26
0.00;1.2;3;2015-20-06 12:28:05
[RAND]
0.00;1.8;3;2015-20-06 12:32:22
0.00;1.2;3;2015-20-06 12:33:04
[/RAND]

Everything between [RAND] and [/RAND] should be put in a random order. So far I have the following but I have absolutely no idea how to continue from here or if this is even the right approach.

using (StreamReader reader = new StreamReader(LocalFile))
{
    bool InRegion = false;
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if (line.Equals("[RAND]"))
                    InRegion = true;

                if (line.Equals("[/RAND]"))
                    InRegion = false;
        }
}

One of my concerns is that I am using StreamReader and therefor cannot change the file.

There could be 2 lines but also 10 lines inside the RAND block and multiple RAND blocks per file. Could somebody explain me how to go by this?

Thanks a lot in advance.

EDIT:

Easier example:

A
B
C
[RAND]
D
E
F
[/RAND]
G
H

It should then scramble the lines with D, E and F in a random order so you get something as follows:

A
B
C
E
F
D
G
H
Roel
  • 754
  • 3
  • 13
  • 30
  • What do you want to do exactly? I can't understand your question well – farid bekran Jun 20 '15 at 11:50
  • I added another example to make it a bit easier. Basically it should scramble the order of the lines between `[RAND]` and `[/RAND]` in .txt files. – Roel Jun 20 '15 at 11:54

2 Answers2

2

The "bulky" way, leading to the most code (albeit readable), would be:

  • Read all lines, close the file
  • Find the blocks to randomize
  • Randomize those blocks
  • Write the result to a new file
  • Move the new file over the old file

Something like this:

var linesInFile = File.ReadAllLines();

var newLines = new List<string>();
var toRandomize = new List<string>();

bool inRegion = false;

for (int i = 0; i < linesInFile.Count; i++)
{
    string line = linesInFile[i];

    if (line == "[RAND]")
    {
        inRegion = true;
        continue;
    }
    if (line == "[/RAND]")
    {
        inRegion = false;       
        // End of random block.
        // Now randomize `toRandomize`, add it to newLines and clear it     
        newLines.AddRange(toRandomize);
        toRandomize.Clear();
        continue;
    }

    if (inRegion)
    {
        toRandomize.Add(line);
    }
    else
    {
        newLines.Add(line);
    }
}

File.WriteAllLines(newLines, ...);

See Randomize a List<T> to randomize the list.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

I think its good to

read all text of file at once

and get random areas with regular expression

and replace randomized result with it

above steps can be done by Replace method of RegEx class.

and finally save new content to file

For example:

var regExp = @"(\[RAND\])(\w|\s|\d)*(\[/RAND\])";
var res = Regex.Replace(str, regExp, match =>
{
       // suffle the and return result
       // the return string replaced with occuring rand area
       // for example with suffle algorithms
       return "Randomized";
 });
farid bekran
  • 2,684
  • 2
  • 16
  • 29