Re: std.algorithm for changing array values

2014-01-15 Thread bearophile
Kapps: One thing to keep in mind is that structs are passed by value, so this foreach would be operating on a copy of the entry. So your setting the value to 42 would have no effect. Instead you would need "foreach(ref entry; entries)" in order to have the change take effect (regardless of whe

Re: std.algorithm for changing array values

2014-01-15 Thread Meta
On Wednesday, 15 January 2014 at 20:34:32 UTC, Andre wrote: Hi, I checked std.algorithm but have no glue which functions I can use to rewrite following code with some nice algorithm functions. I have an array of structs. I want to check for a specific key and if found I change the value. If it

Re: std.algorithm for changing array values

2014-01-15 Thread Kapps
On Wednesday, 15 January 2014 at 20:34:32 UTC, Andre wrote: foreach(entry;entries){ if (entry.key == 3){ entry.value = 42; found = true; } } One thing to keep in mind is that structs are passed by v

Re: std.algorithm for changing array values

2014-01-15 Thread Andre
Am 15.01.2014 21:54, schrieb anonymous: If you want to stop after the first match: auto f = entries.find!(d => d.key == 3); if(f.empty) entries ~= Data(3,42); else f.front.value = 42; If there can be duplicates and you want to change them all: auto f = entries.filter!(d => d.key == 3); if(f.em

Re: std.algorithm for changing array values

2014-01-15 Thread anonymous
On Wednesday, 15 January 2014 at 20:54:06 UTC, anonymous wrote: On Wednesday, 15 January 2014 at 20:34:32 UTC, Andre wrote: Hi, I checked std.algorithm but have no glue which functions I can use to rewrite following code with some nice algorithm functions. I have an array of structs. I want to

Re: std.algorithm for changing array values

2014-01-15 Thread anonymous
On Wednesday, 15 January 2014 at 20:34:32 UTC, Andre wrote: Hi, I checked std.algorithm but have no glue which functions I can use to rewrite following code with some nice algorithm functions. I have an array of structs. I want to check for a specific key and if found I change the value. If it

Re: std.algorithm for changing array values

2014-01-15 Thread TheFlyingFiddle
On Wednesday, 15 January 2014 at 20:34:32 UTC, Andre wrote: Hi, I checked std.algorithm but have no glue which functions I can use to rewrite following code with some nice algorithm functions. I have an array of structs. I want to check for a specific key and if found I change the value. If it