how to filter associative arrays with foreach ?

wjoe invalid at example.com
Mon Jun 21 15:32:09 UTC 2021


On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote:
> I often need to iterate through a filtered collection 
> (associative array) as following:
>
> ```d
> string strComputerIDunwanted = "WS2"; /// associative array key 
> to exclude
>
> foreach (strComputerID, udtComputer; udtComputers) { /// 
> .remove!(a => a == strComputerIDunwanted) ... ?
>
>    if (strComputerID != strComputerIDunwanted) {
>
>       ...
>
>    }
>
> }
> ```
>
> Is there a way to filter the collection at the foreach-level to 
> avoid the inner if ?

something like this ?

``` D
import std.array;
import std.algorithm;

     udtComputers.byPair
         .filter!(p => p.key != strComputerIDunwanted)
         .each!( (p) { /* foreach body */ } );
```


More information about the Digitalmars-d-learn mailing list