PUBLISHED : Two simple iterators : lists and enumerations

**SPIP**

# Article validated

The article "**Two simple iterators : lists and enumerations**
(https://www.spip.net/en_article6487.html)" was validated by
<multi>[ar]جورج[fr]George [en]George</multi>.

* * *

## Two simple iterators : lists and enumerations

Sunday 17 May 2020 , by [jack](.././?page=auteur&id_auteur=593&) ,
[Loiseau2nuit](.././?page=auteur&id_auteur=5162&)

Among the numerous powerful iterators that come with SPIP 3, two stand out
for their simplicity : lists and enumerations.

## List Iterator

The list iterator allows you to browse through the items in a list. Items
can be words, figures or tiny bits of text. It’s called by `DATA`
loop’s `liste` criterion to which the list of elements to be browsed is
passed:

This loop displays a list of the first prime numbers:

    <ul>

    <BOUCLE_prime_num

    (DATA)

    {liste 2, 3, 5, 7, 11, 13}

    >

           <li>
    
    #VALEUR
    
    </li>

    </BOUCLE_prime_num>

    </ul>

It possible to use all criteria that apply to all loops
(https://www.spip.net/ecrire/?exec=article&id_article=2085).

**Specify separator:**

    <BOUCLE_months

    (DATA)

    {liste january, february, march, april}

    {" ; "}

    >

    #VALEUR
    
     (
    
    [

    (

    #VALEUR

    |couper

    {3}

    )

    ]
    
    )

    </BOUCLE_months>

will return: january (jan) ; february (feb) ; march (mar) ; april (apr)

**Reverse the reading loop order:**

    <BOUCLE_months

    (DATA)

    {liste 1, 10, 100, 1000}

    {", "}

    {inverse
    
    #ENV

    {reverse}
    
    }

    >

    #VALEUR

    </BOUCLE_months>

will return: "1, 10, 100, 100, 1000" or "1000, 100, 10, 1" depending on the
’reverse’ value in the environment.

**Loop only on a list’s sub-dataset:**

    <BOUCLE_months

    (DATA)

    {liste january, february, march, april, may, june, july}

    {", "}

    {3,2}

    >

    #VALEUR

    </BOUCLE_months>

will jump 3 months forward and return only 2: "april, may"

**Values for markers can be calculated:**

    <BOUCLE_colors

    (DATA)

    {liste
    
    #GET

    {mostlikedcolor}
    
    ,
    
    #ENV

    {chosencolor}
    
    }

    {", "}

    >

    #VALEUR

    </BOUCLE_colors>

## Enumeration iterator

Enumeration iterator is a really simple loop.
It’s most generic use is as follows

    <BOUCLE_enumerate

    (DATA)

    {enum val1, val2}

    >

    #VALEUR

    </BOUCLE_enumerate>

or

    <BOUCLE_enumerate

    (DATA)

    {enum val1, val2, interval}

    >

    #VALEUR

    </BOUCLE_enumerate>

`val1` and `val2` are two numeric values or 2 characters. SPIP determining
which of the 2 values, is the smallest, this loop is going to enumerate
values between `val1` and `val2`. In the first variant, no `interval` is
set : it is 1 by default.

**Integers in increments of 1:**

1. <BOUCLE\_enum (DATA) {enum 10,15} {", "} > #VALEUR </BOUCLE\_enum>

is looping on values, starting at 10, breaking on 15, thus returning "10,
11, 12, 13, 14, 15".

**Decimals in increments of 1:**

1. <BOUCLE\_enum (DATA) {enum 15.5,10.3} {", "} > #VALEUR </BOUCLE\_enum>

is looping on values, starting 15, breaking on 10, thus returning "15.5,
14.5, 13.5, 12.5, 11.5, 10.5".

**Integers in increments of 2:**

1. <BOUCLE\_enum (DATA) {enum 10, 15, 2} {", "} > #VALEUR </BOUCLE\_enum>

is looping on values from 10 to 15, with an `interval` of 2, thus returning
"10, 12, 14".

**Letters:**

1. <BOUCLE\_enum (DATA) {enum g,m} {", "} > #VALEUR </BOUCLE\_enum>

is looping on letters from "g" to "m", thus returning "g, h, i, j, k, l,
m".

— Sent by SPIP (https://www.spip.net/)

rubon57.jpg