Tokenize a string: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 480:
John Lennon.Paul McCartney.George Harrison.Ringo Starr.
John.Lennon..Paul.McCartney..George.Harrison..Ringo.Starr.
</pre>
 
=={{header|Amazing Hopper}}==
 
Hopper provides instructions for separating and modifying tokens from a string.
Let "s" be a string; "n" token number:
 
1) {n}, $(s) ==> gets token "n" from string "s".
 
2) {"word", n} $$(s) ==> replace token "n" of "s", with "word".
 
Note: the "splitnumber" macro cannot separate a number converted to a string by the "XTOSTR" function, because this function "rounds" the number to the decimal position by default.
 
<lang Hopper>
#include <hopper.h>
 
#proto splitdate(_DATETIME_)
#proto splitnumber(_N_)
#proto split(_S_,_T_)
 
main:
s="this string will be separated into parts with space token separator"
aS=0,let( aS :=_split(s," "))
{","}toksep // set a new token separator
{"String: ",s}
{"\nArray:\n",aS},
{"\nSize="}size(aS),println // "size" return an array: {dims,#rows,#cols,#pages}
{"\nOriginal number: ",-125.489922},println
w=0,let(w:=_split number(-125.489922) )
{"Integer part: "}[1]get(w) // get first element from array "w"
{"\nDecimal part: "}[2]get(w),println // get second element from array "w"
{"\nDate by DATENOW(TODAY) macro: "},print
dt=0, let( dt :=_splitdate(datenow(TODAY);!puts)) // "!" keep first element from stack
{"\nDate: "}[1]get(dt)
{"\nTime: "}[2]get(dt),println
 
exit(0)
 
.locals
splitdate(_DATETIME_)
_SEP_=0,gettoksep,mov(_SEP_) // "gettoksep" return actual token separator
{","}toksep, // set a new token separator
_NEWARRAY_={}
{1},$( _DATETIME_ ),
{2},$( _DATETIME_ ),pushall(_NEWARRAY_)
{_SEP_}toksep // restore ols token separator
{_NEWARRAY_}
back
 
splitnumber(_X_)
part_int=0,part_dec=0,
{_X_},!trunc,mov(part_int),
minus(part_int), !sign,mul
xtostr,mov(part_dec), part_dec+=2, // "part_dec+=2", delete "0." from "part_dec"
{part_dec}xtonum,mov(part_dec)
_NEWARRAY_={},{part_int,part_dec},pushall(_NEWARRAY_)
{_NEWARRAY_}
back
 
split(_S_,_T_)
_NEWARRAY_={},_VAR1_=0,_SEP_=0,gettoksep,mov(_SEP_)
{_T_}toksep,totaltoken(_S_),
mov(_VAR1_), // for total tokens
_VAR2_=1, // for real position of tokens into the string
___SPLIT_ITER:
{_VAR2_}$( _S_ ),push(_NEWARRAY_)
++_VAR2_,--_VAR1_
{ _VAR1_ },jnz(___SPLIT_ITER) // jump to "___SPLIT_ITER" if "_VAR1_" is not zero.
clear(_VAR2_),clear(_VAR1_)
{_SEP_}toksep
{_NEWARRAY_}
back
 
</lang>
{{Out}}
<pre>Output:
 
String: this string will be separated into parts with space token separator
Array:
this,string,will,be,separated,into,parts,with,space,token,separator
Size=1,11
 
Original number: -125.49
Integer part: -125
Decimal part: 489922
 
Date by DATENOW(TODAY) macro: 22/11/2021,18:41:20:13
Date: 22/11/2021
Time: 18:41:20:13
 
</pre>
 
Line 3,553 ⟶ 3,648:
<lang zsh>str='Hello,How,Are,You,Today'
IFS=, echo ${(j:.:)${=str}}</lang>
 
=={{header|Amazing Hopper}}==
 
Hopper provides instructions for separating and modifying tokens from a string.
Let "s" be a string; "n" token number:
 
1) {n}, $(s) ==> gets token "n" from string "s".
 
2) {"word", n} $$(s) ==> replace token "n" of "s", with "word".
 
Note: the "splitnumber" macro cannot separate a number converted to a string by the "XTOSTR" function, because this function "rounds" the number to the decimal position by default.
 
<lang Hopper>
#include <hopper.h>
 
#proto splitdate(_DATETIME_)
#proto splitnumber(_N_)
#proto split(_S_,_T_)
 
main:
s="this string will be separated into parts with space token separator"
aS=0,let( aS :=_split(s," "))
{","}toksep // set a new token separator
{"String: ",s}
{"\nArray:\n",aS},
{"\nSize="}size(aS),println // "size" return an array: {dims,#rows,#cols,#pages}
{"\nOriginal number: ",-125.489922},println
w=0,let(w:=_split number(-125.489922) )
{"Integer part: "}[1]get(w) // get first element from array "w"
{"\nDecimal part: "}[2]get(w),println // get second element from array "w"
{"\nDate by DATENOW(TODAY) macro: "},print
dt=0, let( dt :=_splitdate(datenow(TODAY);!puts)) // "!" keep first element from stack
{"\nDate: "}[1]get(dt)
{"\nTime: "}[2]get(dt),println
 
exit(0)
 
.locals
splitdate(_DATETIME_)
_SEP_=0,gettoksep,mov(_SEP_) // "gettoksep" return actual token separator
{","}toksep, // set a new token separator
_NEWARRAY_={}
{1},$( _DATETIME_ ),
{2},$( _DATETIME_ ),pushall(_NEWARRAY_)
{_SEP_}toksep // restore ols token separator
{_NEWARRAY_}
back
 
splitnumber(_X_)
part_int=0,part_dec=0,
{_X_},!trunc,mov(part_int),
minus(part_int), !sign,mul
xtostr,mov(part_dec), part_dec+=2, // "part_dec+=2", delete "0." from "part_dec"
{part_dec}xtonum,mov(part_dec)
_NEWARRAY_={},{part_int,part_dec},pushall(_NEWARRAY_)
{_NEWARRAY_}
back
 
split(_S_,_T_)
_NEWARRAY_={},_VAR1_=0,_SEP_=0,gettoksep,mov(_SEP_)
{_T_}toksep,totaltoken(_S_),
mov(_VAR1_), // for total tokens
_VAR2_=1, // for real position of tokens into the string
___SPLIT_ITER:
{_VAR2_}$( _S_ ),push(_NEWARRAY_)
++_VAR2_,--_VAR1_
{ _VAR1_ },jnz(___SPLIT_ITER) // jump to "___SPLIT_ITER" if "_VAR1_" is not zero.
clear(_VAR2_),clear(_VAR1_)
{_SEP_}toksep
{_NEWARRAY_}
back
 
</lang>
{{Out}}
<pre>Output:
 
String: this string will be separated into parts with space token separator
Array:
this,string,will,be,separated,into,parts,with,space,token,separator
Size=1,11
 
Original number: -125.49
Integer part: -125
Decimal part: 489922
 
Date by DATENOW(TODAY) macro: 22/11/2021,18:41:20:13
Date: 22/11/2021
Time: 18:41:20:13
 
</pre>
 
{{omit from|PARI/GP|No real capacity for string manipulation}}
543

edits