www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regex match in for loop

reply "seany" <seany uni-bonn.de> writes:
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.

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?
Jul 15 2014
next sibling parent "Brad Anderson" <eco gnuk.net> writes:
On Tuesday, 15 July 2014 at 20:18:58 UTC, seany 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.
There is nothing wrong with declaring a variable in a for loop. It's just limited to the scope inside the for loop so it's not useful if you need the variable after the for loop ends.
 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?
You can type out the return type. It can be a little tricky to determine sometimes though so you can also use typeof() like: typeof(match(greetings[i], r) m; to get the proper type. You should use matchFirst and matchAll instead of match and the "g" flag. It's more clear and easier to use. What are you trying to do in this bit of code? There may be a better overall structure.
Jul 15 2014
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Jul 15 2014