String splitting (Version: 3) - Miva - Snippets
<mvt:assign name="g.string" value="'one|two|three|four|five|six'" /> <h3>Split String mvt:do</h3> <mvt:assign name="g.startTime" value="s.dyn_tm_usec" /> <p>Does not inherently support multi-character splitting, you'd have to perform a glosub first (see next example)</p> <mvt:do file="g.Module_Library_Utilities" name="l.success" value="SplitString( g.string, '|', g.splitString )" /> <!-- @@ &mvt:global:MvDO_Error; --> <mvt:foreach iterator="line" array="global:splitString"> &mvt:line;</br> </mvt:foreach> </br>Completed in <mvt:eval expr="s.dyn_tm_usec - g.startTime" /> milliseconds. <hr> <h3>Deserialize+Global Substitution</h3> <p>This method supports multi-character splitting, as you're just replacing your seperator with a single comma</p> <mvt:assign name="g.startTime" value="s.dyn_tm_usec" /> <mvt:assign name="g.array" value="miva_array_deserialize( glosub(g.string,'|',',') )" /> <mvt:foreach iterator="line" array="global:array"> &mvt:line;</br> </mvt:foreach> </br>Completed in <mvt:eval expr="s.dyn_tm_usec - g.startTime" /> milliseconds. <hr> <h3>Get Token</h3> <p>Doesn't support multi-character splitting, a little harder to read in my opinion</p> <mvt:assign name="g.startTime" value="s.dyn_tm_usec" /> <mvt:assign name="g.nl" value="'|'" /> <mvt:assign name="l.count" value="1" /> <mvt:assign name="l.token" value="gettoken(g.string, g.nl , l.count)" /> <mvt:while expr="NOT ISNULL l.token"> <mvt:assign name="l.array_length" value="miva_array_insert( l.settings:my_array, l.token, -1 )" /> <mvt:assign name="l.count" value="l.count + 1" /> <mvt:assign name="l.token" value="gettoken(g.string, g.nl , l.count)" /> </mvt:while> <mvt:foreach iterator="row" array="my_array"> &mvt:row;</br> </mvt:foreach> </br>Completed in <mvt:eval expr="s.dyn_tm_usec - g.startTime" /> milliseconds.