array of randomly generated names

spir denis.spir at
Fri Oct 15 12:50:53 PDT 2010


A few questions raised by a single func.

alias char[] Text ;

Text letters = ['a','b','c',...] ;

Text[] nameSet (uint count , uint size) {
	/* set of count random names of size size */
	Text[] names ; names.length = count ;
	Text name ; name.length = size ;
	for (int i=0 ; i<count ; i++) {
		for (int j=0 ; j<size ; j++)
		    name[j] = letters[uniform(0u,26u)] ;
		names[i].length = size ;
		names[i][] = name ;
	return names ;

1. In the  inner loop generating name, I have found neither a way to feed directly ints into name, nore a way to cast ints to chars using to! (also found no chr()). So, I had to list letters. But this wouldn't work with a wide range of unicode chars... How to build name directly from random ints?

2. I was surprised to get all names equal... Seems that "names[i] = name" actually copies a ref to the name. Is there another way to produce a copy than "names[i][] = name"?

3. As you see, I individually set the length of each names[i] in the outer loop. (This, only to be able to copy, else the compiler complains about unequals lengths.) How can I set the length of all elements of names once and for all?

4. Is there a kind of map(), or a syntax like list comprehension, to generate array content from a formula? (This would here replace both loops.)

5. Seems there is no auto-conversion between char[] and string. Thus, I use only char[], else I'm constantly blocked with immutability. But for this reason I cannot use nice facilities to construct text expressions, like format(). Also, I need to convert every literal, even chars, when they must go into a char[]. Grrr! Even to initialise:
    Text text = to!Text("start text") ;
Hints welcome.

Thank you,
-- -- -- -- -- -- --
vit esse estrany ☣

More information about the Digitalmars-d-learn mailing list