Re: Real simple question... for good programmers

2022-10-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/22/22 5:53 PM, WhatMeWorry wrote:



string[] tokens = userSID.output.split!isWhite;
writeln("tokens = ", tokens);

tokens = ["SID", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"S-1-5-21-3823976785-3597194045-4221507747-1779", "", "", "", "", "", 
"", "", ""]



Is there a clever way that I can discard all the extra null strings in 
the resultant string array?


I've been playing with isControl, whitespace, etc. Ready to rip my hair 
out.


Try just split without the `isWhite`. If you look at the docs, you will see:

"When no delimiter is provided, strings are split into an array of 
words, using whitespace as delimiter. Runs of whitespace are merged 
together (no empty words are produced)."


-Steve



Re: Real simple question... for good programmers

2022-10-22 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 22 October 2022 at 21:53:05 UTC, WhatMeWorry wrote:



string[] tokens = userSID.output.split!isWhite;
writeln("tokens = ", tokens); 

[...]
Is there a clever way that I can discard all the extra null 
strings in the resultant string array?


Easiest way is to use [`filter`][1]. Here's an example:

```d
import std.algorithm: splitter, filter;
import std.uni: isWhite; // or use std.ascii for non-unicode input
import std.array: array;
import std.stdio: writeln;

string exampleText =
"Hello   123-456-ABCx\ny\tz\r\nwvu   goodbye";

void main()
{
string[] tokens = exampleText
.splitter!isWhite
.filter!(t => t.length > 0)
.array;
writeln("tokens = ", tokens);
}
```

I've also used the lazily-evaluated [`splitter`][2] instead of 
the eagerly-evaluated `split`, to avoid allocating a temporary 
array unnecessarily.


[1]: 
https://phobos.dpldocs.info/std.algorithm.iteration.filter.html
[2]: 
https://phobos.dpldocs.info/std.algorithm.iteration.splitter.3.html


Re: Real simple question... for good programmers

2022-10-22 Thread Daniel via Digitalmars-d-learn

On Saturday, 22 October 2022 at 22:01:09 UTC, Enjoys Math wrote:

On Saturday, 22 October 2022 at 21:53:05 UTC, WhatMeWorry wrote:



string[] tokens = userSID.output.split!isWhite;
writeln("tokens = ", tokens); 

tokens = ["SID", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", 
"S-1-5-21-3823976785-3597194045-4221507747-1779", "", "", "", 
"", "", "", "", ""]	



Is there a clever way that I can discard all the extra null 
strings in the resultant string array?


I've been playing with isControl, whitespace, etc. Ready to 
rip my hair out.




Why not `strip`?  Works on ranges:
https://dlang.org/phobos/std_algorithm_mutation.html#.strip


Strip won't work because it only works on the beginning and ends 
of the range.  What you want is `remove`.  See my other MWE post.




Re: Real simple question... for good programmers

2022-10-22 Thread Enjoys Math via Digitalmars-d-learn

__MWE Code:__
```
module DlangForumsMWE;

import std.stdio;
import std.algorithm.mutation;

int main()
{
   //string[] tokens = userSID.output.split!isWhite;
   //writeln("tokens = ", tokens);

   auto tokens = ["SID", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "",
  "", "", "", 
"S-1-5-21-3823976785-3597194045-4221507747-1779", "", "", "", "", 
"", "", "", ""];


   writeln("Before:\n", tokens);

   writeln();
   tokens = tokens.remove!(x => x == "");
   writeln("After:\n", tokens);

   readln();
   return 0;
}
```

__Outputs:__
```
Before:
["SID", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "S-1-5-21-3823976785-3597194045-4221507747-1779", "", "", "", 
"", "", "", "", ""]


After:
["SID", "S-1-5-21-3823976785-3597194045-4221507747-1779"]
```




Re: Real simple question... for good programmers

2022-10-22 Thread Ali Çehreli via Digitalmars-d-learn

On 10/22/22 14:53, WhatMeWorry wrote:
>
>
> string[] tokens = userSID.output.split!isWhite;
> writeln("tokens = ", tokens);

Could you please show minimal compilable code that demonstrates the 
issue. I spent some time with some guesses but failed (to get my code to 
compile with std.array.split).


Ali

P.S. Sorry for also sending email.


Re: Real simple question... for good programmers

2022-10-22 Thread Enjoys Math via Digitalmars-d-learn

On Saturday, 22 October 2022 at 21:53:05 UTC, WhatMeWorry wrote:



string[] tokens = userSID.output.split!isWhite;
writeln("tokens = ", tokens); 

tokens = ["SID", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", 
"S-1-5-21-3823976785-3597194045-4221507747-1779", "", "", "", 
"", "", "", "", ""]	



Is there a clever way that I can discard all the extra null 
strings in the resultant string array?


I've been playing with isControl, whitespace, etc. Ready to rip 
my hair out.




Why not `strip`?  Works on ranges:
https://dlang.org/phobos/std_algorithm_mutation.html#.strip


Real simple question... for good programmers

2022-10-22 Thread WhatMeWorry via Digitalmars-d-learn




string[] tokens = userSID.output.split!isWhite;
writeln("tokens = ", tokens); 

tokens = ["SID", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "S-1-5-21-3823976785-3597194045-4221507747-1779", "", 
"", "", "", "", "", "", ""]	



Is there a clever way that I can discard all the extra null 
strings in the resultant string array?


I've been playing with isControl, whitespace, etc. Ready to rip 
my hair out.





Re: Real Simple Question?

2016-10-25 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/22/16 5:34 PM, WhatMeWorry wrote:

On Saturday, 22 October 2016 at 20:51:14 UTC, Jonathan M Davis wrote:

On Saturday, October 22, 2016 20:35:27 WhatMeWorry via
Digitalmars-d-learn wrote:

[...]


Just put it in a separate module and then import it. e.g.

file: mypackage/constants.d
==
module mypackage.constants;

GLfloat[] vertices =
[
  // Positions  // Texture Coords
  -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
   0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
   .
   (lots and lots of values)
   .
];


I would make this:

immutable GLfloat[] vertices

If you can do it. This will make it so it's a) accessible from pure 
functions, and b) will not create a separate thread-local copy for each 
thread (if you use threading).



Ok, but now I'm getting these error in my new mypackage/constants.d

...\common\vertex_data.d(5,15): Error: undefined identifier 'GLfloat'
...\common\vertex_data.d(53,12): Error: undefined identifier 'vec3'


It's difficult to know why without more code hints, but it appears that 
vertex_data.d needs to import the module that defines these types. Note 
that if you want to have an imported module provide these definitions 
from another module, then you need to public import the module.


So for instance (guessing at your code, since I don't know what you have 
for import statements):


mypackage/my_types.d:

alias GLfloat = float;
alias vec3 = GLfloat[3];

mypackage/constants.d:

public import mypackage.my_types; // this allows importers of 
constants.d to see the types used


GLfloat[] vertices = ...

vertex_data.d:

import mypackage.constants; // pulls in definitions from my_types.d

-Steve


Re: Real Simple Question?

2016-10-25 Thread tcak via Digitalmars-d-learn

On Saturday, 22 October 2016 at 21:34:36 UTC, WhatMeWorry wrote:
On Saturday, 22 October 2016 at 20:51:14 UTC, Jonathan M Davis 
wrote:

[...]


Ok, but now I'm getting these error in my new 
mypackage/constants.d


..\common\vertex_data.d(5,15): Error: undefined identifier 
'GLfloat'
..\common\vertex_data.d(53,12): Error: undefined identifier 
'vec3'


Is there a way to just suck in the text from say a .txt file 
that would not be compiled before inclusion in main.d?


You could format your array in JSON format, and read it in your 
program. That could be another solution.


Re: Real Simple Question?

2016-10-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, October 22, 2016 21:34:36 WhatMeWorry via Digitalmars-d-learn 
wrote:
> Ok, but now I'm getting these error in my new
> mypackage/constants.d
>
> ..\common\vertex_data.d(5,15): Error: undefined identifier
> 'GLfloat'
> ..\common\vertex_data.d(53,12): Error: undefined identifier 'vec3'

mypackage/constants.d needs to import the modules for any of the types it's
using, otherwise it doesn't know about them. Modules are not textually
included like header files are in C++ (compilation times would be _way_
worse if they were), so a module only has access to what it imports, and
it's not affected by anything that imports it.

> Is there a way to just suck in the text from say a .txt file that
> would not be compiled before inclusion in main.d?

String imports is a feature, but then you end up with multiple copies of the
array in your program instead of one, and string imports are almost always
the wrong solution.

- Jonathan M Davis



Re: Real Simple Question?

2016-10-22 Thread WhatMeWorry via Digitalmars-d-learn
On Saturday, 22 October 2016 at 20:51:14 UTC, Jonathan M Davis 
wrote:
On Saturday, October 22, 2016 20:35:27 WhatMeWorry via 
Digitalmars-d-learn wrote:

[...]


Just put it in a separate module and then import it. e.g.

file: mypackage/constants.d
==
module mypackage.constants;

GLfloat[] vertices =
[
  // Positions  // Texture Coords
  -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
   0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
   .
   (lots and lots of values)
   .
];
==

file: main.d
==
import mypackage.constants;

void main()
{
auto v = vertices;
}
==

Probably the key thing to remember is that when you compile 
your program, all of the modules that are part of your program 
rather than a separate library need to be compiled into it. 
Simply importing them isn't enough - though using either rdmd 
or dub make that easier.


This is the official documentation's page on modules:

http://dlang.org/spec/module.html

This is the chapter from Al's book that covers modules:

http://ddili.org/ders/d.en/modules.html

And you'd almost certainly benefit from simply reading Ali's 
book as a whole:


http://ddili.org/ders/d.en/index.html

- Jonathan M Davis


Ok, but now I'm getting these error in my new 
mypackage/constants.d


..\common\vertex_data.d(5,15): Error: undefined identifier 
'GLfloat'

..\common\vertex_data.d(53,12): Error: undefined identifier 'vec3'

Is there a way to just suck in the text from say a .txt file that 
would not be compiled before inclusion in main.d?







Re: Real Simple Question?

2016-10-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, October 22, 2016 20:35:27 WhatMeWorry via Digitalmars-d-learn 
wrote:
> This is probably so simple that there's no example anywhere.
>
> Basically, I've got a huge array definition (see below) which I
> reuse over and over again in different projects.
>
> GLfloat[] vertices =
> [
>  // Positions  // Texture Coords
>  -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
>   0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
>   .
>   (lots and lots of values)
>   .
>
> I'd like to place this one array into a separate file and just
> include it with a one line statement in many projects.  I'm
> thinking mixins and/or imports but then if I knew how to do this,
> I wouldn't be asking.
>
> Thanks in advance.

Just put it in a separate module and then import it. e.g.

file: mypackage/constants.d
==
module mypackage.constants;

GLfloat[] vertices =
[
  // Positions  // Texture Coords
  -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
   0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
   .
   (lots and lots of values)
   .
];
==

file: main.d
==
import mypackage.constants;

void main()
{
auto v = vertices;
}
==

Probably the key thing to remember is that when you compile your program,
all of the modules that are part of your program rather than a separate
library need to be compiled into it. Simply importing them isn't enough -
though using either rdmd or dub make that easier.

This is the official documentation's page on modules:

http://dlang.org/spec/module.html

This is the chapter from Al's book that covers modules:

http://ddili.org/ders/d.en/modules.html

And you'd almost certainly benefit from simply reading Ali's book as a
whole:

http://ddili.org/ders/d.en/index.html

- Jonathan M Davis



Real Simple Question?

2016-10-22 Thread WhatMeWorry via Digitalmars-d-learn

This is probably so simple that there's no example anywhere.

Basically, I've got a huge array definition (see below) which I 
reuse over and over again in different projects.


GLfloat[] vertices =
[
// Positions  // Texture Coords
-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
 0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
 .
 (lots and lots of values)
 .

I'd like to place this one array into a separate file and just 
include it with a one line statement in many projects.  I'm 
thinking mixins and/or imports but then if I knew how to do this, 
I wouldn't be asking.


Thanks in advance.