need help

Regan Heath regan at netwin.co.nz
Tue Apr 25 00:11:58 PDT 2006


On Tue, 25 Apr 2006 14:10:26 +0800, Boris Wang <nano.kago at hotmail.com>  
wrote:
> The suitable explain is :
>   args[ leftLimit .. rightLimit ]
>
>   the leftLimit is the first index, and the rightLimit is the
> last index + 1, just equal with args.length.

Or rather, that rightLimit is one past the last item you want to include.  
To include the entire array you use 0 and array.length.

> but we used to write the following code:
>
>   for ( int i = 0; i < len - 1; i ++ )

To access items in the slice args[0..args.length] you would use this for  
loop:

for(int i = 0; i < args.length; i++) {}

or one of these foreach statements:

foreach(int i, char[] arg; args) {}
foreach(char[] arg; args) {}
foreach(arg; args) {}

Regan

> "Boris Wang" <nano.kago at hotmail.com> Ð 
> ´ÈëÏûÏ¢ÐÂÎÅ:e2kdlg$1p04$1 at digitaldaemon.com...
>> You are right, the args array begin from index 0, but
>>
>>>   foreach( char[] arg; args[0 .. args.length] )
>>
>> This should not be right, it should be :
>>    foreach( char[] arg; args[0 .. args.length - 1] )
>>
>> and more, when i run the followed code:
>>
>> import std.stdio;
>>
>> int main( char[][] args )
>> {
>> foreach( char[] arg; args[0 .. args.length - 1 ] )
>> {
>>  printf( "%s ", cast(char*)arg );
>> }
>> }
>>
>> it even produce a assert:
>>
>> Error: AssertError Failure hello_my.d(10)
>>
>>
>> "Regan Heath" <regan at netwin.co.nz>
>> ??????:ops8jr1mr223k2f5 at nrage.netwin.co.nz...
>>> On Tue, 25 Apr 2006 11:13:58 +0800, Boris Wang <nano.kago at hotmail.com>
>>> wrote:
>>>> int main( char[][] args )
>>>> {
>>>>  foreach( char[] arg; args[1 .. args.length] )
>>>>  {
>>>>      printf( "%.*s ", arg );
>>>>  }
>>>>
>>>>  for ( int i = 0; i < args.length; i++ )
>>>>  {
>>>>      printf("%.*s ", args[i] );
>>>>  }
>>>>
>>>>  return 0;
>>>> }
>>>>
>>>> The first printf can't display the information, and the second do.
>>>>
>>>> I use dmd 0.154, XP SP2.
>>>
>>> The first one skips the first arg, to include the first arg use:
>>>
>>>   foreach( char[] arg; args[0 .. args.length] )
>>>   {
>>>       printf( "%.*s ", arg );
>>>   }
>>>
>>> Apart from that they produce identical output for me.
>>>
>>> Regan
>>
>>
>
>




More information about the Digitalmars-d-learn mailing list