how do you append arrays?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 25 05:02:40 PST 2016


On Thursday, 25 February 2016 at 12:53:37 UTC, asdf wrote:
> I'm trying to make a terminal input preprocessor with 
> alias/shortcuts and history.
>
>
> import std.stdio;
>
> void main() {
>     string line;
>     string[] history;
>
>     line = readln();
>     foreach(int i; 0..100) history = history + [""]; // XXX
>
>     while(!stdin.eof) {
>         writeln(line);
>         if(line != history[0]) {
>             history[1..100] = history[0..99];
>             history[0] = line;
>         }
>         line = readln();
>     }
> }

Also for this kind of thing you probably want to use a circular 
buffer. I'm sure there is an implementation of one somewhere. see 
std.range.cycle


More information about the Digitalmars-d-learn mailing list