String Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > String commands > !Delimited Strings >

String Operations

STR.ParseAny

Previous Top Next


MiniRobotLanguage (MRL)

 

STR.ParseAny

Extract Field from Delimited String Using Multiple Single-Character Delimiters

 

clip1126

Result of the Testscript below.

 

Intention

The STR.ParseAny command extracts the nth field from a source string (P1) based on a specified set of single-character delimiters (P3) and field index (P2). The result is stored in a variable (P4) or pushed to the Top of Stack (TOS) if P4 is omitted.

Each character in P3 is treated as a separate delimiter (e.g., ,; allows , or ; to split fields). If P3 is omitted, the default delimiter is a comma (,). The index (P2) is 1-based, where 1 is the first field. The search is case-sensitive.

This command is useful for parsing delimited data with multiple possible single-character delimiters, such as CSV files or command-line arguments. If the nth field is not found, an empty string is returned. For a single multi-character delimiter, use STR.Parse instead.

 

Schematic (Delimited Field Extraction)

Source: Hello,world;new.test

Delimiters: ,; Index=1 --> Result: Hello

 |H|e|l|l|o|,|w|o|r|l|d|;|n|e|w|.|t|e|s|t|

 ^ ^ (1st field before , or ;)

Delimiters: ,; Index=3 --> Result: new

 |H|e|l|l|o|,|w|o|r|l|d|;|n|e|w|.|t|e|s|t|

 ^ ^ (3rd field before .)

 

Syntax

 

STR.ParseAny|P1|P2[|P3][|P4]

 

Parameter Explanation

 

P1 - (Source String) The string containing delimited fields. Variable or literal.

P2 - (Field Index) Numeric, 1-based index of the field to extract (e.g., 1 for first field).

P3 - (Optional Delimiters) String of single-character delimiters (default is comma, ,). Each character is treated as a separate delimiter.

P4 - (Optional Result Variable) Variable to store the extracted field. If omitted, result is pushed to TOS.

 

Examples

 

' Extract fields with multiple single-character delimiters to variable

STS.CLEAR

$$SRC=Hello,world;new.test

$$DEL=,;.

STR.ParseAny|$$SRC|1|$$DEL|$$RES

STS.CLEAR

' $$RES = Hello

STS.CLEAR

STR.ParseAny|$$SRC|3|$$DEL|$$RES

STS.CLEAR

' $$RES = new

 

' Extract field with default delimiter to TOS

STS.CLEAR

$$SRC=Hello,this,new,world

STR.ParseAny|$$SRC|2

POP.$$RES

STS.CLEAR

' $$RES = this

 

' Extract non-existent field to variable

STS.CLEAR

$$SRC=Hello,this

STR.ParseAny|$$SRC|3|$$RES

STS.CLEAR

' $$RES = "" (empty, no 3rd field)

 

 

'-------------------------------------------------

' =================================================================

' SELF-VALIDATING TEST SCRIPT for STR.ParseAny

' Purpose: Verify all features with JIV. for automated checks and a summary report.

' Tests multiple single-character delimiters, index values, result to variable/TOS, edge cases.

' Uses result variable $$RES for most tests, with stack access for TOS tests.

' Includes extra stack clearing to avoid corruption.

' Uses short jumps (JIV.) and clear PRT. for readability.

' =================================================================

' Initialize counters

$$PAS=0

$$FAI=0

STS.CLEAR

PRT. ===================================================

PRT. 1. FIELD EXTRACTION TESTS

PRT. ===================================================

PRT. Test 1.1: Extract 1st field with multiple delimiters to variable

STS.CLEAR

$$SRC=Hello,world;new.test

$$DEL=,;.

STR.ParseAny|$$SRC|1|$$DEL|$$RES

STS.CLEAR

$$EXP=Hello

JIV.$$RES!$$EXP|Lab_Error1

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next1

:Lab_Error1

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next1

PRT. Test 1.2: Extract 3rd field with multiple delimiters to variable

STS.CLEAR

$$SRC=Hello,world;new.test

$$DEL=,;.

STR.ParseAny|$$SRC|3|$$DEL|$$RES

STS.CLEAR

$$EXP=new

JIV.$$RES!$$EXP|Lab_Error2

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next2

:Lab_Error2

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next2

PRT. Test 1.3: Extract 2nd field with default delimiter to TOS

STS.CLEAR

$$SRC=Hello,this,new,world

STR.ParseAny|$$SRC|2

$$TOS=#tos#

JIV.$$TOS!1|Lab_Error3

POV.$$RES

$$EXP=this

JIV.$$RES!$$EXP|Lab_Error3

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next3

:Lab_Error3

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP), Stack: $$TOS (exp: 1)

PRT.$$MSG

VIC.$$FAI

:Lab_Next3

STS.CLEAR

PRT. ===================================================

PRT. 2. EDGE CASE TESTS

PRT. ===================================================

PRT. Test 2.1: Empty source string (index=1, to variable)

STS.CLEAR

$$SRC=

$$DEL=,

STR.ParseAny|$$SRC|1|$$DEL|$$RES

STS.CLEAR

$$EXP=

JIV.$$RES!$$EXP|Lab_Error4

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next4

:Lab_Error4

$$MSG= -> FAIL - Result: $$RES (exp: empty)

PRT.$$MSG

VIC.$$FAI

:Lab_Next4

PRT. Test 2.2: No delimiters (index=1, to variable)

STS.CLEAR

$$SRC=HelloWorld

$$DEL=,;

STR.ParseAny|$$SRC|1|$$DEL|$$RES

STS.CLEAR

$$EXP=HelloWorld

JIV.$$RES!$$EXP|Lab_Error5

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next5

:Lab_Error5

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next5

PRT. Test 2.3: Invalid index (index=5, to variable)

STS.CLEAR

$$SRC=Hello,this,new

$$DEL=,

STR.ParseAny|$$SRC|5|$$DEL|$$RES

STS.CLEAR

$$EXP=

JIV.$$RES!$$EXP|Lab_Error6

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next6

:Lab_Error6

$$MSG= -> FAIL - Result: $$RES (exp: empty)

PRT.$$MSG

VIC.$$FAI

:Lab_Next6

PRT. Test 2.4: Zero index (index=0, to variable)

STS.CLEAR

$$SRC=Hello,this,new

$$DEL=,

STR.ParseAny|$$SRC|0|$$DEL|$$RES

STS.CLEAR

$$EXP=

JIV.$$RES!$$EXP|Lab_Error7

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next7

:Lab_Error7

$$MSG= -> FAIL - Result: $$RES (exp: empty)

PRT.$$MSG

VIC.$$FAI

:Lab_Next7

PRT. Test 2.5: Binary data with delimiters (index=1, to variable)

STS.CLEAR

$$SRC=$crlf$Hello$sp$this$sp$new$tab$

$$DEL=$sp$

STR.ParseAny|$$SRC|1|$$DEL|$$RES

STS.CLEAR

$$EXP=$crlf$Hello

JIV.$$RES!$$EXP|Lab_Error8

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next8

:Lab_Error8

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next8

PRT. Test 2.6: Empty delimiters (index=1, to variable)

STS.CLEAR

$$SRC=Hello,this,new

$$DEL=

STR.ParseAny|$$SRC|1|$$DEL|$$RES

STS.CLEAR

$$EXP=Hello,this,new

JIV.$$RES!$$EXP|Lab_Error9

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next9

:Lab_Error9

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next9

PRT. Test 2.7: Trailing delimiter (index=4, to variable)

STS.CLEAR

$$SRC=Hello,this,new;

$$DEL=,;

STR.ParseAny|$$SRC|4|$$DEL|$$RES

STS.CLEAR

$$EXP=

JIV.$$RES!$$EXP|Lab_Error10

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next10

:Lab_Error10

$$MSG= -> FAIL - Result: $$RES (exp: empty)

PRT.$$MSG

VIC.$$FAI

:Lab_Next10

PRT. ===================================================

PRT. TEST SUMMARY

PRT. ===================================================

CAL.$$TOT=$$PAS+$$FAI

$$MSG=Total Tests: $$TOT

PRT.$$MSG

$$MSG=Passed: $$PAS

PRT.$$MSG

$$MSG=Failed: $$FAI

PRT.$$MSG

' Display final result

JIV.$$FAI=0|Lab_Success

$$MSG=FAILURE: $$FAI of $$TOT tests failed.

MBX.$$MSG|Test Result|16

JMP.Lab_End

:Lab_Success

MBX.SUCCESS: All tests passed!|Test Result|64

:Lab_End

ENR.

 

 

Remarks

 

-Case-sensitive search for delimiters.

-P2 must be positive and non-zero; negative or zero values return an empty string.

-P3 defaults to comma (,) if omitted.

-Result excludes the delimiters.

-If the nth field is not found, returns an empty string.

-Binary-safe for P1 and P3; no special/system vars expanded.

-For a single multi-character delimiter, use STR.Parse.

-Note: Implementation may leave unintended items on the stack; use STS.CLEAR after execution.

 

Limitations

 

-Each character in P3 is treated as a separate single-character delimiter; for multi-character delimiters, use STR.Parse.

-Does not support nested delimiters or quoted fields; use STR.GetNested or STR.GrabQuoted for those cases.

-Current implementation may corrupt stack; clear stack after use.

 

See also:

STR.Parse

STR.GetNested

STR.GrabQuoted

STR.Extract

The Stack (TOS)