www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How the hell to split multiple delims?

reply AlphaPurned <Alpha Beta.com> writes:
I've tried 10 different ways with split and splitter, I've used 
all the stuff that people have said online but nothing works. I 
always get a template mismatch error.

Why is something so easy to do so hard in D?

auto toks = std.regex.split(l, Regex("s"));
auto toks = std.regex.splitter(l, Regex("s"));
auto toks = std.regex.splitter(l, ctRegex!r"\.");
Feb 15 2020
next sibling parent mipri <mipri minimaltype.com> writes:
On Saturday, 15 February 2020 at 11:32:42 UTC, AlphaPurned wrote:
 I've tried 10 different ways with split and splitter, I've used 
 all the stuff that people have said online but nothing works. I 
 always get a template mismatch error.

 Why is something so easy to do so hard in D?

 auto toks = std.regex.split(l, Regex("s"));
 auto toks = std.regex.splitter(l, Regex("s"));
 auto toks = std.regex.splitter(l, ctRegex!r"\.");
Why do you think this doesn't work? I'm not just asking this to be rude. Your problem is likely in the rest of your code. $ rdmd --eval 'split("the quick,brown:fox",regex("[ ,:]")).writeln' ["the", "quick", "brown", "fox"] $ rdmd --eval 'typeid(split("the quick,brown:fox",regex("[ ,:]"))).writeln' immutable(char)[][] $ rdmd --eval 'splitter("the quick,brown:fox",regex("[ ,:]")).writeln' ["the", "quick", "brown", "fox"] $ rdmd --eval 'typeid(splitter("the quick,brown:fox",regex("[ ,:]"))).writeln' std.regex.Splitter!(cast(Flag)false, string, Regex!char).Splitter That final value is a range type. Consider the guides at the top of https://dlang.org/phobos/std_range.html Ranges take a while to get comfortable with.
Feb 15 2020
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/15/20 6:32 AM, AlphaPurned wrote:
 I've tried 10 different ways with split and splitter, I've used all the 
 stuff that people have said online but nothing works. I always get a 
 template mismatch error.
 
 Why is something so easy to do so hard in D?
 
 auto toks = std.regex.split(l, Regex("s"));
 auto toks = std.regex.splitter(l, Regex("s"));
 auto toks = std.regex.splitter(l, ctRegex!r"\.");
What type is 'l'? -Steve
Feb 15 2020
parent reply AlphaPurned <Alpha Beta.com> writes:
On Saturday, 15 February 2020 at 14:35:59 UTC, Steven 
Schveighoffer wrote:
 On 2/15/20 6:32 AM, AlphaPurned wrote:
 I've tried 10 different ways with split and splitter, I've 
 used all the stuff that people have said online but nothing 
 works. I always get a template mismatch error.
 
 Why is something so easy to do so hard in D?
 
 auto toks = std.regex.split(l, Regex("s"));
 auto toks = std.regex.splitter(l, Regex("s"));
 auto toks = std.regex.splitter(l, ctRegex!r"\.");
What type is 'l'? -Steve
l happened to be a char ;/ I had auto s = ...; ... foreach(l; s) splitter(l,...) and I was thinking s was an array but it happened to be a string so l was char. The error message made me think(because I was expecting it to just work) it was with the regex since I remember some issues with regex and I haven't used them in a while so I thought maybe I was doing something wrong. It would be far better if the error message just told me that l is the wrong type for splitter rather than printing out template constraints and such. When I see a bunch of lines of irrelevant error code it just makes me close my eyes. This was a programming bug on my part but it goes to the root that error messages could be better. Could they not simply print out the id of the variable and the type that is wrong rather than making it seem like the template is wrong(as if I'm not using it properly) or both. If it would have said something like "type l is of char while splitter expects string" I would have realized the bug immediately. I didn't go down that path because I thought l was a string. I was reading a text: readText("file") and for some reason I thought it would read it by lines or I was going to split it and forgot... so I was getting one large string rather than array of strings. This then cascaded the problem and because of another issue of not using D for a while it confounded. The error that D displayed was not registering and too much useless info(for the actual error). It would be very nice if D could have more user friendly error messages that try to pinpoint the bug rather than just throwing out generic information[which may or may not be all that helpful]. D should try to have some heuristic that figures out the actual real error and give the relevant info. If it mentioned l or s as being of the wrong type(which it could figure out where s comes from through l and the foreach) then I think I would have saved about 15 minutes and not have written this post. This is litterally the error(When I change s back to a string) 1>Test.d(31): error : template `std.regex.split` cannot deduce function from argument types `!()(immutable(char), CTRegexWrapper!char)`, candidates are: 1>C:\Data\ldc2-1.17.0-windows-multilib\bin\..\import\std\regex\package.d(1603): `std.regex.split(String, RegEx)(String input, RegEx rx) if (isSomeString!String && isRegexFor!(RegEx, String))` 1>Test.d(61): error : template instance `Test.main.rate!(d, "")` error instantiating 1>Test.d(66): error : cannot implicitly convert expression `l` of type `immutable(char)` to `string` 1>Test.d(31): error : template `std.regex.split` cannot deduce function from argument types `!()(immutable(char), CTRegexWrapper!char)`, candidates are: 1>C:\Data\ldc2-1.17.0-windows-multilib\bin\..\import\std\regex\package.d(1603): `std.regex.split(String, RegEx)(String input, RegEx rx) if (isSomeString!String && isRegexFor!(RegEx, String))` 1>Test.d(79): error : template instance `Test.main.rate!(res, " ")` error instantiating This is very annoying. It talks about regex, CTRegexWrapper... so much BS for such a simple error. To me these error messages are counter productive. It's just a wall of crap that I have to sift through and analyze and then reason to the bug. 1>Test.d(66): error : cannot implicitly convert expression `l` of type `immutable(char)` to `string` I realize the error is right there but notice how it is buried within several lines of BS. See all the regex references? That is why I thought it was a problem with regex and it led me on a wild goose chase. I figured that it had to be a regex problem(again, because I was assuming l was a string). I realize it was my faulty logic, but it was greatly exacerbated by all the junk D prints out thinking it helps(maybe it does sometimes but sometimes it doesn't).
Feb 16 2020
parent evilrat <evilrat666 gmail.com> writes:
On Sunday, 16 February 2020 at 09:57:26 UTC, AlphaPurned wrote:
 1>Test.d(31): error : template ...
 1>Test.d(61): error : template ...
 1>Test.d(66): error : cannot implicitly convert expression `l` 
 of type `immutable(char)` to `string`
 1>Test.d(31): error : template ...
 1>Test.d(79): error : template ...
You can clearly see it all starts with 'template' except your code. Did you write any templates? No? Well then. How do you think library writers can point you out to the problem? What if you do template with template? BOOM. Don't get me wrong I'm not protecting the existing order, but.. Have you ever thinked about why so much 'junk' for templates? If not, here is a little comparison - whenever you use templates you've stepping into completely dynamic typing territory. Have you ever written any JS or Python code and then tried to make sense out of it when refactor? Yep, same shit here. Compiler have no idea what nonsense is all this junk, until you use it. And this is awesome because in dynamic typing languages you won't know it happens until you run it!
 This is very annoying. It talks about regex, CTRegexWrapper...

 so much BS for such a simple error. To me these error messages 
 are counter productive. It's just a wall of crap that I have to 
 sift through and analyze and then reason to the bug.
(no offence, you can ignore this) so ignorant, much attitude, such wow... You won't believe it but sifting through and analyze is just but regular programming stuff.
Feb 16 2020
prev sibling parent reply Craig Dillabaugh <craig.dillabaugh gmail.com> writes:
On Saturday, 15 February 2020 at 11:32:42 UTC, AlphaPurned wrote:
 I've tried 10 different ways with split and splitter, I've used 
 all the stuff that people have said online but nothing works. I 
 always get a template mismatch error.

 Why is something so easy to do so hard in D?

 auto toks = std.regex.split(l, Regex("s"));
 auto toks = std.regex.splitter(l, Regex("s"));
 auto toks = std.regex.splitter(l, ctRegex!r"\.");
I had the same problem myself recently, and almost ended up here to ask the same question as you but stumbled across the following (ugly) solution without using regexs. char[] line = "Split this by#space or#sign."
Feb 15 2020
parent Paul Backus <snarwin gmail.com> writes:
On Saturday, 15 February 2020 at 22:30:03 UTC, Craig Dillabaugh 
wrote:
 On Saturday, 15 February 2020 at 11:32:42 UTC, AlphaPurned 
 wrote:
 I've tried 10 different ways with split and splitter, I've 
 used all the stuff that people have said online but nothing 
 works. I always get a template mismatch error.

 Why is something so easy to do so hard in D?

 auto toks = std.regex.split(l, Regex("s"));
 auto toks = std.regex.splitter(l, Regex("s"));
 auto toks = std.regex.splitter(l, ctRegex!r"\.");
I had the same problem myself recently, and almost ended up here to ask the same question as you but stumbled across the following (ugly) solution without using regexs. char[] line = "Split this by#space or#sign."
This works for me: import std; void main() { string line = "Split this by#space or#sign."; writeln(parts); // ["Split", "this", "by", "space", "or", "sign."] }
Feb 15 2020