import ballerina/io;function main (string[] args) {
io:println("Iterating over a string array");
string[] fruits = ["apple", "banana", "cherry"];
foreach v in fruits {
io:println("fruit: " + v);
} io:println("\nIterating over a map.");
map words = {a:"apple", b:"banana", c:"cherry"};
foreach k, v in words {
string|error result = <string>v;
match result {
string value =>{
io:println("letter: " + k + ", word: " + value);
}
error err =>{
io:println("Error: " + err.message);
}
}
} io:println("\nIterating over a json object");
json apple = {name:"apple", colors:["red", "green"], price:5};
foreach j in apple{
match <string>j {
string js => {
io:println("value: " + js);
}
error e => {}
}
} io:println("\nIterating over a json array");
json[] colors = check <json[]> apple.colors;
foreach i, j in colors{
match <string>j {
string js => {
io:println("color " + i + " : " + js);
}
error e => {}
}
} io:println("\nIterating over a xml");
xml book = xml `<book>
<name>Sherlock Holmes</name>
<author>Sir Arthur Conan Doyle</author>
</book>`;
foreach i, x in book.*.elements(){
io:println("xml at " + i + " : ");
io:println(x);
} io:println("\nIterating over an integer range");
int endValue = 10;
int sum;
foreach i in [1..endValue] {
sum = sum + i;
}
io:println("summation from 1 to " + endValue + " is :" + sum);
}
ForeachThe 'Foreach' statement is a looping construct that traverses through the items of a collection of data, such as arrays, maps, json, or xml. |
|
import ballerina/io;
|
|
function main (string[] args) {
io:println("Iterating over a string array");
string[] fruits = ["apple", "banana", "cherry"];
|
|
foreach v in fruits {
io:println("fruit: " + v);
}
|
For arrays: use one variable to get the values defined in the array.
Use two variables as comma separated values to get the index and value. For example, |
io:println("\nIterating over a map.");
map words = {a:"apple", b:"banana", c:"cherry"};
|
|
foreach k, v in words {
string|error result = <string>v;
match result {
string value =>{
io:println("letter: " + k + ", word: " + value);
}
error err =>{
io:println("Error: " + err.message);
}
}
}
|
For maps: use 1 variable to get the values defined in the map. Use 2 variables to get both the key (string) and value. |
io:println("\nIterating over a json object");
json apple = {name:"apple", colors:["red", "green"], price:5};
|
|
foreach j in apple{
match <string>j {
string js => {
io:println("value: " + js);
}
error e => {}
}
}
|
For json: use only a single json type variable. |
io:println("\nIterating over a json array");
|
|
json[] colors = check <json[]> apple.colors;
foreach i, j in colors{
match <string>j {
string js => {
io:println("color " + i + " : " + js);
}
error e => {}
}
}
|
To Iterate over a JSON array, you need to first cast it into an array of json (json[]). |
io:println("\nIterating over a xml");
xml book = xml `<book>
<name>Sherlock Holmes</name>
<author>Sir Arthur Conan Doyle</author>
</book>`;
|
|
foreach i, x in book.*.elements(){
io:println("xml at " + i + " : ");
io:println(x);
}
|
For xml: use 1 variable to get the xml value. Use 2 variables to get both the index (int) and xml value. |
io:println("\nIterating over an integer range");
int endValue = 10;
int sum;
|
|
foreach i in [1..endValue] {
sum = sum + i;
}
io:println("summation from 1 to " + endValue + " is :" + sum);
}
|
An Integer range in the foreach represents an incremental integer value range from the start expression (1) to the end expression (endValue) inclusively. |
$ ballerina run foreach.bal
Iterating over a string array
|
|
fruit: apple
fruit: banana
fruit: cherry
|
Only one value was defined, therefore, only the values are printed. |
Iterating over a map.
|
|
letter: a, word: apple
letter: b, word: banana
letter: c, word: cherry
|
Two values were defined, therefore, both the key and the value in the map are printed. |
Iterating over a json object
|
|
value: apple
value: ["red","green"]
value: 5
|
Returns the values in the JSON object. |
Iterating over a json array
color 0 : red
color 1 : green
|
|
Iterating over a xml
|
|
xml at 0 : <name>Sherlock Holmes</name>
xml at 1 : <author>Sir Arthur Conan Doyle</author>
|
Two variables were defined, therefore, the Index and the value are printed. |
Iterating over an integer range
|
|
summation from 1 to 10 is :55
|
Prints the sum of the integers for the defined range. |