Sort Associative Array by Key

2019-08-25 Thread Samir via Digitalmars-d-learn
Is there a way to output the values of an associative array based on the lexicographic order of the keys? For example, if foo = ["VXE":8, "BZP":5, "JLC":2], I'd like to output something like: 5 2 8 Thanks! Samir

Re: Sort Associative Array by Key

2019-08-27 Thread Samir via Digitalmars-d-learn
On Sunday, 25 August 2019 at 17:01:23 UTC, a11e99z wrote: auto foo = ["VXE":8, "BZP":5, "JLC":2]; foo.byPair.array.sort!"a[0] On Sunday, 25 August 2019 at 19:03:10 UTC, JN wrote: I think normal lambdas are better than these string ones: foo.byPair.array.sort!((a, b) => a[0] < b[0]).map!(a =>

Re: += on associative arrays leads to surprising result

2019-08-27 Thread Samir via Digitalmars-d-learn
On Tuesday, 27 August 2019 at 16:12:07 UTC, berni wrote: What's your oppinion on this? As someone relatively new to programming in general and to D in particular, this behavior does, on the surface, seem inconsistent. Good to see that a bug exists for this, per ag0aep6g. I never understoo

Re: "Error: function expected before (), not module *module* of type void

2018-09-21 Thread Samir via Digitalmars-d-learn
On Monday, 24 March 2008 at 17:41:11 UTC, Steven Schveighoffer wrote: I know you fixed the problem, but just an FYI, the reason is because when you import rollDice, you bring both rollDice the module and rollDice the function into the global namespace (which confuses the compiler 'cause it does

Re: "Error: function expected before (), not module *module* of type void

2018-09-21 Thread Samir via Digitalmars-d-learn
On Saturday, 22 September 2018 at 01:58:57 UTC, Adam D. Ruppe wrote: You probably shouldn't name a module the same as a member anyway, and it should also have two names, like "module myproject.isprime;" But the fix here is to just use the full name. import isPrime; void main() { isPrime.isP

How to store unique values of array in another array

2018-10-18 Thread Samir via Digitalmars-d-learn
What is the proper way to find the unique values of an array and store them in another array? When I try: import std.stdio: writeln; import std.conv; import std.algorithm; void main() { int[] unsortedArray = [5, 3, 8, 5, 2, 3, 0, 8]; int[] uniqueArray; uniqueArray = uniq(sort(unso

Re: How to store unique values of array in another array

2018-10-18 Thread Samir via Digitalmars-d-learn
On Thursday, 18 October 2018 at 18:53:06 UTC, Adam D. Ruppe wrote: But, if you need to copy it into a new array, use `.array` at the end. Thanks. That did the trick. But if I may, what is the difference between uniqueArray = uniq(sort(unsortedArray)).array; and uniqueArray = unsortedArra

Re: How to store unique values of array in another array

2018-10-18 Thread Samir via Digitalmars-d-learn
On Thursday, 18 October 2018 at 19:25:26 UTC, Adam D. Ruppe wrote: Which is better simply depends on which one reads better to you. Thanks again, Adam.

Why is dynamic array length required here?

2018-10-18 Thread Samir via Digitalmars-d-learn
I am working my way through the exercises in the "Programming in D" tutorial (http://ddili.org/ders/d.en/arrays.html). Why is the line assigning the length of the dynamic array required? /* Write a program that asks the user how many values will be entered and then reads all of them. Have the

Re: Why is dynamic array length required here?

2018-10-21 Thread Samir via Digitalmars-d-learn
Stanislav, Ali, Mike -- Thank you all for your thoughtful and helpful replies to my queries. Apologies that it has taken this long to reply to you. I still haven't been able to find time to go through all of the code examples provided but hope to do so later this week. If I have additional q

Writing Program Without main Function

2018-12-07 Thread Samir via Digitalmars-d-learn
Is it possible to write and execute a D program without a main function? Most of my programs will start with some `import` statements, followed by any functions and then ending with the `main` function (e.g. `void main() {`). As I am just a beginner to programming and still new to D, I have

Re: Writing Program Without main Function

2018-12-07 Thread Samir via Digitalmars-d-learn
Ok. Upon further investigation, I think I see what is going on. Most of the repos I am skimming are for this year's Advent of Code. They structure their repo with an `app.d` file which does contain a `main` function but this program is structured such that it imports the files I was looking

Re: Writing Program Without main Function

2018-12-08 Thread Samir via Digitalmars-d-learn
On Saturday, 8 December 2018 at 03:30:30 UTC, Jonathan M Davis wrote: There's one main per program, not per module. Where main lives depends on how the program was written, but dub encourages putting it in app.d, because that's what it generates when you create a new dub project. imports then p

9999999999999999.0 - 9999999999999998.0

2019-01-05 Thread Samir via Digitalmars-d-learn
I saw the following thread[1] today on Hacker News that discusses an article that compares how various languages compute .0 - 9998.0. A surprisingly large number of languages return 2 as the answer. I ran the following which returned 1: import std.stdio: writeln;

Re: 9999999999999999.0 - 9999999999999998.0

2019-01-06 Thread Samir via Digitalmars-d-learn
On Sunday, 6 January 2019 at 01:05:08 UTC, Adam D. Ruppe wrote: That's because it is done at compile time, since both are compile-time constants. The compiler will evaluate it using the maximum precision available to the compiler, ignoring your request to cast it to double (which annoys some pe

Error with matplotlib

2019-02-17 Thread Samir via Digitalmars-d-learn
I am trying to run the code from the "Simple Example" listed in the matplotlib-d package page[1] and am running into the following error: $ dub build Performing "debug" build using /usr/home/samir/dlang/dmd-2.082.0/freebsd/bin64/dmd for x86_64. matplotlib-d 0.1.4: building configuration "libr

Re: Error with matplotlib

2019-02-18 Thread Samir via Digitalmars-d-learn
On Monday, 18 February 2019 at 18:27:17 UTC, Andre Pany wrote: Therefore python needs to be installed and also matplotlib.pyplot. Hi Andre, I do have both python3 and matplotlib installed: $ python3 Python 3.6.7 (default, Jan 10 2019, 01:15:48) [GCC 4.2.1 Compatible FreeBSD Clang 6.0.0 (t

Re: Windows Defender won't let me install DMD

2019-02-18 Thread Samir via Digitalmars-d-learn
On Monday, 18 February 2019 at 16:32:26 UTC, belkin wrote: Obviously I need a compiler (which one is best ). I too am a beginner (both at programming and at D). I have been sticking to the dmd compiler (over others such as gdc or ldc) mainly because that is what the two reference books[1] I

Re: Windows Defender won't let me install DMD

2019-02-18 Thread Samir via Digitalmars-d-learn
On Monday, 18 February 2019 at 19:44:56 UTC, Samir wrote: that is what the two reference books[1] I am using to learn D have been using. Idiot me forgot to mention the two resources I have been using: (1) "Programming in D", by Ali Çehreli, available at http://ddili.org/ders/d.en/index.html (2

Re: Error with matplotlib

2019-02-18 Thread Samir via Digitalmars-d-learn
On Monday, 18 February 2019 at 20:30:23 UTC, Andre Pany wrote: The issue is, the dub package is using python 2 (it executes application python) while you have installed matplotlib for Python 3 (application python3). Thank you for that! After installing the version of matplotlib for python2 a

Re: Error with matplotlib

2019-02-18 Thread Samir via Digitalmars-d-learn
On Monday, 18 February 2019 at 21:50:25 UTC, Andre Pany wrote: In the meantime you could adapt the python script on your local file system in the dub packages cache folder as described here https://github.com/koji-kojiro/matplotlib-d/pull/11/files Andre, Thank you very much for your help toda

Re: Error with matplotlib

2019-02-19 Thread Samir via Digitalmars-d-learn
On Tuesday, 19 February 2019 at 11:57:00 UTC, Andre Pany wrote: The python script generates a list of available functions in the python package. This list will be used to generate D coding at compilation time. That is the reason, the error occurs in a mixin. Unfortunately one function had the sa

Help with Regular Expressions (std.regex)

2019-03-03 Thread Samir via Digitalmars-d-learn
I am belatedly working my way through the 2018 edition of the Advent of Code[1] programming challenges using D and am stumped on Problem 3[2]. The challenge requires you to parse a set of lines in the format: #99 @ 652,39: 24x23 #100 @ 61,13: 15x24 #101 @ 31,646: 16x28 I would like to store e

Re: Help with Regular Expressions (std.regex)

2019-03-04 Thread Samir via Digitalmars-d-learn
On Sunday, 3 March 2019 at 19:27:17 UTC, user1234 wrote: oops forgot the bang auto allMatches = matchAll(line, pattern).map!(a => a.hit).array; Thanks, user1234! Looks like `map` is another topic I need to read up upon. I slightly modified your suggestion and went with: auto allMatches

Re: Help with Regular Expressions (std.regex)

2019-03-05 Thread Samir via Digitalmars-d-learn
On Monday, 4 March 2019 at 18:57:34 UTC, dwdv wrote: There is also std.file.slurp which makes this quite easy: slurp!(int, int, int, int, int)("03.input", "#%d @ %d,%d: %dx%d"); That's brilliant! This language just keeps putting a smile on my face every time I learn something new like this!

Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn
I am trying to read from a text file using the following code: import std.stdio; import std.string; void main() { File file = File("test.txt"); string line; while (!file.eof()) { line = strip(file.readln()); if (line[0] == '>') { // line 10 writel

Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn
On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote: stripping the last line could result in an empty line if it just has strippable characters? The last line of the file is just text but without a newline (\n) character or any other whitespace character at the end. I get the same error wh

Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn
On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote: stripping the last line could result in an empty line if it just has strippable characters? The last line is just the text of the last line. There is no newline character at the end. I also get the same error if I remove the strip functi

Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn
On Sunday, 16 June 2019 at 23:55:41 UTC, lithium iodate wrote: There is *very* likely to be a terminating new-line at the end of the file (many editors add one without asking!). If that the case, the last line seen by the loop will be empty and you must not attempt to access any elements. On

Re: Range violation error when reading from a file

2019-06-17 Thread Samir via Digitalmars-d-learn
On Monday, 17 June 2019 at 03:46:11 UTC, Norm wrote: On Monday, 17 June 2019 at 00:22:23 UTC, Samir wrote: Any suggestions on how to rectify? You could change the IF to `if(line.length > 0 && line[0] == '>')` Thanks, Norm. That seemed to do the trick and fixed the error. On Monday, 17 J

Re: Range violation error when reading from a file

2019-06-20 Thread Samir via Digitalmars-d-learn
On Tuesday, 18 June 2019 at 09:42:41 UTC, aliak wrote: On Tuesday, 18 June 2019 at 01:15:54 UTC, Samir wrote: On Monday, 17 June 2019 at 03:46:11 UTC, Norm wrote: That's because you're using write*ln*. So even though line is empty, you still output a new line. Curious. I am going to have to

Using std.algorithm.iteration to Calculate Hamming Distance

2019-06-23 Thread Samir via Digitalmars-d-learn
D already has a function to calculate the Levenshtein distance[1]. I am trying to come up with a function to calculate the Hamming distance[2] between two strings, `a` and `b`. So far, this seems to work: foreach (i, j; zip(a, b)) { if (i != j) ++hammingDistance; } Is there a wa

Re: Using std.algorithm.iteration to Calculate Hamming Distance

2019-06-23 Thread Samir via Digitalmars-d-learn
On Sunday, 23 June 2019 at 13:29:25 UTC, KnightMare wrote: zip( "hello world", "Hello World" ).map!"a[0] != a[1]".sum Excellent! Thank you!

Finding the maxElement of Two-Dimensional Array

2019-06-30 Thread Samir via Digitalmars-d-learn
How come this works: int[][] ta = [[2, 1, 4, 3], [3, 10, 2, 5]]; writeln(ta[1].maxElement); // 10 but I get an error when specifying the number of elements when declaring the array: int[4][2] ta = [[2, 1, 4, 3], [3, 10, 2, 5]]; writeln(ta[1].maxElement); // get error on this line Error: temp

Re: Finding the maxElement of Two-Dimensional Array

2019-06-30 Thread Samir via Digitalmars-d-learn
On Sunday, 30 June 2019 at 15:38:42 UTC, a11e99z wrote: try to take slice from static arrays writeln(ta[1][].maxElement); That does what I am looking for. Thank you for the quick reply!

Finding Max Value of Column in Multi-Dimesional Array

2019-07-04 Thread Samir via Digitalmars-d-learn
Is there a cleaner way of finding the maximum value of say the third column in a multi-dimensional array than this? int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]]; writeln([p[0][2], p[1][2], p[2][2]].max); I've tried the following writeln([0, 1, 2].map!(p[a][2]).max); but get an "Error: undefine

Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread Samir via Digitalmars-d-learn
On Friday, 5 July 2019 at 01:41:38 UTC, 9il wrote: You may want to take a look into mir-algorithm [1] library. It contains ndsilce package [2] to work with multidimensional data. Thanks for referring me to this library, Ilya. I will have to check this out. While it seems a bit more complica

Re: To learn D

2019-07-05 Thread Samir via Digitalmars-d-learn
On Friday, 5 July 2019 at 13:56:18 UTC, Craig Dillabaugh wrote: Ali's book is targeted at beginners (see link below). I don't see why D wouldn't make a good first language. If your objective is to learn D, then I don't think learning C or Python is going to be help that much. Obviously if yo

Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread Samir via Digitalmars-d-learn
On Friday, 5 July 2019 at 00:54:15 UTC, Samir wrote: Is there a cleaner way of finding the maximum value of say the third column in a multi-dimensional array than this? int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]]; writeln([p[0][2], p[1][2], p[2][2]].max); I've tried the following writeln([0,

Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-06 Thread Samir via Digitalmars-d-learn
On Friday, 5 July 2019 at 19:56:54 UTC, ag0aep6g wrote: It works when you pass an actual callable instead, e.g. a lambda: p.map!(a => a[column]).maxElement.writeln; On Friday, 5 July 2019 at 20:22:14 UTC, dwdv wrote: Furthermore, Samir, the parameter `a` can be renamed to whatever you prefer