www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Real simple question... for good programmers

reply WhatMeWorry <kheaser gmail.com> writes:
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.
	
Oct 22 2022
next sibling parent reply Enjoys Math <enjoysmath gmail.com> writes:
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
Oct 22 2022
parent Daniel Donnelly, Jr. <enjoysmath gmail.com> writes:
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.
Oct 22 2022
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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.
Oct 22 2022
prev sibling next sibling parent Enjoys Math <enjoysmath gmail.com> writes:
__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"]
```
Oct 22 2022
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
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-ABC x\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
Oct 22 2022
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Oct 22 2022