Logo

Programming-Idioms

History of Idiom 7 > diff from v30 to v31

Edit summary for version 31 by :

Version 30

2015-10-27, 03:02:18

Version 31

2015-10-29, 14:05:11

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Code
items.asMap().forEach((i, value) {
  print('index=$i, value=$value');
});
Code
items.asMap().forEach((i, value) {
  print('index=$i, value=$value');
});
Comments bubble
An alternative, but not really idiomatic.
Comments bubble
An alternative, but not really idiomatic.
Code
var Iter:Integer;
    Items: array of String;
[..]
  for Iter := 0 to high(Items) do
    WriteLn('Item ', Iter, ' = ', Items[Iter]);
Code
var Iter:Integer;
    Items: array of String;
[..]
  for Iter := 0 to high(Items) do
    WriteLn('Item ', Iter, ' = ', Items[Iter]);
Imports
import std.stdio;
Imports
import std.stdio;
Code
foreach(i, x; items)
{
   writefln("%s: %s", i, x);
}
Code
foreach(i, x; items)
{
   writefln("%s: %s", i, x);
}
Comments bubble
use 'ref' on the item variable 'x' to access by reference
Comments bubble
use 'ref' on the item variable 'x' to access by reference
Code
for (final T x : items)
    System.out.println(x);
Code
for (final T x : items)
    System.out.println(x);
Comments bubble
Solution if the index is irrelevant.
Comments bubble
Solution if the index is irrelevant.
Code
for (int i = 0; i < items.size(); i++) {
    T x = items.get(i);
    System.out.printf("Item %d = %s%n", i, x);
}
Code
for (int i = 0; i < items.size(); i++) {
    T x = items.get(i);
    System.out.printf("Item %d = %s%n", i, x);
}
Comments bubble
Solution if the list is a List.
Comments bubble
Solution if the list is a List.
Code
for (int i = 0; i < items.length; i++) {
    T x = items[i];
    System.out.printf("Item %d = %s%n", i, x);
}
Code
for (int i = 0; i < items.length; i++) {
    T x = items[i];
    System.out.printf("Item %d = %s%n", i, x);
}
Comments bubble
Solution if the list is an array.
Comments bubble
Solution if the list is an array.
Code
items.each_index do |i| 
	puts "Item #{i} = #{items[i]}"
end
Code
items.each_index do |i| 
	puts "Item #{i} = #{items[i]}"
end
Comments bubble
See also the one-liner Ruby solution
Comments bubble
See also the one-liner Ruby solution
Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s\n", i, toString(x) );
}
Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s\n", i, toString(x) );
}
Comments bubble
The loop variable i is the index. Inside the loop, access the value with items[i]
Comments bubble
The loop variable i is the index. Inside the loop, access the value with items[i]
Imports
import "fmt"
Imports
import "fmt"
Code
for i, x := range items {
    fmt.Printf("Item %d = %v \n", i, x )
}
Code
for i, x := range items {
    fmt.Printf("Item %d = %v \n", i, x )
}
Comments bubble
The range clause gives you index and value at the same time as loop variables
Comments bubble
The range clause gives you index and value at the same time as loop variables