www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - arrays and foreach

reply "darkstalker" <slayerbeast gmail.com> writes:
i have this example program:

---
void main()
{
     int[3] a;
     foreach (p; a)
         p = 42;
     writeln(a);
}
---

after running it, i expect to get [42, 42, 42] but instead i get 
[0, 0, 0] (i know that you can do a[] = 42, it's just a trivial 
example). So it seems that you cannot write into the array 
because the elements are being passed by value each iteration. It 
possible to have 'p' passed by reference?
Apr 16 2012
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/17/12, darkstalker <slayerbeast gmail.com> wrote:
  It possible to have 'p' passed by reference?
Yes and the answer is in the question: foreach (ref p; a) p = 42;
Apr 16 2012
parent "darkstalker" <slayerbeast gmail.com> writes:
On Tuesday, 17 April 2012 at 00:00:57 UTC, Andrej Mitrovic wrote:
 On 4/17/12, darkstalker <slayerbeast gmail.com> wrote:
  It possible to have 'p' passed by reference?
Yes and the answer is in the question: foreach (ref p; a) p = 42;
thanks, it works
Apr 16 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/16/2012 04:56 PM, darkstalker wrote:
 i have this example program:

 ---
 void main()
 {
 int[3] a;
 foreach (p; a)
 p = 42;
 writeln(a);
 }
 ---

 after running it, i expect to get [42, 42, 42] but instead i get [0, 0,
 0] (i know that you can do a[] = 42, it's just a trivial example). So it
 seems that you cannot write into the array because the elements are
 being passed by value each iteration. It possible to have 'p' passed by
 reference?
Yes: foreach (ref p; a) Ali
Apr 16 2012
parent reply Somedude <lovelydear mailmetrash.com> writes:
Le 17/04/2012 02:01, Ali Çehreli a écrit :
 On 04/16/2012 04:56 PM, darkstalker wrote:
 i have this example program:

 ---
 void main()
 {
 int[3] a;
 foreach (p; a)
 p = 42;
 writeln(a);
 }
 ---

 after running it, i expect to get [42, 42, 42] but instead i get [0, 0,
 0] (i know that you can do a[] = 42, it's just a trivial example). So it
 seems that you cannot write into the array because the elements are
 being passed by value each iteration. It possible to have 'p' passed by
 reference?
Yes: foreach (ref p; a) Ali
Hi Ali, Sorry for hijacking this thread, but since you're around, I hope you'll see this message. As a D beginner, I'm browsing through your book. I wanted to tell you that there is something essential missing in it: how to compile. It's actually quite hard to find it in the official website, and since the first chapters are targetted at complete beginners, I thought it would be nice to add a few lines or pages about compilation, linking, debugging, etc. What do you think ?
Apr 17 2012
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 4/17/2012 4:42 PM, Somedude wrote:

 Ali
Hi Ali, Sorry for hijacking this thread, but since you're around, I hope you'll see this message. As a D beginner, I'm browsing through your book. I wanted to tell you that there is something essential missing in it: how to compile. It's actually quite hard to find it in the official website, and since the first chapters are targetted at complete beginners, I thought it would be nice to add a few lines or pages about compilation, linking, debugging, etc. What do you think ?
In the sidebar at dlang.org, under Documentation, is a link labeled "Tutorial". The very first code example shows a minimal program and how to compile it.
Apr 17 2012
parent reply Somedude <lovelydear mailmetrash.com> writes:
Le 17/04/2012 12:19, Mike Parker a écrit :
 On 4/17/2012 4:42 PM, Somedude wrote:
 
 Ali
Hi Ali, Sorry for hijacking this thread, but since you're around, I hope you'll see this message. As a D beginner, I'm browsing through your book. I wanted to tell you that there is something essential missing in it: how to compile. It's actually quite hard to find it in the official website, and since the first chapters are targetted at complete beginners, I thought it would be nice to add a few lines or pages about compilation, linking, debugging, etc. What do you think ?
In the sidebar at dlang.org, under Documentation, is a link labeled "Tutorial". The very first code example shows a minimal program and how to compile it.
Well, yes, that's sufficient for "Hello, world", but not for something barely more complex, i.e a function with a unit test. In this case, I had to type: rdmd -unittest --main test.d Without the --main, I would get linker errors, and couldn't find the reason for these errors. Happily, someone here explained me that the effect of the --main flag was to insert a main() function just for this case. Now, I see there is a mention of this in http://dlang.org/rdmd.html But without rdmd, I would compile with dmd, which has no --main switch, and it would still fail to link, because of lack of main(). The code I posted wasn't my code, and I knew it had worked, so I assumed it was correct, I didn't figure out that adding a main() was necessary given there was a unit test. And anyway, explaining in the book how to link is a necessary step imho. This is why I made this page more visible in the Wiki: http://prowiki.org/wiki4d/wiki.cgi?D__Tutorial/CompilingLinkingD
Apr 17 2012
parent reply David <d dav1d.de> writes:
 In this case, I had to type:
 rdmd -unittest --main test.d

 Without the --main, I would get linker errors, and couldn't find the
 reason for these errors. Happily, someone here explained me that the
 effect of the --main flag was to insert a main() function just for this
 case.
That's not surprising, try to write a C program with no main: ─[ArchBox][/tmp]╼ touch foo.c ─[ArchBox][/tmp]╼ LANG=C gcc foo.c /usr/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../crt1.o: In function `_start': (.text+0x18): undefined reference to `main' collect2: error: ld returned 1 exit status You get the linker errors, because there is no entry-point for your program, but that has nothing to do with D, it's a common behaviour for C, C++, D and other languages. And the unittest(-switch) is also good relativly good explained at http://dlang.org/unittest.html (dlang -> search -> unittest)
Apr 17 2012
parent "SomeDude" <lovelydear mailmetrash.com> writes:
On Tuesday, 17 April 2012 at 12:11:21 UTC, David wrote:
 In this case, I had to type:
 rdmd -unittest --main test.d

 Without the --main, I would get linker errors, and couldn't 
 find the
 reason for these errors. Happily, someone here explained me 
 that the
 effect of the --main flag was to insert a main() function just 
 for this
 case.
That's not surprising, try to write a C program with no main: ─[ArchBox][/tmp]╼ touch foo.c ─[ArchBox][/tmp]╼ LANG=C gcc foo.c /usr/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../crt1.o: In function `_start': (.text+0x18): undefined reference to `main' collect2: error: ld returned 1 exit status You get the linker errors, because there is no entry-point for your program, but that has nothing to do with D, it's a common behaviour for C, C++, D and other languages. And the unittest(-switch) is also good relativly good explained at http://dlang.org/unittest.html (dlang -> search -> unittest)
Yeah I understand, but I thought that by writing unit tests, I would implicitly add an entry point, but now I see how dumb an idea that is.
Apr 18 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/17/2012 12:42 AM, Somedude wrote:

 Sorry for hijacking this thread, but since you're around, I hope you'll
 see this message. As a D beginner, I'm browsing through your book.
 I wanted to tell you that there is something essential missing in it:
 how to compile. It's actually quite hard to find it in the official
 website, and since the first chapters are targetted at complete
 beginners, I thought it would be nice to add a few lines or pages about
 compilation, linking, debugging, etc.
 What do you think ?
Thank you very much for the feedback. I will do something about that. Although there is no hello world compilation, the dmd command line appears multiple times throughout the chapters. The 'assert and enforce' chapter shows it for the -release compiler switch and coincidentally I am on the 'Unit Testing' chapter as we speak, which includes the -unittest switch. :) But you are right, both dmd and rdmd command lines must appear earlier. Thank you, Ali
Apr 17 2012
parent reply Somedude <lovelydear mailmetrash.com> writes:
Le 17/04/2012 16:07, Ali Çehreli a écrit :
 On 04/17/2012 12:42 AM, Somedude wrote:
 
 Sorry for hijacking this thread, but since you're around, I hope you'll
 see this message. As a D beginner, I'm browsing through your book.
 I wanted to tell you that there is something essential missing in it:
 how to compile. It's actually quite hard to find it in the official
 website, and since the first chapters are targetted at complete
 beginners, I thought it would be nice to add a few lines or pages about
 compilation, linking, debugging, etc.
 What do you think ?
Thank you very much for the feedback. I will do something about that. Although there is no hello world compilation, the dmd command line appears multiple times throughout the chapters. The 'assert and enforce' chapter shows it for the -release compiler switch and coincidentally I am on the 'Unit Testing' chapter as we speak, which includes the -unittest switch. :) But you are right, both dmd and rdmd command lines must appear earlier. Thank you, Ali
Yes, I think that you have a lot of valuable information, but the organization is lacking. The advanced chapters look good, but the first "beginner" chapters can be . what struck me as an error in your "Hello world program" chapter, you give as an exercise: "Type or copy the hello world program into your editor as a source file, compile it, and run the program. The program should produce the following output: Hello world! If you are new to using a compiler, you will need to get help from the compiler documentation or, preferably, an experienced person. You can't benefit from the rest of this book if you can't compile and run programs." Spock would have said: "Illogical, captain!" At this point, you haven't told how to compile and run the program. Seriously, if your book is aimed at complete beginners (and the first chapters seem to say that), you really want to tell them what to do, not "go find the compiler documentation", else if they can do that, they simply don't need to read your book. For a complete beginner, it's very frustrating to have to read 4 chapters of fluff, then write the program, and still not knowing how to run the program. Where is the fun ? I can guarantee that at this point, 9 out of 10 beginners will drop the reading. FIRST show, THEN explain. Better yet: first show, then ask questions, and finally explain. So how would I have organized this chapter ? Something like this: --------------- The first program to show in most programming... Here is a hello world in D: ... Copy this program and save it in a file under the name hello.d .d is the extension for D source code. Now we'll compile it, that is to say the compiler will check that the syntax of this source code, and if it is valid D, will translate it into machine language and make it a program. Type in the command prompt: rdmd hello.d If you didn't make any mistake, you should have the impression that nothing happened. In the contrary, everything went well ! You can check that the executable file hello (or hello.exe under window) has been created. If instead the compiler barked at you lots of text, you probably made a mistake while copying. Correct it and retry. You will routinely make many mistakes when programming so that this process of correcting and compiling will get very familiar. Once this is done, type the name of the executable to run it. You should see Hello, world ! Congratulations, your first D program is running ! ------------ Then ONLY after that, I would proceed to the explanations. (As an exercise, I would ask also: what is the smallest valid D program ?) I would put the "Hello, world" chapter at the very beginning, right after "The practice of programming". This chapter would be enriched by an example on the computer, showing the values. The order of the beginning chapters I think should be: Intro Practice of programming Hello world writeln and write Compiler Fundamental types I would then merge "Assignment and order of evaluation" with "Variables" I would also remove the "Standard Input and Output Streams", they don't belong to an introduction. In fact, the problem is, you really want to separate your chapters into a sections, one as an introductory course, and one for more advanced explanations of the core language, and maybe one to introduce specific concepts of the standard library (your last chapters). I would put in Introduction section the first 19 chapters until "Redirecting input and output", as well as all the flow constructs (do-while, for, foreach, switch case, you really want to merge those with other chapters like while, these don't need separate chapters), ternary ops and associative arrays. With that, beginning programmers can have some fun. In the second section (core language), I would put File Program environment auto typeof name space litterals enum function function params Lazy ops Immutability Classes <-- I didn't see a chapter on this topic ? Intefaces Exceptions Scope Assert and Enforce In the third section (Generic Programming and advanced concepts), I would put: Templates Mixins Ranges Parallelism Concurrency synchronized __traits calloc etc, or 2 chapters for interoperations with C/asm Etc Finally, as appendices, I would put chapters about the tools: The Compiler <-- some more details about it The Linker <-- name mangling, etc Creating interface files, etc I think this organization would make much more sense, and would make a wonderful book. I hope this helps.
Apr 18 2012
next sibling parent reply Somedude <lovelydear mailmetrash.com> writes:
Le 18/04/2012 10:26, Somedude a écrit :
 Yes, I think that you have a lot of valuable information, but the
 organization is lacking. The advanced chapters look good, but the first
 "beginner" chapters can be .
 
... largely improved.
Apr 18 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2012 03:06 AM, Somedude wrote:
 Le 18/04/2012 10:26, Somedude a écrit :
 Yes, I think that you have a lot of valuable information, but the
 organization is lacking. The advanced chapters look good, but the first
 "beginner" chapters can be .
... largely improved.
Thank you, this is all very valuable information. Yes, I will make the improvements and I am very grateful. Although, I must say that there has been some reason in all of the decisions, which has been admittedly bad. At least it all started as a tutorial attached to a Turkish forum with a small but lively forum. It was fine back then to not explain everything in the "book" partly because of the experience that was already on that forum. Especially the number of platforms and IDEs that the user may be using has been a deterrent. About the order of the chapters: Knowing that it may be boring to keep some topics like classes for so much later, I tried to reduce the amount of "accept this as a recipe for now, it will be explained later" type of information. I know my decision is not right and "people don't learn like that" but that has been an early decision. :) Also, I was under the impressions that there was no known good way of teaching programming. Again, thank you very much the suggestions I will incorporate them. :) Ali
Apr 18 2012
prev sibling next sibling parent reply "Paul D. Anderson" <paul.d.removethis.anderson comcast.andthis.net> writes:
SomeDude: Your outline and especially your emphasis on what a 
rank beginner needs to know is very good.

Would you consider writing it up yourself? Not the whole thing, 
maybe but the beginner info and the compiler/linker appendices. 
You have a commendable prose style.

There are tutorials available already, but too many is way better 
than too few.

Paul


On Wednesday, 18 April 2012 at 08:26:14 UTC, Somedude wrote:
 Le 17/04/2012 16:07, Ali Çehreli a écrit :
 On 04/17/2012 12:42 AM, Somedude wrote:
 
 Sorry for hijacking this thread, but since you're around, I 
 hope you'll
 see this message. As a D beginner, I'm browsing through your 
 book.
 I wanted to tell you that there is something essential 
 missing in it:
 how to compile. It's actually quite hard to find it in the 
 official
 website, and since the first chapters are targetted at 
 complete
 beginners, I thought it would be nice to add a few lines or 
 pages about
 compilation, linking, debugging, etc.
 What do you think ?
Thank you very much for the feedback. I will do something about that. Although there is no hello world compilation, the dmd command line appears multiple times throughout the chapters. The 'assert and enforce' chapter shows it for the -release compiler switch and coincidentally I am on the 'Unit Testing' chapter as we speak, which includes the -unittest switch. :) But you are right, both dmd and rdmd command lines must appear earlier. Thank you, Ali
Yes, I think that you have a lot of valuable information, but the organization is lacking. The advanced chapters look good, but the first "beginner" chapters can be . what struck me as an error in your "Hello world program" chapter, you give as an exercise: "Type or copy the hello world program into your editor as a source file, compile it, and run the program. The program should produce the following output: Hello world! If you are new to using a compiler, you will need to get help from the compiler documentation or, preferably, an experienced person. You can't benefit from the rest of this book if you can't compile and run programs." Spock would have said: "Illogical, captain!" At this point, you haven't told how to compile and run the program. Seriously, if your book is aimed at complete beginners (and the first chapters seem to say that), you really want to tell them what to do, not "go find the compiler documentation", else if they can do that, they simply don't need to read your book. For a complete beginner, it's very frustrating to have to read 4 chapters of fluff, then write the program, and still not knowing how to run the program. Where is the fun ? I can guarantee that at this point, 9 out of 10 beginners will drop the reading. FIRST show, THEN explain. Better yet: first show, then ask questions, and finally explain. So how would I have organized this chapter ? Something like this: --------------- The first program to show in most programming... Here is a hello world in D: ... Copy this program and save it in a file under the name hello.d .d is the extension for D source code. Now we'll compile it, that is to say the compiler will check that the syntax of this source code, and if it is valid D, will translate it into machine language and make it a program. Type in the command prompt: rdmd hello.d If you didn't make any mistake, you should have the impression that nothing happened. In the contrary, everything went well ! You can check that the executable file hello (or hello.exe under window) has been created. If instead the compiler barked at you lots of text, you probably made a mistake while copying. Correct it and retry. You will routinely make many mistakes when programming so that this process of correcting and compiling will get very familiar. Once this is done, type the name of the executable to run it. You should see Hello, world ! Congratulations, your first D program is running ! ------------ Then ONLY after that, I would proceed to the explanations. (As an exercise, I would ask also: what is the smallest valid D program ?) I would put the "Hello, world" chapter at the very beginning, right after "The practice of programming". This chapter would be enriched by an example on the computer, showing the values. The order of the beginning chapters I think should be: Intro Practice of programming Hello world writeln and write Compiler Fundamental types I would then merge "Assignment and order of evaluation" with "Variables" I would also remove the "Standard Input and Output Streams", they don't belong to an introduction. In fact, the problem is, you really want to separate your chapters into a sections, one as an introductory course, and one for more advanced explanations of the core language, and maybe one to introduce specific concepts of the standard library (your last chapters). I would put in Introduction section the first 19 chapters until "Redirecting input and output", as well as all the flow constructs (do-while, for, foreach, switch case, you really want to merge those with other chapters like while, these don't need separate chapters), ternary ops and associative arrays. With that, beginning programmers can have some fun. In the second section (core language), I would put File Program environment auto typeof name space litterals enum function function params Lazy ops Immutability Classes <-- I didn't see a chapter on this topic ? Intefaces Exceptions Scope Assert and Enforce In the third section (Generic Programming and advanced concepts), I would put: Templates Mixins Ranges Parallelism Concurrency synchronized __traits calloc etc, or 2 chapters for interoperations with C/asm Etc Finally, as appendices, I would put chapters about the tools: The Compiler <-- some more details about it The Linker <-- name mangling, etc Creating interface files, etc I think this organization would make much more sense, and would make a wonderful book. I hope this helps.
Apr 18 2012
parent reply "SomeDude" <lovelydear mailmetrash.com> writes:
On Wednesday, 18 April 2012 at 19:43:50 UTC, Paul D. Anderson 
wrote:
 SomeDude: Your outline and especially your emphasis on what a 
 rank beginner needs to know is very good.

 Would you consider writing it up yourself? Not the whole thing, 
 maybe but the beginner info and the compiler/linker appendices. 
 You have a commendable prose style.

 There are tutorials available already, but too many is way 
 better than too few.

 Paul
Thanks. I wouldn't mind having a stab at it, but: 1. it is Ali's book, not mine, so we would need Ali's agreement. If he offers me to contribute, why not, 2. I'm myself a D beginner, so you would probably need to proof read and correct me afterwards.
Apr 19 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/19/2012 10:02 AM, SomeDude wrote:
 On Wednesday, 18 April 2012 at 19:43:50 UTC, Paul D. Anderson wrote:
 SomeDude: Your outline and especially your emphasis on what a rank
 beginner needs to know is very good.

 Would you consider writing it up yourself? Not the whole thing, maybe
 but the beginner info and the compiler/linker appendices. You have a
 commendable prose style.

 There are tutorials available already, but too many is way better than
 too few.

 Paul
Thanks. I wouldn't mind having a stab at it, but: 1. it is Ali's book, not mine, so we would need Ali's agreement. If he offers me to contribute, why not,
I will improve the beginning of the book with your ideas. (I don't have free time yet; maybe by the end of next week.) I would also like to thank you if you would please e-mail me your full name at acehreli yahoo.com. I think it would be even better if you add a newbie section (or improve the existing ones as you graciously have been doing) on the wiki site, or some other site. I would be happy to link to that section from the book. (I have a feeling Paul D. Anderson meant that you wrote a separate document anyway.)
 2. I'm myself a D beginner, so you would probably need to proof read and
 correct me afterwards.
Gladly, as much as I know myself. :) Ali
Apr 19 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2012 01:26 AM, Somedude wrote:

 For a complete beginner, it's very frustrating to have to read 4
 chapters of fluff, then write the program, and still not knowing how to
 run the program. Where is the fun ? I can guarantee that at this point,
 9 out of 10 beginners will drop the reading.
[...]
 The order of the beginning chapters I think should be:
 Intro
 Practice of programming
 Hello world
 writeln and write
 Compiler
 Fundamental types
That part is done: http://ddili.org/ders/d.en/hello_world.html Thank you, Ali
Apr 21 2012
parent Russel Winder <russel winder.org.uk> writes:
On Sat, 2012-04-21 at 22:14 -0700, Ali =C3=87ehreli wrote:
[...]
 That part is done:
=20
    http://ddili.org/ders/d.en/hello_world.html
Surely "Hello World" shows just a few, and definitely not all, of the essential concepts of a programming language. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Apr 21 2012