{flag}

Replaces every value of a sequence with 1 or 0: whether it belongs to a sequence type.

Sequences Tag pairs

Syntax

{flag '1..10', even} ... {/flag}
{flag prime, from=10, rows=12} ... {/flag}
{flag:prime rows=10} ... {/flag:prime}
{flag store, prime} ... {/flag}

Parameters

values
Where the values come from: a range '1..10', a stored sequence named bare, or left out - the candidates then run from from upward.
type
The sequence type each value is tested against: prime, even, square, ...

Description

{flag '1..10', even} runs a sequence in flag mode: every value is tested against the named type and replaced by 1 when it belongs to it and 0 when it does not - here 0 1 0 1 0 1 0 1 0 1. Where {keep} and {remove} filter, {flag} keeps every position, so the flags line up with the original list. It is the same run as {sequence '1..10', flag, even}.

Because the flags are numbers, actions can count with them: {flag '1..20', prime, sum} is 8, the number of primes up to 20. Without a range the candidates run from from upward and rows says how many flags; a stored sequence named bare works as the source too. The prefix form {flag:prime rows=10} names the type in the tag and takes no range.

The current value is {$sequence} - the flag, not the original number. To show both side by side, flag a stored list and pull the store beside it, as in the example.

Examples

Flags in a row

Each of the numbers 1 to 12 marked 1 when even, 0 when odd.

Templateexamples/flag/1-flags.pad
<p>{flag '1..12', even}{$sequence} {/flag}</p>
Resultopen alone

0 1 0 1 0 1 0 1 0 1 0 1

Count the members

Summing the flags counts how many values belong to the type.

Templateexamples/flag/2-count.pad
<p>Primes up to 100: {flag '1..100', prime, sum}{$sequence}{/flag}</p>
<p>Squares up to 100: {flag '1..100', square, sum}{$sequence}{/flag}</p>
Resultopen alone

Primes up to 100: 25

Squares up to 100: 10

Value and flag side by side

A stored list and its flags against the type prime, in a two-row table.

Templateexamples/flag/3-table.pad
{sequence '10..17', push='teens'}
<table>
  <tr><th>n</th>{pull teens}<td>{$sequence}</td>{/pull}</tr>
  <tr><th>prime</th>{flag teens, prime}<td>{$sequence}</td>{/flag}</tr>
</table>
Resultopen alone
n 10 11 12 13 14 15 16 17
prime 0 1 0 1 0 0 0 1

See also

  • {keep} Keeps only the values of a sequence that belong to a sequence type.
  • {remove} Removes the values of a sequence that belong to a sequence type - the inverse of keep.
  • {make} Replaces every value of a sequence with what a sequence type makes of it.
  • {sequence} Generates a range, a list or one of 80 mathematical sequences, and iterates its values.