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/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";
}

Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC
If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed