www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A converting problem in using "among" with arrays

reply pascal111 <judas.the.messiah.111 gmail.com> writes:
I next code, we have a data type problem 
"54.among(to!uint[10](y)).writeln;":

module main;

import std.stdio;
import std.string;
import std.conv;
import dcollect;
import std.math;
import std.algorithm;

     int main(string[] args)
     {
         int[] x=[23, 34,-88, 54, -90, -34];

         auto y=x.filter!(a=>a<0);

         foreach(i; y)
             i.write(", ");

         54.among(to!uint[10](y)).writeln;

     	return 0;
     }

Error message:
"hello.d|19|error: only one index allowed to index void|"
Jul 29 2022
next sibling parent reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Friday, 29 July 2022 at 22:09:47 UTC, pascal111 wrote:
 I next code, we have a data type problem 
 "54.among(to!uint[10](y)).writeln;":

 module main;

 import std.stdio;
 import std.string;
 import std.conv;
 import dcollect;
 import std.math;
 import std.algorithm;

     int main(string[] args)
     {
         int[] x=[23, 34,-88, 54, -90, -34];

         auto y=x.filter!(a=>a<0);

         foreach(i; y)
             i.write(", ");

         54.among(to!uint[10](y)).writeln;

     	return 0;
     }

 Error message:
 "hello.d|19|error: only one index allowed to index void|"
Did you mean `to!(uint[10])(y)`? This converts to `uint[10]`.
Jul 29 2022
parent reply pascal111 <judas.the.messiah.111 gmail.com> writes:
On Friday, 29 July 2022 at 22:12:54 UTC, Andrey Zherikov wrote:
 On Friday, 29 July 2022 at 22:09:47 UTC, pascal111 wrote:
 I next code, we have a data type problem 
 "54.among(to!uint[10](y)).writeln;":

 module main;

 import std.stdio;
 import std.string;
 import std.conv;
 import dcollect;
 import std.math;
 import std.algorithm;

     int main(string[] args)
     {
         int[] x=[23, 34,-88, 54, -90, -34];

         auto y=x.filter!(a=>a<0);

         foreach(i; y)
             i.write(", ");

         54.among(to!uint[10](y)).writeln;

     	return 0;
     }

 Error message:
 "hello.d|19|error: only one index allowed to index void|"
Did you mean `to!(uint[10])(y)`? This converts to `uint[10]`.
I want searching for value 54 in array y "54.among(y).writeln;", but it seems compiler complaints because the data type is "int[]", so I tried to convert "y" to "uint[]".
Jul 29 2022
parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Fri, Jul 29, 2022 at 10:32:11PM +0000, pascal111 via Digitalmars-d-learn
wrote:
[...]
 I want searching for value 54 in array y "54.among(y).writeln;", but
 it seems compiler complaints because the data type is "int[]", so I
 tried to convert "y" to "uint[]".
You're using the wrong function; .among is intended to be used in the cases where the list of values to search for are fixed or known at compile-time. If the values to search for changes at runtime, you want .canFind instead. For example: if ([1, 2, 3, 4].canFind(3)) writeln("found it!"); Or use .find if you need to retrieve the found value (e.g., if it's a structure and you matched on only one field). auto r = [ 1, 2, 3, 4 ].find(3); assert(r.front == 3); T -- It's amazing how careful choice of punctuation can leave you hanging:
Jul 29 2022
prev sibling next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Friday, 29 July 2022 at 22:09:47 UTC, pascal111 wrote:
 Error message:
 "hello.d|19|error: only one index allowed to index void|"
```d import std.stdio; void main() { int[] y = [-90, -88, -34]; /* ok but no compile y.to!uint[10] */ enum len = 10; y.length = len; int[len] v = y; v.writeln; // [-90, -88, -34, 0, 0, 0, 0, 0, 0, 0] int x = 54; /* or may be -54 because: */ assert(is(typeof(x) == int)); auto z = x.to!uint; /*)) ok but not be -54 because: */ assert(is(typeof(z) == uint)); x.writeln(" == ", z); /* but no compile z = -54 */ x = -54; } ``` SDB 79
Jul 29 2022
prev sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
FYI, you can use the markdown code tag so your code is properly 
rendered when viewed from the forums (make sure to tick the 
"Enable Markdown", right next to Send)


```
     ```D
     void my_function()
     {
     }
     ```
```

it'll be rendered like this:

```D
void my_function()
{
}
```
Jul 29 2022