Prevent console line advancing on user input
Wondering if this is possible? Ask a user at input and wait for response: write("Is the sky blue? "); readf!" %s\n"(response); If the user's response is correct, I'd like to change the color of provided response to indicate it was correct then advance to the next line and ask a different question. If the user's response is incorrect, I'd like to clear the line and repeat the question without advancing down the screen vertically. I'm assuming it can be done by intercepting all input before the console processes them and outputting a '\r', a blank line the length of the screen, and another '\r' to get back to the beginning before reprinting the question. But I have no idea how to intercept the raw input. Would appreciate any pointers. Thanks --anonymouse
Re: short guide on getting started with D
On Wednesday, 5 April 2023 at 05:52:33 UTC, thinkunix wrote: Thank you for these remarks! My observations: #1 typo: After that, add this meso.build file: "meso.build" should be "meson.build" Fixed. #2 Does it matter where the dlang = import... block goes in the meson.build file? I added it after the "test(..." in the first block and it seemed to work, but... #3 where is dep? Is it a file or do I have to create it? I could only get you demo to work by removing the line dependencies: deps from meson.build. Then it created dub.json and dub ran it OK. No need for a reply, just my obvservations from someone who knows a little D and less meson. scot Well, I added sample `deps`. Basically this is something like: ``` phobos = dependency('phobos', version: '2.100.2', method: 'dub', static: true) boilerplate = dependency('boilerplate', version: '1.9.1', method: 'dub', static: true) serialized = dependency('serialized', version: '1.9.1', method: 'dub', static: true) deps = [ phobos, boilerplate, serialized ] ``` I'm new to D and meson as well, this is what I found somewhere on the inernet and it seemed to work for me.
Re: constant pointer failing to compile
On Thursday, 6 April 2023 at 00:59:12 UTC, Mathias LANG wrote: immutable ubyte[4] data = [1, 2, 3, 4]; Using a static array instead of a slice will do the trick. You can leave the `__gshared` if you want, but it is redundant on a global, initialized `immutable` variable. Yes, thank you, using static arrays did work. And thanks for the note on __gshared as well.
member func is best choice for pointers?
```d import std, core.lifetime; struct Node { Node* pNext; void func() { "Node::func %s".writeln( pNext); } } void func( Node* p ) { "::func %s(%s)".writeln( p, p == null ? p : p.next); } void main() { Node n; auto pn = &n; //cast( Node* )0; pn.func(); // prints: Node::func // WTF? // why called Node::func for Node* when ::func is right choice for it? } ``` problem: member func has invariant that **this** is not null (programmer thinks so). global func hasn't the one and programmer should check for it before doing something. when pn is null u've got AccessViolation/Signal_11 with no stacktrace. workaround: avoid overloaded UFCS for pointers
Re: foreach (i; taskPool.parallel(0..2_000_000)
On Thursday, 6 April 2023 at 01:44:15 UTC, H. S. Teoh wrote: D ranges are conceptually sequential, but the actual underlying memory access patterns depends on the concrete type at runtime. An array's elements are stored sequentially in memory, and arrays are ranges. But a linked-list can also have a range interface, yet its elements may be stored in non-consecutive memory locations. So the concrete type matters here; the range API only gives you conceptual sequentiality, it does not guarantee physically sequential memory access. Very helpful Teoh. Thanks again.
Re: Prevent console line advancing on user input
On 4/6/23 4:01 AM, anonymouse wrote: Wondering if this is possible? Ask a user at input and wait for response: write("Is the sky blue? "); readf!" %s\n"(response); If the user's response is correct, I'd like to change the color of provided response to indicate it was correct then advance to the next line and ask a different question. If the user's response is incorrect, I'd like to clear the line and repeat the question without advancing down the screen vertically. I'm assuming it can be done by intercepting all input before the console processes them and outputting a '\r', a blank line the length of the screen, and another '\r' to get back to the beginning before reprinting the question. But I have no idea how to intercept the raw input. You can't intercept raw input. It's the terminal that decides when to send you the line of text, you cannot control it via `stdin`. You need to use a terminal-control library, such as `arsd.terminal` https://github.com/adamdruppe/arsd/blob/master/terminal.d -Steve
regex matching but not capturing
My regex is matching but doesnt seem to be capturing. You may recognize this from the AOC challenges. file contains... **Valve AA has flow rate=0; tunnels lead to valves DD, II, BB** **Valve BB has flow rate=13; tunnels lead to valves CC, AA** **Valve CC has flow rate=2; tunnels lead to valves DD, BB** **... etc** ```d auto s = readText(filename); auto ctr = ctRegex!(`Valve ([A-Z]{2}).*=(\d+).+valves(,* [A-Z]{2})+`); foreach(c;matchAll(s, ctr)) { fo.writeln(c); } ``` produces... **["Valve AA has flow rate=0; tunnels lead to valves DD, II, BB", "AA", "0", ", BB"]** **["Valve BB has flow rate=13; tunnels lead to valves CC, AA", "BB", "13", ", AA"]** **["Valve CC has flow rate=2; tunnels lead to valves DD, BB", "CC", "2", ", BB"]** what I'm attempting to achieve and expect is, for instance, on the 1st line... [lead to valves DD, II, BB", "AA", "0", **", DD", ", II", ", BB"]**
Re: regex matching but not capturing
On Thursday, 6 April 2023 at 15:52:16 UTC, Paul wrote: My regex is matching but doesnt seem to be capturing. You may recognize this from the AOC challenges. file contains... **Valve AA has flow rate=0; tunnels lead to valves DD, II, BB** **Valve BB has flow rate=13; tunnels lead to valves CC, AA** **Valve CC has flow rate=2; tunnels lead to valves DD, BB** **... etc** ```d auto s = readText(filename); auto ctr = ctRegex!(`Valve ([A-Z]{2}).*=(\d+).+valves(,* [A-Z]{2})+`); foreach(c;matchAll(s, ctr)) { fo.writeln(c); } ``` produces... **["Valve AA has flow rate=0; tunnels lead to valves DD, II, BB", "AA", "0", ", BB"]** **["Valve BB has flow rate=13; tunnels lead to valves CC, AA", "BB", "13", ", AA"]** **["Valve CC has flow rate=2; tunnels lead to valves DD, BB", "CC", "2", ", BB"]** what I'm attempting to achieve and expect is, for instance, on the 1st line... [lead to valves DD, II, BB", "AA", "0", **", DD", ", II", ", BB"]** My understanding browsing the documentation is the matchAll returns a range of Captures (struct documented at https://dlang.org/phobos/std_regex.html#Captures). In your for loop I think c[0] will contain the current full match (current line that matches), c[1] will contain the first captured match ("AA" for first line), c.front[2] will contain "0" for first line, etc. There's probably logic somewhere that decides when a Capture is used as an argument to writeln, to just print the full match (line that matches) (making writeln(capture) the same as writeln(capture[0])
Re: regex matching but not capturing
On Thursday, 6 April 2023 at 16:27:23 UTC, Alex Bryan wrote: My understanding browsing the documentation is the matchAll returns a range of Captures (struct documented at https://dlang.org/phobos/std_regex.html#Captures). In your for loop I think c[0] will contain the current full match (current line that matches), c[1] will contain the first captured match ("AA" for first line), c.front[2] will contain "0" for first line, etc. Thanks Alex. Read some more and tried some different ways to access those repetitive ", cc" s on the end. I don't think my regex is capturing them.
Re: regex matching but not capturing
On 4/6/23 11:08, Paul wrote: ways to access those repetitive ", cc" s on the end. I don't think my regex is capturing them. Some internets think you are in parser territory: https://stackoverflow.com/questions/1407435/how-do-i-regex-match-with-grouping-with-unknown-number-of-groups Ali
Re: constant pointer failing to compile
On Thursday, 6 April 2023 at 00:46:26 UTC, Josh Holtrop wrote: ``` constarrptr.d(5): Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(ubyte)*)data` ``` Why? And how can I do the equivalent to what I have been doing in C? I want these pointers to be generated at compile time, not initialized at runtime. (the full project is generating this file containing the data array (which is much longer) and multiple pointer constants to locations within it) You have 2 options: First, to use .ptr, or second, to do the initialization in static this: ```d import std.stdio; static const char[] d; static const char * p; static this() { d = [97, 98, 99]; p = &d[0]; // == d.ptr; } void main() { printf("%.*s\n", cast(int)d.length, d.ptr); writeln("*p = ", *p); }/* Prints: abc *p = a */ ``` SDB@79
Re: constant pointer failing to compile
On Thursday, 6 April 2023 at 00:46:26 UTC, Josh Holtrop wrote: I am trying to port a small C project to D and am getting a compilation error I don't understand. It seems like the compiler is just missing some type hints. Try this: ```d import std.stdio; __gshared immutable ubyte[4] data = [1, 2, 3, 4]; __gshared immutable ubyte* p = data.ptr; int main() { writeln("*p = ", *p); return 0; } ``` Look how I provided the `ubyte[4]` hint to the compiler so it thinks that it's a constant. I'm not sure why this happens, but sometimes the compiler can't deduce static arrays, especially if you're explicitly saying that they're not static (`ubyte[]`).
Re: constant pointer failing to compile
On Thursday, 6 April 2023 at 20:23:29 UTC, Jacob Shtokolov wrote: On Thursday, 6 April 2023 at 00:46:26 UTC, Josh Holtrop wrote: I am trying to port a small C project to D and am getting a Ah, just noticed that other people have already responded! Sorry about that! BTW, your `&data[0]` C-like pointer will also work in D's version, although it's probably a bit uglier.
Re: Prevent console line advancing on user input
On Thursday, 6 April 2023 at 14:51:43 UTC, Steven Schveighoffer wrote: On 4/6/23 4:01 AM, anonymouse wrote: Wondering if this is possible? [snip] You need to use a terminal-control library, such as `arsd.terminal` https://github.com/adamdruppe/arsd/blob/master/terminal.d -Steve That works perfectly. Thanks.
Re: member func is best choice for pointers?
On Thursday, 6 April 2023 at 14:26:01 UTC, a11e99z wrote: member func has invariant that **this** is not null (programmer thinks so). global func hasn't the one and programmer should check for it before doing something ... I understand what you mean. When you try to access the last node of a linked list, you will get the segmentation fault if you are using a built-in viewer (toString). For example: ```d struct Node { int item; Node * back; string toString() { import std.format : format; return format("Node::%s (back)-> %s", item, back.item); } } void main() { import std.stdio : writeln; auto node1 = Node(41); auto node1Ptr = &node1; //printNode(node1Ptr);/* NOT COMPILE! node1.writeln;//* Main::41 (back)-> null */ auto node2 = Node(42); node2.back = node1Ptr; printNode(&node2); // Main::42 (back)-> 41 node2.writeln; // Node::42 (back)-> 41 } void printNode(Node * p) { import std.stdio : writefln; if(p.back is null) { writefln("Main::%s (back)-> null", p.item); } else { writefln("Main::%s (back)-> %s", p.item, p.back.item); } } ``` If you don't remove the comment line (just remove the // sign: toggle-comment) the above code will not compile. Because it is not connect to any node. @SDB