Camelia
Prev: Looping over a list of values one at a time, two at a time and more

Looping over any number of elements

You can also iterate over any number of elements: Let's say we just extracted the results of the Spanish Liga football games from the soccer website http://soccernet.espn.go.com/ . Those come in groups of 4 values: home team, score of home team score of guest team guest team We can loop over the values using a for statement with 4 scalar variables:
examples/arrays/looping_over_many_elements.p6
#!/usr/bin/env perl6
use v6;

my @scores = <
    Valencia  1 1 Recreativo_Huelva
    Athletic_Bilbao 2 5 Real_Madrid
    Malaga      2  2      Sevilla_FC
    Sporting_Gijon  3 2 Deportivo_La_Coruna
    Valladolid     1  0     Getafe
    Real_Betis     0  0     Osasuna
    Racing_Santander     5  0     Numancia
    Espanyol     3  3     Mallorca
    Atletico_Madrid     3  2     Villarreal
    Almeria     0  2     Barcelona
>;

for @scores -> $home, $home_score, $guest_score, $guest {
    say "$home $guest $home_score : $guest_score";
}

Prev: Looping over a list of values one at a time, two at a time and more

Table of Contents

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: