Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 25, 2016 how do you append arrays? | ||||
---|---|---|---|---|
| ||||
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(); } } |
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to asdf | 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();
> }
> }
In D the binary operator "~" is used to concatenate both strings (arrays of characters) and arrays. (also the ~= operator is equivalent to lhs = lhs ~ rhs
Nic
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to asdf | 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
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Thursday, 25 February 2016 at 12:58:54 UTC, Nicholas Wilson wrote:
>
> In D the binary operator "~" is used to concatenate both strings (arrays of characters) and arrays. (also the ~= operator is equivalent to lhs = lhs ~ rhs
>
> Nic
It worked! A link from someone else's question suggested `new string[101]` also. Now on to other problems, it never ends...
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Thursday, 25 February 2016 at 12:58:54 UTC, Nicholas Wilson wrote:
> 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();
>> }
>> }
>
> In D the binary operator "~" is used to concatenate both strings (arrays of characters) and arrays. (also the ~= operator is equivalent to lhs = lhs ~ rhs
>
> Nic
Just a precision: "lhs ~= rhs" isn't exactly equivalent to "lhs = lhs ~ rhs", those are two distinct operators that may deal with memory etc in different ways. For arrays doing "lhs = lhs ~ rhs" will first create (and allocate) the array corresponding to "lhs ~ rhs" and then assign this new array to lhs. On the other hand "lhs ~= rhs" realises in-place append.
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Thursday, 25 February 2016 at 13:06:10 UTC, cym13 wrote: >> >> In D the binary operator "~" is used to concatenate both strings (arrays of characters) and arrays. (also the ~= operator is equivalent to lhs = lhs ~ rhs >> >> Nic > > Just a precision: "lhs ~= rhs" isn't exactly equivalent to "lhs = lhs ~ rhs", those are two distinct operators that may deal with memory etc in different ways. For arrays doing "lhs = lhs ~ rhs" will first create (and allocate) the array corresponding to "lhs ~ rhs" and then assign this new array to lhs. On the other hand "lhs ~= rhs" realises in-place append. I tried both, the error this time is: object.Exception@/data/data/com.termux/files/home/ldc/runtime/druntime/src/ldc/arrayinit.d(151): overlapping array copy |
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to asdf | On Thursday, 25 February 2016 at 13:24:09 UTC, asdf wrote:
> On Thursday, 25 February 2016 at 13:06:10 UTC, cym13 wrote:
>>>
>>> In D the binary operator "~" is used to concatenate both strings (arrays of characters) and arrays. (also the ~= operator is equivalent to lhs = lhs ~ rhs
>>>
>>> Nic
>>
>> Just a precision: "lhs ~= rhs" isn't exactly equivalent to "lhs = lhs ~ rhs", those are two distinct operators that may deal with memory etc in different ways. For arrays doing "lhs = lhs ~ rhs" will first create (and allocate) the array corresponding to "lhs ~ rhs" and then assign this new array to lhs. On the other hand "lhs ~= rhs" realises in-place append.
>
> I tried both, the error this time is:
> object.Exception@/data/data/com.termux/files/home/ldc/runtime/druntime/src/ldc/arrayinit.d(151): overlapping array copy
so you will have to make a copy and then move it
Im assuming
history[1..100] = history[0..99];
this is the line causing your problem.
Note that D has zero based array indexing
so assuming your array has 100 elements history[1..100]
is going one past the end of the array.
also using a circular buffer here will solve this problem.
Nic
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On 25.02.2016 14:33, Nicholas Wilson wrote:
> Note that D has zero based array indexing
> so assuming your array has 100 elements history[1..100]
> is going one past the end of the array.
No, that's fine. `history[1..100]` gives you 99 elements starting at index 1, i.e. all except the first one.
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Thursday, 25 February 2016 at 13:38:56 UTC, ag0aep6g wrote:
> On 25.02.2016 14:33, Nicholas Wilson wrote:
>> Note that D has zero based array indexing
>> so assuming your array has 100 elements history[1..100]
>> is going one past the end of the array.
>
> No, that's fine. `history[1..100]` gives you 99 elements starting at index 1, i.e. all except the first one.
Derp. You are correct, I was thinking about the first index.
|
February 25, 2016 Re: how do you append arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to asdf | On 2/25/16 8:24 AM, asdf wrote:
> On Thursday, 25 February 2016 at 13:06:10 UTC, cym13 wrote:
>>>
>>> In D the binary operator "~" is used to concatenate both strings
>>> (arrays of characters) and arrays. (also the ~= operator is
>>> equivalent to lhs = lhs ~ rhs
>>>
>>> Nic
>>
>> Just a precision: "lhs ~= rhs" isn't exactly equivalent to "lhs = lhs
>> ~ rhs", those are two distinct operators that may deal with memory etc
>> in different ways. For arrays doing "lhs = lhs ~ rhs" will first
>> create (and allocate) the array corresponding to "lhs ~ rhs" and then
>> assign this new array to lhs. On the other hand "lhs ~= rhs" realises
>> in-place append.
>
> I tried both, the error this time is:
> object.Exception@/data/data/com.termux/files/home/ldc/runtime/druntime/src/ldc/arrayinit.d(151):
> overlapping array copy
overlapping copies are not supported.
In this case especially, the copying has to be done backwards.
I believe you could use std.algorithm.copy, but probably need to do it with retro as well.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation