String pattern location · instr

🟢 8 of 8 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["String pattern location · instr<br/>🟢8"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_pattern["instr(DS, pattern)"]:::ok
        v_ds_pattern_start["instr(DS, pattern, start)"]:::ok
        v_ds_pattern_start_occ["instr(DS, pattern, start, occurrence)"]:::ok
        v_ds_placeholder_occ["instr(DS, pattern, _, occurrence)"]:::ok
        v_ds_pattern_start_placeholder["instr(DS, pattern, start, _)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_pattern["instr(comp, pattern)"]:::ok
        v_comp_params["instr(comp, comp, comp, comp)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_pattern["instr(sc, pattern)"]:::ok
    end
    O --- G_ds
    O --- G_comp
    O --- G_sc
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
    click v_ds_pattern href "#form-ds-pattern"
    click v_ds_pattern_start href "#form-ds-pattern-start"
    click v_ds_pattern_start_occ href "#form-ds-pattern-start-occ"
    click v_ds_placeholder_occ href "#form-ds-placeholder-occ"
    click v_ds_pattern_start_placeholder href "#form-ds-pattern-start-placeholder"
    click v_comp_pattern href "#form-comp-pattern"
    click v_comp_params href "#form-comp-params"
    click v_sc_pattern href "#form-sc-pattern"
    

Types

Takes integer, string · Gives integer

Comes from · integer, string — 27 operators in 6 blocks 🟢27
General purpose operators (2) 🟢2
String operators (9) 🟢9
Numeric operators (4) 🟢4
Time operators (7) 🟢7
Aggregate and Analytic operators (2) 🟢2
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 37 operators in 8 blocks 🟢37
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Time operators (3) 🟢3
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Goes to · integer — 43 operators in 8 blocks 🟢43
General purpose operators (1) 🟢1
String operators (1) 🟢1
Numeric operators (18) 🟢18
Time operators (6) 🟢6
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (10) 🟢10
Data validation operators (2) 🟢2
Definitions (VTL-DL) (4) 🟢4
Goes to · operators that take any scalar — 46 operators in 8 blocks 🟢46
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (8) 🟢8
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Forms

instr(DS, pattern) 🟢 · string → integer

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(DS_1, "hello") on a dataset with one String measure: the position of hello in each value.

Manual:

String pattern location, Example 1, output verbatim: “The operator returns the position in the input string of a specified string ( pattern ).” For a data set, “instr returns a dataset with a single measure int_var of type integer.”

Expected:

int_var is 1 for hello world, 5 for say hello and for "hi, hello! ", and 0 for he, where hello is not found and “the returned value is 0”.

script.vtl
DS_r:= instr(DS_1,"hello");
Input DS_1
Id_1Id_2Me_1
1Ahello world
2Asay hello
3Ahe
4Ahi, hello!
Expected DS_r
Id_1Id_2int_var
1A1
2A5
3A0
4A5

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(DS, pattern, start) 🟢 · string, integer → integer

Matches before start skipped 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(DS_1, "o", 6): the search starts at the 6th character, so earlier matches do not count; one measure is null.

Manual:

String pattern location: “The search starts from the startth character of the input string and finds the nth occurrence of the pattern, returning the position of its first character.”

Expected:

int_var, the page’s measure for a data set result, is 8, 0 and 6: the o at 5 of hello world is before the 6th character, both o of boot camp are before it, and the o of abcdeo is at the start position, which counts as with start omitted: “the search starts from the 1st position”. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := instr(DS_1, "o", 6);
Input DS_1
Id_1Me_1
1hello world
2boot camp
3abcdeo
4
Expected DS_r
Id_1int_var
18
20
36
4

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(DS, pattern, start, occurrence) 🟢 · string, integer → integer

Second occurrence from start 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(DS_1, "o", 2, 2): the second o from the 2nd character; one measure is null.

Manual:

String pattern location: “The search starts from the startth character of the input string and finds the nth occurrence of the pattern, returning the position of its first character.”

Expected:

int_var, the page’s measure for a data set result, is 8, 5 and 0: o-o-o has its o at 1, 3 and 5, and the one at 1 is before the 2nd character, so the second found is at 5 (it would be 3 if start were ignored); hello has no second o. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := instr(DS_1, "o", 2, 2);
Input DS_1
Id_1Me_1
1hello world
2o-o-o
3hello
4
Expected DS_r
Id_1int_var
18
25
30
4

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(DS, pattern, _, occurrence) 🟢 · string, integer → integer

Placeholder for start 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for start in instr(DS_1, "o", _, 2): the second o from the 1st position.

Manual:

String pattern location: “If start is omitted, the search starts from the 1st position.” Its valid syntaxes include instr ( DS_1, "ab", _ , 2 ).

Expected:

int_var, the page’s measure for a data set result, is 8, 3 and 0: the second o of hello world is at 8 and that of o-o-o at 3, and hello has one o only. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := instr(DS_1, "o", _, 2);
Input DS_1
Id_1Me_1
1hello world
2o-o-o
3hello
4
Expected DS_r
Id_1int_var
18
23
30
4

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(DS, pattern, start, _) 🟢 · string, integer → integer

Placeholder for occurrence 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for occurrence: instr(DS_1, "o", 2, _) is instr(DS_1, "o", 2) with the underscore written out.

Manual:

In the invocation, a non-specified optional operand “is substituted by an underscore or, if it is in the end of the invocation, can be omitted” (Overview and conventions, syntax conventions); String pattern location: “If nth occurrence is omitted, the value is 1.”

Expected:

the first o from the 2nd character: int_var, the page’s measure for a data set result, is 5, 3 and 0, as the o at 1 of o-o is before the 2nd character and abc has none. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := instr(DS_1, "o", 2, _);
Input DS_1
Id_1Me_1
1hello world
2o-o
3abc
4
Expected DS_r
Id_1int_var
15
23
30
4

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(comp, pattern) 🟢 · string → integer

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(Me_1, "hello") inside calc, where the new measure Me_2 takes the position.

Manual:

String pattern location, Example 2, output verbatim: “The operator returns the position in the input string of a specified string ( pattern ).”

Expected:

Me_2 is 1, 5, 0 and 5; Me_1 is kept.

script.vtl
DS_r := DS_1[calc Me_2:=instr(Me_1,"hello")];
Input DS_1
Id_1Id_2Me_1
1Ahello world
2Asay hello
3Ahe
4Ahi, hello!
Expected DS_r
Id_1Id_2Me_1Me_2
1Ahello world1
2Asay hello5
3Ahe0
4Ahi, hello! 5

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

two instr calls in one calc on DS_2, where Me_1 is null in one data point.

Manual:

String pattern location, Example 3, output verbatim: “If the nth occurrence of the string-pattern after the start th character is not found in the input string, the returned value is 0.”

Expected:

Me_10 is 5 and null, and Me_20 is 2 and 0: hi has no o, so its position is 0, while the null Me_1 gives null. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual).

script.vtl
DS_r := DS_2 [calc Me_10:= instr(Me_1, "o" ), Me_20:=instr(Me_2, "o")];
Input DS_2
Id_1Id_2Me_1Me_2
1Ahelloworld
2Bhi
Expected DS_r
Id_1Id_2Me_1Me_2Me_10Me_20
1Ahelloworld52
2Bhi0

Files: script.vtl · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(comp, comp, comp, comp) 🟢 · string, integer → integer

Pattern and start from components 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(Me_1, Me_2, Me_3) inside calc: pattern and start come from components, and occurrence is left out.

Manual:

String pattern location types pattern as component<string> | string and start as component<integer [value >= 1]> | integer [value >= 1], and “If nth occurrence is omitted, the value is 1.”

Expected:

Me_4 is 8 for o from the 6th character of hello world, 4 for an from the 3rd of banana and 7 for w from the 7th of hello world, where the w is at the start position. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := DS_1[calc Me_4 := instr(Me_1, Me_2, Me_3)];
Input DS_1
Id_1Me_1Me_2Me_3
1hello worldo6
2bananaan3
3hello worldw7
4a1
Expected DS_r
Id_1Me_1Me_2Me_3Me_4
1hello worldo68
2bananaan34
3hello worldw77
4a1

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

All parameters from components 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(Me_1, Me_2, Me_3, Me_4) inside calc: pattern, start and occurrence all come from components.

Manual:

String pattern location types occurrence as component<integer [value >= 1]> | integer [value >= 1], and “If the nth occurrence of the string-pattern after the start th character is not found in the input string, the returned value is 0.”

Expected:

Me_5 is 8, 4, 5 and 0: the 2nd o of hello world is at 8, an from the 3rd character of banana at 4, the 3rd o of o-o-o at 5, and hello has no 2nd o.

script.vtl
DS_r := DS_1[calc Me_5 := instr(Me_1, Me_2, Me_3, Me_4)];
Input DS_1
Id_1Me_1Me_2Me_3Me_4
1hello worldo12
2bananaan31
3o-o-oo13
4helloo12
Expected DS_r
Id_1Me_1Me_2Me_3Me_4Me_5
1hello worldo128
2bananaan314
3o-o-oo135
4helloo120

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

instr(sc, pattern) 🟢 · string → integer

Pattern at the third position 🟢 pandas ✅ · DuckDB ✅
Tests:

instr(SC_1, SC_2) with SC_1 = abcde and SC_2 = c, a scalar with only pattern.

Manual:

String pattern location: “The operator returns the position in the input string of a specified string ( pattern ).”, as in instr ("abcde", "c" ) gives 3.

Expected:

SC_r is 3, an Integer: c is the 3rd character of abcde.

script.vtl
SC_r := instr(SC_1, SC_2);
Input scalars
SC_1SC_2
abcdec
Expected scalars

Name

Type

Value

SC_r

Integer

3

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗