linear search using 'find' on an array of structs?

captain_fid bell.hue at gmail.com
Sat Mar 8 10:08:43 PST 2014


import std.container: find, equal, empty;
import std.container : SList;

struct c
{
         int idx;
         string name;
}

c[] clist = [ {1, "name1"}, {2, "name2"}, { 3, "name3" } ];
// c* clist = [ {1, "name1"}, {2, "name2"}, { 3, "name3" } ];

int
main()
{
         // Case-insensitive find of a string
         string[] s = [ "Hello", "world", "!" ];
         assert(!find!("toLower(a) == b")(s, "hello").empty);

         assert(!find!("toLower(a) == b")(clist.name, 
"name2").empty);
         return 0;
}

I went looking to replace several foreach statements. Can 'find' 
(in understand it's just a linear search) be used on an array of 
structures like above.

Example pulled and modified. Above code gives me (naturally) -
   ... no property 'name' for type 'cp[]'.

Interestingly, I had accidentally coded the commented out line 
before and it compiles correctly but will (as you guessed it) 
fail.

Sorry for the basics...


More information about the Digitalmars-d-learn mailing list