Proposé : Two simple iterators : lists and enumerations

**SPIP**

# Article proposé

L’article "**Two simple iterators : lists and enumerations**
(https://www.spip.net/ecrire/?exec=article&id_article=6487)" est proposé
à la publication depuis le dimanche 20 janvier 2019.

* * *

## Two simple iterators : lists and enumerations

dimanche 20 janvier 2019 , par
[Loiseau2nuit](.././?page=auteur&id_auteur=5162&)

Among the numerous powerful iterators that come with SPIP 3, two of them
are shinning from their simplicity : lists and enumerations.

## List Iterator

The list iterator lets you loop on list items. Items can be words, figures
or, tiny bits of text. It’s called by `DATA` loop’s `liste` criteria,
to which we transmit the items list that should be looped on :

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 every criterias 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
’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 realy simple loop. It’s most generic use is, as
following

    <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 finding out
which one, 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’s worth 1 by default.

**Integers, no "1" :**

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

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

**Decimals, no "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, no "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".

— Envoyé par SPIP (https://www.spip.net/)

rubon135-80ef7.jpg