How to efficiently resolve Associative Arrays not being sorted?

BoQsc vaidas.boqsc at gmail.com
Tue Jun 2 07:32:56 UTC 2020


I want to read a file, put it into an array, make some search and 
replace on the content and output the modified text. However 
Associative Arrays seem to be unsorted by default. Should I drop 
the Associative Arrays and use something else? What are the ways 
to resolve this randomness in Associative Arrays?

ReadfileAndCopyContentIntoArray.d
> import std.stdio;
> 
> int lineNumber = 0;
> char[][int] fileInArray;
> 
> void main(){
> 	File exampleFile = File("exampleText.txt");
> 	foreach(line; exampleFile.byLine){
> 		
> 		lineNumber++;
> 		fileInArray[lineNumber] ~= line;
> 		
> 	}
> 	writeln(fileInArray);
> }

exampleText.txt
> The quick brown fox jumps over the lazy dog
> Sphinx of black quartz, judge my vow.
> How vexingly quick daft zebras jump!
> The five boxing wizards jump quickly
> Maecenas consectetur risus a lacus sodales iaculis.
> Morbi sed tortor sollicitudin, pharetra massa egestas, congue 
> massa.
> Sed sit amet nisi at ligula ultrices posuere quis nec est.
> Mauris vel purus viverra, pellentesque elit id, consequat felis.

The Command Prompt Output
> [6:"Morbi sed tortor sollicitudin, pharetra massa egestas, 
> congue massa.\r", 7:"Sed sit amet nisi at ligula ultrices pos
> uere quis nec est.\r", 2:"Sphinx of black quartz, judge my 
> vow.\r", 3:"How vexingly quick daft zebras jump!\r", 1:"The q
> uick brown fox jumps over the lazy dog\r", 8:"Mauris vel purus 
> viverra, pellentesque elit id, consequat felis.", 5:"Maec
> enas consectetur risus a lacus sodales iaculis.\r", 4:"The five 
> boxing wizards jump quickly\r"]

As can be seen in the Command Prompt Output, the array is not 
ordered correctly.
It goes: 6: 7: 2: 3: 1: 8: 5: 4:
Instead of 1: 2: 3: 4: 5: 6: 7: 8:



More information about the Digitalmars-d-learn mailing list