fork download
  1. // foreachループで配列を反復処理し、インデックスと値を表示する
  2. void example_foreach() {
  3. array numbers = ({2, 3, 5, 7, 11});
  4. foreach (numbers; int index; int number) {
  5. write("Index: %d, Value: %d\n", index, number);
  6. }
  7. }
  8.  
  9. // Array.Iteratorを使用して配列を反復処理し、インデックスと値を表示する
  10. void example_iterator() {
  11. array numbers = ({2, 3, 5, 7, 11});
  12. Array.Iterator iterator = Array.Iterator(numbers);
  13. while (iterator) {
  14. int index = iterator->index();
  15. int value = iterator->value();
  16. write("Index: %d, Value: %d\n", index, value);
  17. iterator += 1;
  18. }
  19. }
  20.  
  21. // Mapping.Iteratorを使用してマップを反復処理し、キーと値を表示する
  22. void example_mapping_iterator() {
  23. mapping data = ([ "a": 1, "b": 2, "c": 3 ]);
  24. Mapping.Iterator iterator = Mapping.Iterator(data);
  25. while (iterator) {
  26. mixed index = iterator->index();
  27. mixed value = iterator->value();
  28. write("Index: %O, Value: %O\n", index, value);
  29. iterator += 1;
  30. }
  31. }
  32.  
  33. // foreach、Array.Iterator、Mapping.Iteratorを使用して配列とマップを反復処理する
  34. int main() {
  35. //write("Using foreach:\n");
  36. //example_foreach();
  37.  
  38. //write("\nUsing iterator on array:\n");
  39. //example_iterator();
  40.  
  41. write("\nUsing mapping iterator on mapping:\n");
  42. example_mapping_iterator();
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.05s 6980KB
stdin
Standard input is empty
stdout
Using mapping iterator on mapping:
Index: "b", Value: 2
Index: "a", Value: 1
Index: "c", Value: 3