dynamic array of strings

Gide Nwawudu gide at btinternet.com
Thu Jan 1 06:19:06 PST 2009


On Tue, 30 Dec 2008 10:56:08 -0500, Michael P.
<baseball.mjp at gmail.com> wrote:

>Jarrett Billingsley Wrote:
>
>> On Mon, Dec 29, 2008 at 10:36 PM, Michael P. <baseball.mjp at gmail.com> wrote:
>> > import std.stdio;
>> > import std.cstream;
>> > void main()
>> > {
>> >  char[][] names;
>> >  char[] currentName;
>> >  while( true )
>> >  {
>> >    din.readf( "%s", &currentName );
>> >    if( currentName == "stop" )
>> >    {
>> >      break;
>> >    }
>> >    else
>> >    {
>> names ~= currentName;
>> >    }
>> >  }
>> >  foreach( name; names )
>> >  {
>> >    writefln( name );
>> >  }
>> > }
>> 
>> ~= performs an append.  It adds the given item to the end of the array.
>
>Under 1.036, this does not work.
>
>Input:
>michael
>is
>cool
>stop
>
>The output is:
>stopeal
>st
>stop
>
>What seems to be happening is that the letters of stop are added to the word, and if the word is longer than stop, then the rest of the letters of the word are added.
>So, I'm not sure if this is a bug or what...
>-Michael P.

currentName is used as a buffer that is overwritten on each call to
din.readf. It is easier to explicitly duplicate the contents to create
a copy that is stored in the array names.


>> names ~= currentName;
names~=currentName.dup;

Gide


More information about the Digitalmars-d-learn mailing list