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

Looping over any number of elements

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.

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 . 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:


#!/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";

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-01-01

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


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 4.9461043

Perl 6 Tricks and Treats newsletter

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