Tokenize a string: Difference between revisions
Content added Content deleted
No edit summary |
mNo edit summary |
||
Line 3,553: | Line 3,553: | ||
<lang zsh>str='Hello,How,Are,You,Today' |
<lang zsh>str='Hello,How,Are,You,Today' |
||
IFS=, echo ${(j:.:)${=str}}</lang> |
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}strtoutf8 |
|||
{"\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_) |
|||
_V1_="", _V2_="", |
|||
_SEP_=0,gettoksep,mov(_SEP_) // "gettoksep" return actual token separator |
|||
{","}toksep, // set a new token separator |
|||
{1},$( _DATETIME_ ), mov(_V1_), |
|||
{2},$( _DATETIME_ ), mov(_V2_), |
|||
{_SEP_}toksep // restore old token separator |
|||
_NEWARRAY_={},{_V1_,_V2_},pushall(_NEWARRAY_) |
|||
{_NEWARRAY_} |
|||
back |
|||
splitnumber(_X_) |
|||
part_int=0,part_dec=0, |
|||
{_X_},trunc,mov(part_int), |
|||
{_X_},minus(part_int),xtostr, |
|||
mov(part_dec),part_dec+=2,{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_), _VAR2_=1, |
|||
___SPLIT_ITER: |
|||
{_VAR2_}$( _S_ ),push(_NEWARRAY_) |
|||
++_VAR2_,--_VAR1_ |
|||
{ _VAR1_ },jnz(___SPLIT_ITER) |
|||
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: 0.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}} |
{{omit from|PARI/GP|No real capacity for string manipulation}} |