Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 29, 2017 Missing array element | ||||
---|---|---|---|---|
| ||||
Hi, Can any one help me on the below program, as I need the missing element for array "a" to be printed when compared with array "b" Program: import std.stdio, std.array, std.algorithm; string[] a = ["test1", "test2", "test4"]; string[] b = ["test2", "test4"]; void main () { auto m = mismatch(a,b); writeln(m[0]); writeln(m[1]); } Output: ["test1", "test2", "test4"] ["test2", "test4"] Required output: "test1" From, Vino.B |
August 29, 2017 Re: Missing array element | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino.B | On Tuesday, 29 August 2017 at 18:20:38 UTC, Vino.B wrote:
> Hi,
>
> Can any one help me on the below program, as I need the missing element for array "a" to be printed when compared with array "b"
>
> Program:
>
> import std.stdio, std.array, std.algorithm;
> string[] a = ["test1", "test2", "test4"];
> string[] b = ["test2", "test4"];
> void main ()
> {
> auto m = mismatch(a,b);
> writeln(m[0]);
> writeln(m[1]);
> }
>
> Output:
> ["test1", "test2", "test4"]
> ["test2", "test4"]
>
> Required output: "test1"
>
> From,
> Vino.B
The mismatch function doesn't work how your expecting it to. It compares the ith element in a with the ith element in b, until they encounter an element that is not equal. It then returns all elements including and beyond i, for both arrays. In this case the first element does not match, and therefore it returns the entire contents of both arrays.
|
August 29, 2017 Re: Missing array element | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino.B | On 08/29/2017 11:20 AM, Vino.B wrote: > string[] a = ["test1", "test2", "test4"]; > string[] b = ["test2", "test4"]; > Required output: "test1" You're looking for setDifference: https://dlang.org/phobos/std_algorithm_setops.html#.setDifference Ali |
August 30, 2017 Re: Missing array element | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 29 August 2017 at 18:39:03 UTC, Ali Çehreli wrote:
> On 08/29/2017 11:20 AM, Vino.B wrote:
>
>> string[] a = ["test1", "test2", "test4"];
>> string[] b = ["test2", "test4"];
>
>> Required output: "test1"
>
> You're looking for setDifference:
>
> https://dlang.org/phobos/std_algorithm_setops.html#.setDifference
>
> Ali
Hi,
I tried the setDifference but does seem to be working as expected
import std.stdio, std.array, std.algorithm;
string[] a = ["test4", "test3", "test2", "test1"];
string[] b = ["test2", "test4"];
void main ()
{
auto m = setDifference(a,b);
writeln(m);
}
Output : Required output ["test3", "test1"]
["test3", "test2", "test1"]
From,
Vino.B
|
August 30, 2017 Re: Missing array element | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino.B | On Wednesday, 30 August 2017 at 06:16:16 UTC, Vino.B wrote: > On Tuesday, 29 August 2017 at 18:39:03 UTC, Ali Çehreli wrote: >> https://dlang.org/phobos/std_algorithm_setops.html#.setDifference > I tried the setDifference but does seem to be working as expected From the documentation of setDifference: "The two ranges are assumed to be sorted by less." In other words, you will need to sort your ranges first. Either pre-sort them as string[] a = ["test1", "test2", "test3", "test4"]; string[] b = ["test2", "test4"]'; or call setDifference(a.sort(), b.sort()). -- Biotronic |
Copyright © 1999-2021 by the D Language Foundation