On Tue, Jul 15, 2014 at 08:18:55PM +0000, seany via Digitalmars-d-learn wrote: > Consider this: > > import std.stdio, std.regex, std.array, std.algorithms ; > > void main(string args[]) > { > > string[] greetings = ["hello", "hallo", "hoi", "salut"]; > > regex r = regex("hello", "g"); > > for(short i = 0; i < greetings.count(); i++) > { > > auto m = match(greetings[i], r); > } > > } > > To the best of my knowledge, declaring a variable inside a for loop is > illegal, you can not delacre the same variable repeatedly over the > iterations.
Says who? Each instance of 'm' only exists for the duration of a single loop iteration, so it's perfectly fine to reuse the same name the next time round. It would be a horribly crippled language if you couldn't declare temporary variables inside the loop body! > Also just the declaration auto m; outside the for loop does not make > sense either - auto needs an Right Hand Side expression. > > So what is the correct way of doing it? What you have is already (mostly) correct, except: (1) You misspelled 'std.algorithm' as 'std.algorithms'; (2) Your declaration of 'r' should use 'auto', not 'regex': auto r = regex("hello", "g"); Or, if you wish to be explicit, the correct type name is 'Regex!char': Regex!char r = regex("hello", "g"); Once you fix (1) and (2), the code will work just fine. T -- Indifference will certainly be the downfall of mankind, but who cares? -- Miquel van Smoorenburg