echo: "-n", the next installment

monarch_dodra monarchdodra at gmail.com
Sat Jul 27 05:52:08 PDT 2013


On Saturday, 27 July 2013 at 12:39:24 UTC, monarch_dodra wrote:
> On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:
>> After a few weeks of not getting around to it, here's my 
>> second post:
>>
>> http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html
>
> I tried to post a comment on your blog, but I failed. Anyways, 
> I wanted to post:

And here is the second comment I wanted to put:

When parsing the options, you use an if-else. I don't know if its 
just me, but I find that using a switch is clearer (it's what you 
do in your second example). It also introduces string cases 
(illegal in C++), and labeled control statements.

The code becomes:

import std.stdio : writef, writeln;
void main(string[] args)
{
     assert(args.length);
     args = args[1 .. $];
     bool writeNewline = true;

     size_t i = 0;
     myForeach: foreach(arg; args)
     {
         switch(arg)
         {
             case "-n":
                 writeNewline = false;
                 ++i;
                 break;

             default:
                 break myForeach;
         }
     }
     args = args[i .. $];

     writef("%-(%s %)", args);
     if(writeNewline)
         writeln();
}

PS: If I figure out how to comment on your blog, I'll paste the 
comments there :)


More information about the Digitalmars-d-announce mailing list