www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to iterate on Array?

reply "aki" <aki google.com> writes:
I want to print the contents of Array!int

import std.stdio;
import std.container;

void pr(Array!int a) {
	foreach(i, v; a[]) {
		writeln("%4s: %s\n", i, v);
	}
}

But when I compile it by DMD 2.062 on Windows
it says:

testArray.d(5): Error: cannot infer argument types
(line 5 is at "foreach(i, v; a[]) {" )

What's wrong? how can I iterate the array?

Thanks, aki.
Jun 27 2015
next sibling parent "John Chapman" <johnch_atms hotmail.com> writes:
On Saturday, 27 June 2015 at 17:43:13 UTC, aki wrote:
 I want to print the contents of Array!int

 import std.stdio;
 import std.container;

 void pr(Array!int a) {
 	foreach(i, v; a[]) {
 		writeln("%4s: %s\n", i, v);
 	}
 }

 But when I compile it by DMD 2.062 on Windows
 it says:

 testArray.d(5): Error: cannot infer argument types
 (line 5 is at "foreach(i, v; a[]) {" )

 What's wrong? how can I iterate the array?

 Thanks, aki.
size_t i; foreach (v; a[]) writeln("%s: %s", i++, v);
Jun 27 2015
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/27/2015 10:43 AM, aki wrote:

 void pr(Array!int a) {
      foreach(i, v; a[]) {
You are rightly assuming that the loop counter is available for all container types. Unfortunately, it is the case only for arrays. Luckily, it is very easy to achieve it with std.algorithm.enumerate: import std.stdio; import std.container; import std.range; void pr(Array!int a) { foreach(i, v; a[].enumerate) { writeln("%4s: %s\n", i, v); } } void main() { auto a = Array!int(); pr(a); } Ali
Jun 27 2015
next sibling parent "jmh530" <john.michael.hall gmail.com> writes:
On Saturday, 27 June 2015 at 18:32:10 UTC, Ali Çehreli wrote:
 Luckily, it is very easy to achieve it with std.range.enumerate:
FTFY
Jun 27 2015
prev sibling parent "aki" <aki google.com> writes:
On Saturday, 27 June 2015 at 18:32:10 UTC, Ali Çehreli wrote:
 On 06/27/2015 10:43 AM, aki wrote:
 You are rightly assuming that the loop counter is available for 
 all container types. Unfortunately, it is the case only for 
 arrays.
Now I know. Thanks for it. aki.
Jun 27 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Saturday, 27 June 2015 at 17:43:13 UTC, aki wrote:
 But when I compile it by DMD 2.062 on Windows
 it says:

 testArray.d(5): Error: cannot infer argument types
 (line 5 is at "foreach(i, v; a[]) {" )
2.062 is really ancient, we're about to release 2.068 soon. Consider upgrading, because there have been tons of bugfixes since your version. On current DMD, the error message is slightly clearer: "Error: cannot infer argument types, expected 1 argument, not 2"
Jun 28 2015
parent "aki" <aki google.com> writes:
On Sunday, 28 June 2015 at 10:16:47 UTC, Marc Schütz wrote:
 On Saturday, 27 June 2015 at 17:43:13 UTC, aki wrote:
 But when I compile it by DMD 2.062 on Windows
 it says:

 testArray.d(5): Error: cannot infer argument types
 (line 5 is at "foreach(i, v; a[]) {" )
2.062 is really ancient, we're about to release 2.068 soon. Consider upgrading, because there have been tons of bugfixes since your version. On current DMD, the error message is slightly clearer: "Error: cannot infer argument types, expected 1 argument, not 2"
Ah, you answered my another question. Even if I put all the types like foreach(size_t i, int v; a[]) he still said "cannot infer." I wonder why he need to infer? Now I got it. I'm looking forward 2.068. Aki.
Jun 28 2015